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 implement Publish and Subscribe with RabbitMQ

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how RabbitMQ to achieve Publish and Subscribe, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. Message switch [Exchange]

Earlier in the tutorial, we sent and received messages from the queue. In RabbitMQ, it is time to introduce a full-message model.

Let's take a quick look at what our previous tutorials said:

[producer]: it's a user program for sending messages.

[consumer]: it's a user program for receiving and using messages.

[queue]: is a cache area for temporary messages

The core idea of the RabbitMQ messaging model is that [producers] do not send any information directly to the queue. In fact, the producer has no idea whether the message will be delivered to any queue at all.

In contrast, the producer can only send messages to the message switch. Exchange is a very simple thing. On the one hand, it receives messages from the producer, on the other hand, it pushes the received messages to the queue. [message switch] must know exactly how it handles receiving messages. Should it be sent to a specific queue? Should it be sent to multiple queues? Or it should be discarded. This rule is defined by the type of [message switch].

There are some types of [message switch] available: [Direct] direct, [Topic] topic, [Headers] title, and [Fanout] fan out. We will focus on the last one-[Fanout] fan out. Let's create a message switch of this type and name it Logs:

Channel.ExchangeDeclare ("logs", "fanout")

[Fanout] type [message switch] is very simple. As you might guess from the name, it just spreads all the messages it receives to all the queues it knows. This is exactly what we need our logger.

Display the list of [message switches]:

Use Rabbitmqctl to list the most useful [message switches] that can run on the server

Sudo rabbitmqctl list_exchanges

There will be some amq.* [message switches] and default (unnamed) message switches in this list. These are created by default, but it is unlikely that you need to use them now.

Default message switch

In the earlier part of the tutorial, our team [message switch] knew nothing, but I could still send messages to the queues we wanted to go, because we used the default [message switch]. These default message switches are identified by two double quotation marks.

Let's recall how messages were sent in the past:

Var message = GetMessage (args); var body = Encoding.UTF8.GetBytes (message); channel.BasicPublish (exchange: "", routingKey: "hello", basicProperties: null, body: body)

The first parameter is the name of the message switch. An empty string represents a default or unnamed message switch: the message is routed to a queue with the specified routingkey name, if it exists.

Now, we can publish to our named message switch:

Var message = GetMessage (args); var body = Encoding.UTF8.GetBytes (message); channel.BasicPublish (exchange: "logs", routingKey: ", basicProperties: null, body: body)

2. Temporary queue

Maybe you remember the name of the queue we used before (remember Hello and task_queue? ) it's important for us to be able to assign a name to a queue-- because we need to point [Worker] to the same queue. It is important to name the queue if you want to share the queue between the producer and the consumer.

But this is not the case with our logger. We want to hear all the log messages, not just a subset of them. We are only interested in the news we have just received, not the old one. To solve the above problems, we need to do two things.

First of all, whenever we connect to Rabbit, we need a new and empty queue. To do this, we can create a queue with a random name, or even better-let the server choose a random queue name for us.

Second, once we disconnect the queue with the consumer, the queue should be deleted automatically.

In the .NET client, when we didn't provide a parameter for queueDeclare (), we created a non-persistent, exclusive, auto-delete queue with the generated name:

Var queueName = channel.QueueDeclare () .QueueName

At this point, the QueueName contains the random queue name. For example, it might look like amq.gen-jzty20brgko-hjmujj0wlg.

3. Bind [Binding]

We have created a [message switch] and queue of type [Fanout]. Now we need to tell the message switch to send messages to our queue. The relationship between [message switch] and [queue] is called binding.

Channel.QueueBind (queue: queueName, exchange: "logs", routingKey: "")

From now on, the message switch of the log can push messages to the queues we define.

We can view the [binding] list data with the following statement:

Rabbitmqctl list_bindings

4. Integrate all the code

The [producer] program, which sends out log messages, doesn't seem to be very different from previous tutorials. The most important change is that the message we want to send now arrives at the log [message switch] with our specified name, not anonymous. We need to provide a name represented by routingkey when sending a message, but a [message switch] of type [Fanout] can ignore the value of the routingKey.

These are all the contents of the article "how RabbitMQ implements Publish and Subscribe". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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