RabbitMQ Message schema
Code and notes related to each mode md Summarized to gitHub Prefecture ：
1.1.RabbitMQ The core of
The core concept ：
Server： also called Broker , Accept client connections , Realization AMQP Physical services . install rabbitmq-server
Connection： Connect , Application and Broker The Internet connection TCP/IP/ Three handshakes and four waves
Channel： Network channel , Almost all operations are in Channel In the middle of ,Channel It's a channel for reading and writing messages , The client can set up a system for each Channel, Every Channel Represents a conversational task .
Message : news ： Data transferred between services and Applications , from Properties and body form ,Properties But retouching the message , For example, the priority of a message , Advanced features like delay ,Body Is the content of the message body .
Virtual Host Virtual address , For logical isolation , Top level message routing , A virtual host can have several reasons Exhange and Queueu, You can't have the same name in the same virtual host Exchange
Exchange： Switch , Receive a message , Send messages to the bound queue according to the routing key .(== No message storage capability ==)
Bindings：Exchange and Queue Virtual connection between ,binding You can protect multiple routing key.
Routing key： It's a routing rule , Virtual machines can use it to determine how to route a particular message .
Queue： queue ： Has become a Message Queue, Message queue , Save messages and forward them to consumers .
1.2.RabbitMQ The overall architecture
We can see from the architecture diagram that ：RabbitMQ The architecture contains ：Provider（ producer ）、Exchanges（ Switch ）、Queues（ queue ）、Consumer（ consumer ）.
1.3.RabbitMQ Operation flow
The producer may package the business data , Then encapsulate it as a message , send out ( AMQP The command corresponding to this action in the protocol is Basic.Publish) To Broker in . Consumers subscribe and receive messages (AMQP The command corresponding to this action in the protocol is Basic.Consume perhaps Basic. Get) , The original data can be obtained through the possible unpacking process , After that, the business processing logic . This business processing logic does not necessarily need to use the same thread as the receiving logic .
Consumer processes can use a thread to receive messages , Put it in memory , For example, use Java Medium BlockingQueue . Business processing logic uses another thread to read data from memory , In this way, the application can be further interpreted , Improve the processing efficiency of the whole application .
The detailed implementation of the underlying communication process , Those who are interested can learn about another blog post , It's very detailed , I will not elaborate too much here ：
2.RabbitMQ Supported message patterns
Refer to the official website ：https://www.rabbitmq.com/getstarted.html
2.1. Simple mode Simple
In simple mode messaging , There's no need to create switches in particular （ Specify the type of switch ）, Through the default switch Binding Default exchange, By designation Routing key relation Queues Medium queue name To the corresponding Queue Queue to send messages .
Be careful ：
rabbitmq There must be a switch to send messages , If the switch is not created , By default Default_exchange Binding .
Finally to queue1 Send message in , Get the message as follows ：
Ack Mode： Message reply mode
Nack message： After receiving the message, do not reply to the message , And consumers don't respond to feedback to producers ,Queue The message will still be saved in ;
Ack message： Receive the message and reply , At this point, the producer receives a reply from the consumer , take Queue In this message consumption .
2.2. Work queue mode WorkQueues
The work queue mode is also specified as the default default Default switch For many Queue Consumers send messages ,
There are two main patterns ：
1、 Polling mode distribution ： One for each consumer , To distribute equally , average , It will not reduce the sending of messages because of which consumer's faster internal processing speed ;
2、 Fair distribution ： Fair distribution according to the consumption ability of consumers , Faster processing, more processing , Slow processing, less processing ; distribution according to work ;
The difference in the actual code is that the response mode of fair distribution must be set to manual response , And polling mode is set to auto answer ：
// According to the actual scene settings ： Depending on the processing speed of the service 、 Disk space, etc , by 1 It means to consume only at a time 1 strip
finalChannel.basicQos(1); // Fair distribution requires manual response finalChannel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
2.3. Publish subscribe mode Publish/Subscribe
Publish subscribe mode uses fanout type The switch binds every consumer Queue queue , It's equivalent to broadcasting , Subscribe to the switch （ channel ） Of Queue Will receive a message from the producer sent through the switch .
And in the process, consumers Queues Must be with fanout_exchange One to one binding .
2.4. Routing mode Routing
The switch type of routing mode is ：direct type
The routing pattern is very similar to the publish subscribe pattern , It's also very easy to understand . It just adds routing to the publish subscribe model key, Through the corresponding route key With the corresponding Queue Binding , This Queue I was key It's the only sign .
And then when the switch sends the message again , Will be bound to the switch Queue Specify the corresponding route in key Send , Only corresponding routes key For sending key One to one correspondence to receive messages .
Test to Routing key by error Of queue queue （ Queues are bound with routes direct_exchange）：
2.5. The theme Topic Pattern
Theme mode topics It's very similar to routing patterns , It's just that we've added support to routing Fuzzy matching Routing key In the form of .
Switch type selection for theme mode must be topic type .
Matching rules ：
#： Express 0 One or more , The value range is [0,+ infinity ）
*： Express 1 individual , The value is 1, That is, there must be a .
Test queue Routing key as follows , It's all tied up topic Topic type switches ：
You can see , Only queue1 And queue2 Received the corresponding message ：
2.6. Parameter mode
Parameter mode exchange The type is ：headers.
The parameter pattern will match the route according to the corresponding Queue Carry the parameter information to match , and exchange According to arguement Parameters are sent one-to-one ：
Arguments Information identification , Test selection x=1 Send to the queue of ：
You can see queue1 We've received a message from ：
RabbitMQ Supported message patterns ：
Simple mode Simple
- Switch type ：default
- characteristic ： One to one distribution through the default switch
Working mode Work
- Switch type ：default
- characteristic ： Poll distribution ： To distribute equally ; Fair distribution ： distribution according to work ;
Publish subscribe mode
- Switch type ：fanout
- characteristic ： Subscribed Channels , Each queue is bound to the switch , You can get messages
- Switch type ：direct
- characteristic ： Yes route key As identification , Send a message via routing key One on one transmission , There will be a loss in actual performance
- Switch type ：topic
- characteristic ： stay route key On this basis, fuzzy matching is added ,#：0 One or more ;*： There has to be one
- Switch type ：headers
- characteristic ： Portability parameter Arguments, The actual sending matches the queue with corresponding parameters to send