Noun interpretation
connection
TCP link between client and server
Channels channel
For network channels, almost all operations are carried out in channels. Multiple channels can be established and multiplexed with the same TCP link
Message message
Data transmission consists of body and properties. Body is the content of the message entity
The values of properties are as follows
'content_ Type '= >' type ',
'content_ Encoding '= >' encoding ',
'application_headers' => 'headers',
'delivery_mode' => '',
'priority' => '',
'correlation_id' => '',
'reply_to' => '',
'expiration' => '',
'message_id' => '',
'timestamp' => '',
'type' => '',
'user_id' => '',
'app_id' => '',
'cluster_id' => '',
Virtual host virtual host
It is used for logical isolation. The top-level route is equivalent to MySQL database
A virtual host can have multiple exchanges and queues. The same virtual host cannot have exchange and queue with the same name
Exchange switch
Receive the message and distribute the message to the bound queue according to the routing key
Each virtual host has a default exchange: AMQP default
By default, exchange will bind all queues and cannot bind. You can directly bind with the queue name as the routing key. If there is no queue, the message will be discarded
Four types of switches:
-direct:
Compare the routing keys in the message with those in all bindings associated with the exchange. If they are equal, they will be sent to the queue corresponding to the binding.
-topic:
The one above is equivalent matching, and this one is fuzzy matching
*Match a word
#Configure 0 or more characters
*, # can only be written in Left and right, and cannot be next to characters
Words need to be used between words separate
You can also directly specify a queue name without *, #, to achieve the direct effect
-fanout:
The message is directly forwarded to the corresponding queue of all binding. This exchange ignores the routing key and is the most efficient. Fanout > Direct > topic
-headers:
The headers in the message are matched with the binding parameters associated with the exchange. If they are matched, they are sent to the queue corresponding to the binding
Queue queue
A queue for storing messages for consumer consumption
Routing key routing rules
binding
The binding between exchange and queue can include routing key
Dead letter queue
1. The message is denied and confirmed
2. The message lifetime in the queue exceeds the set TTL time
3. The number of messages in the message queue has exceeded the maximum queue length
The message will become a dead letter. If the dead letter queue is configured, the message will be thrown into the dead letter queue. If it is not configured, the message will be discarded
Configuration process:
1. Create a dead letter exchange: ex_ Dlx, a dead letter queue: queue_ Dlx, via routing key: route_ Dlx binding
2. Create a business exchange and a business queue, which are bound through the routing key, in which the business queue adds attributes
x-dead-letter-exchange:ex_dlx
x-dead-letter-routing-key:route_dlx
With the x-message-ttl attribute, the delay queue can be realized, and the function of closing orders without payment for 15 minutes can be realized
Mandatory
When sending a message, when the mandatory parameter is set to true, the switch cannot find a qualified queue according to its type and routing key, then rabbitmq will call basic The return command returns the message to the producer
Alternate exchange backup switch
When creating exchange, you can set the backup switch. When the message is unreachable, it will be sent. The priority is higher than the mandatory
Message TTL expiration time
TTL can be set when adding queues and publishing messages. If both are set, the one with shorter time shall prevail. When the message exceeds the value set by TTL, it will become dead letter
Attribute value: x-message-ttl
Queue TTL expiration time
Like messages, queues also have expiration times, which are automatically deleted when the time comes
Attribute value: x-expires
Persistence
交换机Persistence:设置durable:true
队列Persistence:设置durable:true
消息Persistence:发送消息的时候设置属性deliveryMode:2
Transaction mechanism
- channel. Txselect is used to set the current channel to transaction mode
- channel. Txcommit is used to commit transactions
- channel. Txrollback is used for transaction rollback
但是使用Transaction mechanism会吸干RabbitMQ的性能,所以有了下面的发送方确认机制
Sender acknowledgement mechanism
channel. confirmSelect(); // This mechanism is asynchronous
Message distribution basicqos allows to limit the maximum number of unacknowledged messages that consumers on the channel can maintain
channel. basicQos(5, false); // It means that when a single consumer consumes five messages and there is no ACK, the server will not distribute messages to this consumer
channel. basicQos(5, true); // It means that when all consumers of the channel consume 5 messages without ACK, the server will not distribute messages to the channel
How to improve message reliability
Set the mandatory parameter or back up the switch
Set publisher confirm mechanism or transaction mechanism
Set switches, queues, and messages to be persistent
Set the autoack of the consumer to false, and confirm the message after consuming the message
This work adoptsCC agreement, reprint must indicate the author and the link to this article