Objective
Recent projects to use RABBITMQ, the park has a lot of excellent articles, RABBITMQ official website also has. NET instances. Here I try to record the use of the process in the form of the text and Mao.
Installation
RABBITMQ is built under the Erlang OTP platform, so you need to download and install the following two components under Windows:
1. Erlang OTP for Windows
2. Rabbit MQ Windows Service Install
RABBITMQ generally also needs to install its HTTP-based management platform plug-in, which allows us to view and manage RABBITMQ, which can be enabled by the command line:
Rabbitmq-plugins Enable Rabbitmq_management
After that you can login via http://localhost:15672/, the password and password of the account can use the default guest/guest, login after the page:
RabbitMq use
is the specific use of the process
The box is RABBITMQ's message broker, and we'll explain it in the following way:
1. Send the message to the Exchange
2. After the exchange accepts the message, it is responsible for routing it to the specific queue
3. Bindings is responsible for connecting Exchange and queues (queue)
4. The message arrives in the queue, and then waits to be processed by the receiving end of the message
5. Message receiving end processing message
Initialize RABBITMQ
Now we use the code to demo the process, first get rabbitmq.client on NuGet:
Then create the exchange,bingding,queue required for 2,3,4 step in the diagram
stringExchangename ="Test.exchange";stringQueueName ="Test1.queue";stringOtherqueuename ="Test2.queue";using(Iconnection conn =rabbitmqfactory.createconnection ())using(IModel channel =Conn. Createmodel ()) {//2 Define an ExchangeChannel. Exchangedeclare (Exchangename,"Direct", Durable:true, Autodelete:false, arguments:NULL); //4 definition of two queueChannel. Queuedeclare (queuename, durable:true, Exclusive:false, Autodelete:false, arguments:NULL); Channel. Queuedeclare (otherqueuename, durable:true, Exclusive:false, Autodelete:false, arguments:NULL); //3 Defining Exchange-to-queue bindingChannel. Queuebind (QueueName, Exchangename, routingkey:queuename); Channel. Queuebind (Otherqueuename, Exchangename, routingkey:otherqueuename);}
After successful creation, it can be viewed in Rabbit's management platform:
Send Message
From the picture we learned that the message was sent to RABBITMQ exchange, but we can pass some message properties such as Routingkey, and in the code we pass the name of the queue as Routingkey:
stringExchangename ="Test.exchange";stringQueueName ="Test1.queue";stringOtherqueuename ="Test2.queue";using(Iconnection conn =rabbitmqfactory.createconnection ())using(IModel channel =Conn. Createmodel ()) {varProps =Channel. Createbasicproperties (); Props. Persistent=true; varMsgbody = Encoding.UTF8.GetBytes ("Hello, world!."); //1. Send the message to Exchange, but add RoutingkeyChannel. Basicpublish (Exchangename, Routingkey:queuename, Basicproperties:props, body:msgbody); Channel. Basicpublish (Exchangename, Routingkey:otherqueuename, Basicproperties:props, body:msgbody);}
After sending, you can see it on the RABBITMQ:
Receiving messages
stringQueueName ="Test1.queue";stringOtherqueuename ="Test2.queue";using(Iconnection conn =rabbitmqfactory.createconnection ())using(IModel channel =Conn. Createmodel ()) {//5. Getting messages from the Test1.queue queueBasicgetresult Msgresponse = Channel. Basicget (QueueName, Noack:true); stringMsgbody =Encoding.UTF8.GetString (msgresponse.body); //5. Getting messages from the Test2.queue queueMsgresponse = Channel. Basicget (Otherqueuename, Noack:true); Msgbody=Encoding.UTF8.GetString (msgresponse.body);}
Summarize
We have a general understanding of the use of RABBITMQ through the above description.
Introduction to. NET Using RabbitMQ Graphics