In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the switch types of message queuing Rabbitmq". In daily operations, I believe many people have doubts about the switch types of message queuing Rabbitmq. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the questions of "what are the switch types of message queuing Rabbitmq?" Next, please follow the editor to study!
I. switch type
In rabbitmq, messages from producers are received through the switch, and then distributed from the switch to different queues. In the process of distribution, the switch type will affect the logic of the distribution.
There are four types of switches in rabitmq, which are fanout, direct, topic and headers, of which the first three are more common and the latter is less used.
II. Fanout
In general, the switch distribution will first find the bound queue, and then judge the routekey to decide whether to distribute the message to a queue; but if the type of the switch is fanout, then the switch will no longer judge the routekey, but will distribute the message directly to the bound queue, as shown in the following test code
Channel channel = connection.createChannel (); / / create a channel in rabbitmq
Channel.exchangeDeclare ("exchangeName", "fanout"); / / create a switch with type as fanout
Channel.queueDeclare ("queueName"); / / create a queue
Channel.queueBind ("queueName", "exchangeName", "routingKey"); / / bind queues and switches
III. Direct
In the case of type direct, the switch will also get the bound queue when distributing the message, and then judge routeing;. When the switch discovers that the type of direct determines that the rule of routeing is an exact match pattern, it will distribute the message to the specified queue only if the message is completely equal to routeing.
Multiple routing keys can be specified in a queue. Let's assume that there are two queues, queue 1 and queue 2. Three routing keys, zhangsan and lisi,wangwu, are specified in queue 1, and a queue key lisi is specified in queue 2. The code for specifying multiple routing keys is as follows:
Channel channel = connection.createChannel (); / / create a channel in rabbitmq
Channel.exchangeDeclare ("exchangeName", "direct"); / / create a switch with type as direct
Channel.queueDeclare ("queueName"); / / create a queue
Channel.queueBind ("queueName", "exchangeName", "zhangsna"); / / bind and set routing keys
Channel.queueBind ("queueName", "exchangeName", "lisi"); / / bind and set routing keys
Channel.queueBind ("queueName", "exchangeName", "wangwu"); / / bind and set routing keys
When the producer sends a message with the routeting of zhangsan to the switch, the switch will only distribute the message to queue 1 when it is distributed, because the switch only matches queue 1 when the routeting matches, so queue 2 will not receive the message.
When the producer sends a message with the routeting of lisi to the switch again, the switch will distribute the message to queue 1 and queue 2 when it is distributed, because the switch matches successfully when the routeting matches, so both queues receive the message
IV. Topic
In the case of type topic, the switch also needs to match both bindKey and routingKey; when distributing messages, but unlike the direct type, when the switch finds that the type is topic, the rule for judging routeing is a fuzzy matching pattern.
Rabitmq defines a set of matching rules. Here, I assume that the producer sends a message where the routingKey is wiki.imooc.com. If the switch is of type topic, if you want to get this message, you can use the * sign as a wildcard to specify routingKey, which are *. *. Com, *. Imooc.*, * wiki.imooc.*; can also use # as a wildcard to specify routing keys, such as wiki.# and # .com.
In the wildcard column above, we need to master these points:
The routing key is called. For delimiters, each separator represents a word
The wildcard character * matches one word, and the wildcard character # can match multiple words
* can be used on routingKey and bindKey, # can only be used in RoutingKey
5. Headers
The switch of type headers is completely different from the previous three matching Fangs. It does not rely on bindingKey and routingKey, but specifies a key-value pair when binding the queue and the switch. When the switch distributes the message, it will first unlock the headers data in the message body, and then determine whether there is a set key-value pair in it. If the match is found, the message will be distributed to the queue. This type of switch is relatively poor in performance and is rarely used in practical work.
At this point, the study on "what are the switch types of message queuing Rabbitmq" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.