1. Basic concepts | basic concepts | rabbitmq PHP notes

Posted by DJ Digital Gem on Tue, 11 Jan 2022 14:18:21 +0100

1 Broker

Message queuing server entity.

2 vhost

Virtual host. Multiple vhost s can be set up in a broker to separate the permissions of different users. Details can be viewed Official documents.

3 Connect

Connection is the socket link of RabbitMQ, which encapsulates some logic related to the socket protocol.

4 Channel

Channel. Each connection of the client can establish multiple channels, and each channel represents a session task. It is established in the above TCP connection, and the data flow is carried out in the channel. In other words, generally, the program starts to establish a TCP connection and then establishes a channel.

Q: Why use Channel instead of TCP connection directly?

A: For OS, establishing and closing TCP connections is costly. Frequent establishment and closing of TCP connections has a great impact on the performance of the system, and the number of TCP connections is limited, which also limits the ability of the system to handle high concurrency. However, establishing a Channel in a TCP connection does not have the above cost. For a Producer or Consumer, you can use multiple channels to Publish or Receive concurrently.

5 Queue

Queue. It is used to store messages, and each message will be put into one or more queues.

6 Exchange

Message switch. It is used to receive messages and forward messages to the bound queue. It is the core of rabbitMq.

There are four types of exchange types: fanout, direct, topic and headers.

6.1 fanout

Broadcast switches do not handle routing keys. It sends messages to all queues bound to the switch. If a message is sent to an exchange without queue binding, the message will be lost. This is because in rabbitmq, exchange does not have the ability to store messages, and only the queue has the ability to store messages. The pseudo code is as follows:

// Declare switch, switch Name: log, type: fanout
exchange_declare(exchange_name="log", exchange_type="fanout")

// Queue log Info and log Error bind switch log, no routing key is required
queue_bind(queue_name='log.info', exchange_name='log');
queue_bind(queue_name='log.error', exchange_name='log');

// Producer sends message
basic_publish(message='test message', exchange_name='log'); 

// result
log.info and log.error The queue receives messages“ test message"

6.2 direct

Direct exchanger (default). It routes messages to queues whose Binding Key exactly matches the Routing Key. The pseudo code is as follows:

// Declare switch, switch Name: log, type: direct
exchange_declare(exchange_name="log", exchange_type="direct")

// Queue log Info and log Error bind the switch log and match it exactly with the routing key
queue_bind(queue_name='log.info', exchange_name='log', routing_key='route_key_info');
queue_bind(queue_name='log.error', exchange_name='log', routing_key='route_key_error');

// Producer sends message
basic_publish(message='test message', exchange_name='log', routing_key='route_key_info');

// result
 only log.info The queue receives messages“ test message"

6.3 topic

Topic exchange. It will route the message to the queues where the Binding Key and the Routing Key are vaguely matched. In the Binding Key, * is used to match one word, # used to match 0 / 1 / multiple words, and the words are marked with "." Is a delimiter. The pseudo code is as follows:

// Declare switch, switch Name: social, type: topic
exchange_declare(exchange_name="social", exchange_type="topic")

// The queue qq and wechat are bound to the switch social, and the routing key is used for fuzzy matching
queue_bind(queue_name='qq', exchange_name='social', routing_key='#.qq');
queue_bind(queue_name='wechat', exchange_name='social', routing_key='#.wechat.*');

// Producer sends message
basic_publish(message='This is QQ account number', exchange_name='social', routing_key='account.qq');
basic_publish(message='This is QQ Temporary session messages', exchange_name='social', routing_key='temporary.msg.qq');
basic_publish(message='This is a wechat account', exchange_name='social', routing_key='account.wechat');
basic_publish(message='This is a wechat friend', exchange_name='social', routing_key='account.wechat.friend');

// result
wechat The queue will receive the message "this is a wechat friend"
qq The queue receives the message "this is QQ Account number "and" this is QQ Temporary session message

6.4 headers

Headers do not rely on the matching rules of Binding Key and Routing Key to route messages, but match according to the headers attribute in the sent message content.

Specify a set of key value pairs when Binding Queue to Exchange. When a message is sent to Exchange, RabbitMQ will get the headers of the message (also in the form of a key value pair) and compare whether the key value pairs match the key value pairs specified when Binding Queue to Exchange. If there is an exact match, the message will be routed to the Queue, otherwise it will not be routed to the Queue. (it is rarely used, so it is not tested)

7 Routing Key

Routing Key. When sending a message to Exchange, the producer usually specifies a Routing Key to specify the routing rules of the message, and the Routing Key needs to be used in combination with Exchange Type and Binding Key to take effect. By specifying the Routing Key, you can determine where the message flows.

8 Binding Key

When Binding Exchange and Queue, a Binding Key is usually specified; When the Binding Key matches the Routing Key, the message will be routed to the corresponding Queue. The Binding Key depends on the Exchange Type. When the switch type is fanout, Exchange will ignore the Binding Key and route messages to all queues bound to the Exchange.

9 Producer

Message producer is the program that delivers messages.

10 Consumer

Message consumers are programs that receive messages.

If the article helps you, don't forget to like it

This article was first published in LearnKu.com On the website.