Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.

📖 preface

Good attitude, not so tired. In a good mood, all you see is beautiful scenery.Copy the code

"If you can't solve a problem for a while, use this opportunity to see your limitations and put yourself out of order." As the old saying goes, it's easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.

RabbitMQIntroduction to the

AMQP (Advanced Message Queuing Protocol) is an open standard of application-layer protocols designed for message-oriented middleware. Message-oriented middleware is primarily used for decoupling between components so that the sender of a message does not need to be aware of the message consumer and vice versa. The main characteristics of AMQP are message orientation, queue, routing (including point-to-point and publish/subscribe), reliability, and security. RabbitMQ is an open source IMPLEMENTATION of AMQP. The server is written in Erlang and supports a variety of clients, such as Python, Ruby,.NET, Java, JMS, C, PHP, ActionScript, XMPP, STOMP, and AJAX. It is used to store and forward messages in distributed systems, and has good performance in ease of use, scalability, and high availability.

The best place to learn RabbitMQ is of course on its website: www.rabbitmq.com/.

First of all, there are a few concepts about queues


  1. Producer producer: Producers send messages to queues
  2. Consumer: The consumer gets the message from the queue
  3. Exchange: x in the figure, subscription mode, producers send messages to the switch, and the switch chooses whom to send the message to
  4. Channel: A channel must be created to link to MQ
  5. Queue: The place where messages are stored

Six queue modes are supported

I. Simple Mode (“Hello World!”)


One producer, one consumer

The production sends the message, the consumer gets the message, and the process is over.

Ii. Work Mode (Work queues)


One producer has many consumers

Production sends a message that can only be accessed by one consumer.

If the producer sends multiple messages, the consumer will not get the same message twice.

PS: By defaultRabbitMQEach message is sent to the next consumer in order. On average, each consumer will get the same number of messages. This way of distributing messages is called a loop. Try it with three or more workers.

Message confirmation: two types of automatic confirmation (easy to lose data) and manual confirmation (not easy to lose data)

Manual validation: We explicitly turn them off with the autoAck = true flag. Once we have completed the task, we should set this flag to false and send the appropriate confirmation from staff. If you forget to return acknowledgement, messages cannot be released and memory piles up.

Message persistence: Message persistence and message nonpersistence

Message persistence: Data is written to disk and not lost (queues and messages are marked persistent)

Message nonpersistence: Data is written to memory and stored fast

Fair to send

Able men are always busy

Use the basicQos method and prefetchCount = 1 setting. Do not send a new message to a worker until the previous message has been processed and confirmed. Instead, it sends it to the next worker who is still not busy.

Iii. Subscription Model (Publish/Subscribe)


Similar to the Work pattern, one producer has multiple consumers, but with an exchange in the middle, a message can be retrieved by multiple consumers.

The producer sends the message to the switch, which allocates the message to the “bound” consumer, provided that the consumer is bound to the switch.

PS: P: producer (sends data) X: switch queue: stores data (message buffer) C: consumer (gets data)

** The core idea of the messaging model in RabbitMQ is that the producer will never send any message directly to the queue. In fact, producers often don’t even know if a message will be delivered to any queue. **

Flow: Messages are sent toExchanges(Switch),ExchangesThe message is sent to the bound queue, and the consumer gets the queue message

Four kinds ofExchangesType:direct, topic, headers and fanout

The type of switch used to publish subscriptions isfanout

Iv. Routing Mode (Routing)


Similar to the subscription model, there is also a producer with multiple consumers, and there is an exchange in the middle. A message can be obtained by multiple consumers. However, he sets an extra key when delivering the message, and the consumer sets one or more keys when receiving the message. Only when the keys match, can the message be received.

For example, consumer 1 sets insert and consumer 2 sets insert and update keys. If a producer sends an INSERT key, both consumers get the data. If the producer sends a key as update, then only consumer 2 will get the data.

5. Wildcard (topic) pattern (Topics)


Similar to the routing mode, there is also a producer with multiple consumers, and there is an exchange in the middle. A message can be obtained by multiple consumers. Same pass key, but its key can be fuzzy match, * match one word, # match 0 or more words.

For example, consumer 1 sets item.# and consumer 2 sets key item.*. Consumer number one can get all messages beginning with item, and consumer number two can only match item. A message followed by one word. For example, if the message is item.insert, it can be retrieved, but item.insert.update cannot be retrieved.

A message sent to a topic exchange cannot have any routing_key – it must be a dot-separated list of words. Words can be anything, but usually they specify some functionality related to the message. Examples of valid routing keys: “stock.USD. Nyse”, “NYx.vmw”, “quick.orange.rabbit”. The routing key can contain any number of words, up to 255 bytes.

The binding key must also take the same form. The logic behind topic exchange is similar to direct exchange – a message sent using a specific routing key will be delivered to all queues bound to a matching binding key. However, there are two important special cases of binding keys:

  • * (asterisk)Can replace a word.
  • # (hash)Zero or more words can be substituted.

Remote Procedure Call (RPC)


Building RPC systems with RabbitMQ: clients and scalable RPC servers. Since we don’t have any time-consuming tasks worth distributing, we will create a virtual RPC service that returns Fibonacci numbers.

RPCWill work like this:

For RPC requests, the client sends a message with two properties: replyTo, set to an anonymous exclusive queue created only for the request; And correlationId, set to a unique value for each request.

Requests are sent to the rpc_queue queue.

The RPC worker (aka: Server) is waiting for a request on this queue. When a request occurs, it executes the job and sends a message with the result back to the client using a queue from the replyTo field.

The client waits for data on the reply queue. When a message appears, it checks the correlationId property. If it matches the value in the request, the response to the application is returned.


PS: Finally, thank you for your patience to watch the end, leave a “like” collection is your biggest encouragement to me!


🎉 summary:

  • For more references, see here:The Blog of Chan Wing Kai

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!