Masstransit publish vs send5/8/2023 – Making a postman, curl, etc to this method will fire a message to your companies controller. If you look into the sample, you will find the “SendMessage” method is called inside “SubscribeToCompany” put request of the user controller. – To communicate between Microservices, I mentioned in the post there is a method named “SendMessage” – Then as shown above in the startup.cs, configure your microservices with MassTransit, – Then get RabbitMq’s password and username, and put them in the appsettings.json of the microservices – For messaging between the two microservices to work, you need to first run rabbit mq in a docker container or anywhere on a network you have access to. The code bellow will not only initialize configurations for rabbitmq and masstransit, but also set up our consumers and start the bus service. The source code for this post is found here.įirst, we need to add mass-transit to our services. If you like this post, don’t hesitate to follow me on Twitter or Gi t hub and subscribe to this page’s notifications to stay updated with new articles or like my page on Facebook. Messages sent contain metadata about the message. When a message is sent with MassTransit via rabbitmq, it is sent to an exchange with the name corresponding to the type of the message’s class. To receive a message, you need to subscribe a consumer, this processes the message. And events are sent to whosoever is willing to subscribe to them. A command is a message sent to a specific endpoint. Messages are sent using Events and Commands. Switching from rabbitmq to another tool will just be a matter of changing configurations. MassTransit is a library that provides a good layer of abstraction above rabbitmq and other messaging tools. Though we can perfectly use this when communicating between our micro-services, it will require much work, and what if in the future we want to change the messaging system from Rabbitmq to Service bus or something else? What if we want to do things in a clean and maintainable way ?. Using the notions of queues and exchanges, listening for messages with loops, and all the like. Rabbitmq has a library that permits us to communicate in a raw fashion. Masstransit and its Rabbitmq abstraction layer All these are through rabbitmq and asp.net core. Thereafter, the company service broadcasts a message telling the subscription process is over “Event”. Once it receives a call, it asynchronously tells the companies service to add the user to the list of subscribers in the specified company “Command”. The users’ service will receive API calls to subscribe to specific companies. One will be the user’s service and the other will be the companies service. To demonstrate micro-services communication, we will build two simple rest APIs which will communicate with each other. The image’s name is:ĭocker run - d - t - it - hostname my - rabbitmq - name rabbitmq3 - server - p 15672 : 15672 - p 5672 : 5672 - e RABBITMQ_DEFAULT_USER = user - e RABBITMQ_DEFAULT_PASS = password - e RABBITMQ_DEFAULT_VHOST = my _vhost rabbitmq : 3 - management It is easy to install rabbitmq, you can download it directly from their website and install it on your operating system. We will use one of these libraries ( Masstransit). Though rabbitmq seems a little bit complicated in the beginning, there are awesome libraries that abstract all of these AMQP notions and make it easier to use when building your ASP.net core microservices. There are several types of exchanges in Rabbitmq and each determines the way in which your message is routed to corresponding queues. Some other attributes of this communication process could be configured, like sending acknowledgments when a message is received, persisting a queue in a database e.t.c. You can learn more about this process here. The message is then received by another application that subscribes to the given queue ( Consumer). The broker then receives the message via an Exchange which is then responsible for routing that message to appropriate Queues using routing keys and rules called bindings. An application willing to send messages ( Producer) to another application, does so through a broker ( Rabbitmq). Here is a brief overview of how messages are sent via rabbitmq. As we mentioned earlier, Rabbitmq implements the AMQP protocol.
0 Comments
Leave a Reply. |