Exchange-fanout broadcast mode,

Source: Internet
Author: User

Exchange-fanout broadcast mode,
I. Preface

We can see that the producer delivers the message to the Queue. In fact, this will never happen in RabbitMQ. Actually, the producer sends messages to Exchange (X in the switch), and Exchange routes messages to one or more Queue (or discard messages ).

 

There are four types of Exchange in RabbitMQ, and different types have different routing policies. The Exchange types commonly used by RabbitMQ include fanout, direct, topic, and headers. What logic does Exchange route messages to the Queue? In RabbitMQ, Exchange and Queue are associated through Binding, so that RabbitMQ knows how to route messages to the specified Queue correctly.

  • Fanout type Exchange routing rules are very simple. It routes all messages sent to the Exchange to all the Queue bound to it.
  • Direct Exchange routing rules are also very simple. They route messages to the Queue where the binding key and the routing key exactly match.
  • In the topic type, all the queues of the routingKey that match the routingKey (this can be an expression) can receive messages.
  • The headers type Exchange does not rely on the matching rules of the routing key and binding key to route messages. Instead, it matches the headers attribute in the sent message content.
Ii. fanout broadcast mode

In the previous example, all messages are sent and received on a one-to-one basis, that is, messages can only be sent to the specified queue, but sometimes you want your messages to be received by all Queue, similar to the broadcast effect, fanout in exchange mode is required at this time.

Production end:

#-*-Coding: UTF-8-*-import pika # declare a connection = pika. blockingConnection (pika. connectionParameters (host = 'localhost') # declare a channel = connection. channel () # declare an exchange, name exchange, and define its type channel. exchange_declare (exchange = 'test-exchange ', exchange_type = 'fanout') # message content message = 'hello, world! '# Generate a message channel. basic_publish (exchange = 'test-exchange ', routing_key = '', # Set to null body = message,) print (' [x] Sent % R' % message) # Close connection. close ()

Consumer:

#-*-Coding: UTF-8-*-import pika # create a connection = pika. blockingConnection (pika. connectionParameters (host = 'localhost') # create a channel = connection. channel () # declare an exchange, name exchange, and define its type # consistent channel with the production end. exchange_declare (exchange = 'test-exchange ', exchange_type = 'fanout') # Here we want to declare the queue # although we didn't declare the queue on the production end, the message is broadcast, send to all queues # No Name is required for the queue declared here. rabbitmq will randomly assign a name # That is to say, the consumer still needs to get the message content from the queue result = channel. queue_declare (exclusive = True) # exclusive = True will automatically delete the queue after the consumer using this queue is disconnected # queue name queue_name = result. method. queue # bind exchangechannel. queue_bind (exchange = 'test-exchange ', queue = queue_name) print (' [*] Waiting for logs. to exit press CTRL + C') def callback (ch, method, properties, body): print ("[x] % r" % body) channel. basic_consume (callback, queue = queue_name, no_ack = True) channel. start_consuming ()

Each consumer can obtain the messages produced by the producer.

Note: The broadcast mode is real-time. That is to say, when a message is generated on the production end, the consumption end must be in the listening status before obtaining the message from the queue, otherwise, the message cannot be received, even if it is started again. Just like the radio station and the radio station, the content of the radio station is always played. If the radio is not turned on, it cannot be listened to. Even if it is turned on later, the previous content is also played out.

Note:

Exclusive = True # The queue name exists only when the consumer side exists, and exchange will send messages to the queue #. Once the consumer side is closed, the queue will be deleted.

Run three consumers. If one consumer is disabled, the queue in rabbitmq is directly deleted.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.