Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the routing knowledge and usage of RabbitMQ

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about how to understand the routing knowledge and usage of RabbitMQ. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Routing knowledge of RabbitMQ

1. Introduction of routing knowledge of RabbitMQ

1. For producers, it can produce one class of messages or multiple types of messages. Once multiple types of messages are produced, how can producers send different types of messages to different consumers?

In fact, the most common way to answer this question is routing, which allows producers to forward different types of messages to different consumers according to different routing rules.

RabbitMQ uses this strategy, but for producers, it is the exchange that connects directly to the consumer. Routing distributes different types of messages to different message queues (queue) according to the set routing rules, waiting for consumers to pick up messages from their queues.

Here is a schematic diagram of RabbitMQ:

2. Introduce the routing rule of RabbitMQ

The routing rule of RabbitMQ is controlled by the type defined by Exchange, while RabbitMQ has four exchange types: direct, topic, headers, and fanout.

1)。 Direct exchange's routing algorithm: compare the binding_key of the exchange with the routing_key of the message. If there is an exact match, it means that the queue needs to be distributed.

2)。 Fanout exchange's routing algorithm: it broadcasts received messages to all bound queues.

3)。 Topic exchange's routing algorithm: binding key has the same logic as direct, and the messages it receives are distributed to all binding queues that match its routing key.

(note: there are certain restrictions on the routing key of exchange messages of type Topic, which must be a group that uses "." Separate words. Words can be arbitrary, but generally speaking, it is better to express the function accurately. Such as the following examples are legal: "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit". Routing key can be any number of words, but its total length cannot exceed 255bytes.)

4)。 Headers Exchange routing algorithm: does not rely on the matching rules of routing key and binding key to route messages, but matches according to the headers attribute in the content of the message sent. Workflow:

1 > when a queue is bound to the head switch, multiple headers (header) are bound at the same time.

2 > the message will carry a header and a "x-match" parameter. When "x-match" is set to "any", any value of the header is matched to satisfy the condition, while when "x-match" is set to "all", all values of the header are required to match successfully.

Note: the head switch can be seen as another form of directly connected switch. However, the routing key of a directly connected switch must be a string, and the header attribute value does not have this constraint, and they can even be integers or hashes (dictionaries), and so on. More flexible (but in fact we rarely use head switches).

3. Since the producer is bound to the route and will put messages on different queues according to the set routing rules, how is the queue associated with the route?

The answer is through the binding relationship, the connection between the route and the Queue in RabbitMQ, but this connection is associated with something called Binding (binding). Of course, this kind of Binding is not random, but there are rules. The rule here is called binding_key. The API used in amqp is as follows:

Func (ch * Channel) QueueBind (name, key, exchange string, noWait bool, args Table) error

Second, below we use examples to illustrate how the above routing types are used.

1. Example of direct type

1) Code: producer code on the left and consumer code on the right.

For direct, you need to distribute messages to different queues through binding_key and the routing_key of the message.

In the producer code, the exchange name testdirect and type direct are defined, and then publish specifies that the accepted exchange is testdirect when producing the data, and specifies two key values of routing_key: hello and other.

In the consumer code, exchange is also defined as testdirect and direct types, and when you bind Queue, you only need to associate it with testdirect.

2), running result

Two consumers are also started, one to receive messages from the hello message queue and one to receive messages from the other message queue.

Sending end

2. Example of fanout type:

1) Code: consumer code on the left and producer code on the right.

For the routing type fanout, the code implementation is relatively simple. 1) the producer side needs to declare the name of exchange and specify the name of exchange when producing the message publish. 2) the consumer side also needs to declare the name of exchange. The name here needs to be the same as that of the producer. At the same time, you can specify the name corresponding to exchange when binding queue, that is, QueueBind.

2), running effect

1 > Open two terminals and start a consumer, as shown in the following figure:

Send a message to a routed hellotest of type fanout through the producer, as shown below. As can be seen from the consumer's output in the figure above, consumer 1 and consumer 2 receive the content of the message immediately every time the consumer sends a message.

3) View the data on rabbitmq

Let's check the correspondence between exchange,binding and queue on rabbitmq sever through the command of rabbitmq, as shown in the following figure:

According to the command display, 1) two consumers each generated two queue, named amq. Represented by the random code at the beginning. 2) hellotest is of type fanout. 3) the binding relationship shows that hellotest has a binding relationship with both queues.

3. Topic type

1) Code: consumer code on the left and producer code on the right.

Code of type topic, 1) the producer needs to declare topic when defining exchange, and then in Publish, associate the name testtopic of exchange with the route_ key value that can be defined by itself. 2) consumers need to declare the same exchange and specify the type as topic. When binding the message queue, associate the corresponding exchange and set binding_key. Here, it is designed to rely on command line input.

2), running result

Send messages 4 times in turn through the producer, as shown in the following figure:

Consumer 1: the configured route_ key values are "flower.*" and "* .big", indicating that the first character is flower and the subsequent characters are arbitrarily matched; the first arbitrary character and the second character are the two key values of big, will be written to this message queue. So the consumer consumes three messages: "animal.big", "flower.small" and "flower.big".

Consumer 2: the configured route_key is "#", which means that any character is accepted, similar to the fanout mode. So the consumer will consume all the messages.

Consumer 3: the configuration is an exact match format "flower.big", so the consumer will only hit the "flower.big" message.

3) View the data on rabbitmq

Through the following binding command, we can see the three different message queues. The bound exchange is the testtopic route. As for which consumer this queue corresponds to, please refer to the log information in 2).

4. Header type

Note: because the implementation of the header type is completely different from the matching rules of direct, fanout, and topic above, it can even be said to be another set of implementation.

The above is how to understand the routing knowledge and usage of RabbitMQ. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report