In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to master Rabbitmq". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to master Rabbitmq" together.
RabbitMQ is a powerful open source messaging broker. According to the official website, it is also the most widely used message queue. Like his slogan "Messaging that just works," it's simple to use out of the box and supports multiple messaging protocols (AMQP, STOMP, MQTT).
How does an application or service use RabbitMq?
First, there will be two roles: producer and consumer; producer connects to rabbit proxy service, creates an AMQP channel, and then publishes the generated message to the switch through the channel, and the switch binds to or routes to the queue according to routing rules (routing key). The last message to arrive on the queue. Consumers, like producers, need to first create a connection with rabbit proxy servers, create a message pipeline, subscribe to the queue, and then get messages from the queue for processing. This involves several important concepts such as messages, switches, bindings, and queues, which will be explained one by one. The whole process is shown in the figure
message
Producers create messages, where messages mean? The message consists of two parts: payload and label. Payload is the data you want to transmit. The tag describes the payload and RabbitMQ uses it to determine who gets a copy of the message. In fact, you will also find that unlike tcp protocol, AMQP does not have a clear receiver, only uses the label to express the message, and then gives the message to Rabbit. Rabbit sends messages to interested recipients based on the tag.
queue
The message eventually arrives in the queue and waits for consumption. Consumers subscribe via AMQP's Basic.Consume command. Doing so sets the channel to accept mode until you unsubscribe from the queue. After subscribing, consumers can automatically receive the next message from the queue after consuming (or rejecting) the most recently received message.
Note: When does the message get removed from the queue? This involves an action of message acknowledgement. Every message received by the consumer must be acknowledged. Is removed from the queue. Consumers can explicitly send an acknowledgement to rabbitmq via AMQP's Basic.Ack command, or set the autoAck attribute to true when subscribing to the queue; e.g. autoAck: true, rabbitmq automatically considers the message acknowledged once the consumer receives it.
If a consumer receives Consum1 and then disconnects from Rabbit before acknowledging it, Rabbit MQ assumes that the message was not distributed and redistributes it to the next subscribed consumer. The benefit of doing this is that even if your app crashes, you can ensure that the message will be sent to another consumer for processing, or wait for your app to restore its normal connection and continue to consume. Assuming that consumer A's program disconnects from rabbit, the message is then consumed by consumer B. as shown below
Rabbit will not send a message to a consumer unless the consumer confirms, because rabbit assumes that the consumer is not ready to receive the next message until the previous message has been confirmed.
There is no way to confirm the message normally. It cannot be blocked all the time. For example, consumers have bugs. Then use AMPQ's Basic.Reject command; explicitly reject the message, and if one of the parameters request is set to true, Rabbit will resend the message to the next subscribing consumer.
If you detect an error in a message that no consumer can handle, you can set requeue to false and rabbitmq will remove the message from the queue rather than send it to a new consumer.
Note: here you can use special handling for rejected messages, such as sending them to a dead letter queue or to a specially collected erro queue.
Summary: queues are the basic modules of amqp messaging
1. The location provided for the message, where the message awaits consumption
2. Queue is an excellent solution for Load Balancer. Just attach a bunch of consumers and let rabbitmq evenly distribute incoming messages in a circular fashion.
Queue is the last destination of messages in rabbit.
Switch, binding
We know how the consumer gets the message, so the question is, how does the message get to the queue? When a message is sent to a switch, RabbitMQ determines which queue the message should be delivered to, according to certain rules. These rules are called routing keys. Queue is bound to switch by routing key. When you send a message to a proxy server, the message will have a routing key. For example, AMPQ's Basic.Publish method has a parameter routingKey specified by it. RabbitMQ matches it to the routing key used by the binding even if it is empty.
There are four types of switches: direct, fanout, topic, and headers; each type implements a different routing algorithm; the first three are more common. This pattern is very simple: if the routing key matches, the message is delivered to the corresponding queue. Routing algorithm-uses routing keys and queue names with the same name to route messages. Usage Scenario-Used when sending messages directly to a specified queue.
The default direct switch does not need to be declared. The queue declaration is automatically bound to the default switch, and the queue name is used as the right key. Use the following code to send message declarations to the queue.
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
2. fanout exchanger
In this pattern, the routing key can be ignored, and the only thing to do is write a piece of code for the new consumer, declare the new queue, and bind it to the fanout switch. When you send a message to a fanout switch, it delivers the message to all the queues attached to the switch. Routing algorithm-Messages are routed to all queues bound to the switch. Use Scenario-Broadcast Features of Publish-Subscriptions
2. topic exchanger
This type of switch allows messages from different sources to reach the same queue. Routing algorithm-routes messages to bound queues based on full or partial routing key matches. Usage Scenarios-Broadcast to specific queues based on certain conditions.
Thank you for reading, the above is "how to master Rabbitmq" content, after the study of this article, I believe we have a deeper understanding of how to master Rabbitmq this problem, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.