Rabbit three two things

Introduction to RabbitMQ

1. What does RabbitMQ do?

Messaging systems allow software and applications to interconnect and extend. These applications can be linked together to form a larger application or connect user devices to data. Message systems decouple the sending and receiving of messages to realize asynchronous decoupling of applications.

Maybe you’re considering data delivery, non-blocking operations, or push notifications. Maybe you want to implement publish/subscribe, asynchronous processing, or work queues. All of this can be done through a messaging system.

RabbitMQ is a message broker – a medium for a messaging system. It can provide a common platform for your application to send and receive messages and keep messages secure during transmission.

2. What are the features of RabbitMQ?
2.1 reliability

RabbitMQ provides a variety of techniques that allow you to make trade-offs between performance and reliability. These technologies include persistence mechanisms, post validation, publisher validation, and high availability mechanisms.

2.2 Flexible Routing

Messages are routed through the switch before reaching the queue. RabbitMQ provides a variety of built-in switch types for typical routing logic. If you have more complex routing requirements you can combine these switches and even implement your own switch type and use it as a RabbitMQ plug-in.

2.2.1 Switch Types
Direct, Fanout, topic, header (non-routing key matching, similar to Direct, rarely used).
(Direct) 1:1, (fanout) 1: N, (topic) N:1
2.2.2 Switch and Routing triad
1. fanout
Exchange routing rule of type FANout It routes all messages sent to the Exchange to all queues bound to it (no routing required) of type 1:N.
2. direct
A direct Exchange routing rule that routes messages to queues whose binding keys match their routing keys (requiring a route to match the corresponding Queue) is of type 1:1.
3. topic
Topic Exchange routing rules which query message routes in reverse similar to fuzzy query
1. Generally, routing keys are separated by “.”, for example, A1.B2.c3
2. Exchange routing rules Can be used topic* || #As unknown condition of fuzzy query,*Used to match a word#Used to match multiple words.
3. For example:
3.1 the premise:
C3 Queue1 Queue1, a2.b2. C2 Queue2 Queue2, a3.b3.C3 Queue3 Queue3
There are three queues.
3.2 the results to:
According to the*.B2.*Queue1 and Queue2 are the queues to be sent for the query
According to the A1.*.*Queue1 and Queue3 are the queues to be sent to
According to the#.B2.#Queue1 and Queue2 are the queues to be sent for the query
According to the#The queue to be sent for the.b3.c3 query is Queue3
4. headers
A HEADERS Exchange does not rely on a routing key and binding key matching rule to route a message. Instead, it matches the HEADERS attribute in the content of the sent message.

Specify a set of key-value pairs when binding Queue and Exchange. When a message is sent to Exchange, RabbitMQ takes the headers (also a key pair) of the message and compares whether the key pair matches exactly the key pair specified when the Queue was bound to Exchange. The message is routed to the Queue if it is a perfect match, otherwise it is not.

2.3 the cluster

Multiple RabbitMQ servers on the same LAN can be aggregated and used as a single logical proxy.

2.4 the joint

For servers, it requires more loose and unreliable links than clustering does. RabbitMQ provides a federation model for this purpose.

2.5 Highly available queues

Queues can be mirrored across multiple machines in the same cluster to ensure that your messages remain secure if some of the hardware fails.

More than 2.6 agreement

RabbitMQ supports messaging over a variety of messaging protocols.

2.7 Extensive clients

Almost every programming language you can think of has a RabbitMQ client for it.

2.8 Visual Management Tools

RabbitMQ comes with an easy-to-use visual management tool that helps you monitor every step of the message broker process.

2.9 track

RabbitMQ also provides tracing support if your messaging system has abnormal behavior, so you can find the problem.

2.10 Plug-in system

RabbitMQ comes with various plug-ins to extend itself. You can even write your own plugin to use.

referenceRabbitMQ In Chinese

2. Combine channels, switches, and routing keys to queues

1. Channels are the essence of Rabbit communication, through which both producers and consumers produce and consume messages.
2. A switch is essentially a routing query table (name and queue ID, similar to a hash table), which is a virtual thing and does not have a real switch.
3. Message life cycle: the producer produces message A and sends it to the channel. The channel puts the label (routing key) of the message (message by carrier and label) into the switch and sends it to the queue (in fact, it is query matching.
4. Channel is the most important interface we use with RabbitMQ. Most of our business operations are done in Channel, including defining queues, defining exchanges, binding queues to exchanges, and publishing messages.

Install RabbitMQ

3.1.1 Windows Installation:
  • Install Erlang, download the addressClick on the
  • Install RabbitMQ, download the addressClick on the
  • Enter CMD in the address bar and press Enter to start the command line, then enter the following command to start the management function.
rabbitmq-plugins enable rabbitmq_management
Copy the code
3.1.2 Linux Installation:
  • Download rabbitMQ 3.7.15 Docker image.
Docker pull the rabbitmq: 3.7.15Copy the code
  • Use the Docker command to start the service.
Docker run -p 5672:5672 -p 15672:15672 --name rabbitmq \ -d rabbitmq:3.7.15Copy the code
  • Enter the container and enable the management function.
docker exec -it rabbitmq /bin/bash
rabbitmq-plugins enable rabbitmq_management
Copy the code
  • Open the firewall to facilitate access from the Internet (Ali or Tencent servers can set the white list on the console).
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --reload
Copy the code
3.1.3 Debian/Ubuntu Installation:
  • Install RabbitMQ(Debian/Ubuntu) ServerClick on the
  • For details, seeRabbitMQ (Installing on Debian/Ubuntu)

RabbitMQ solves backlog problems

The use of RabbitMQ in daily work can occasionally cause message backlogs due to unexpected circumstances. There are several types of message backlogs:
  1. The overall problem is that the business logic has not designed a balance between consumers and producers. Business processes or logic need to be changed to ensure that consumption keeps pace with the rate of production of news, such as increasing the number of consumers.

  2. Consumers are abnormal and cannot receive new messages all the time. This problem requires checking whether there is a problem with the logic of consumption and optimizing the program.

Solution:
  • Split MQ, producer ONE MQ, consumer one MQ, write a program to listen to the producer’s MQ simulation consumption rate (such as thread sleep), and then send to the consumer’S MQ. If messages are backlogged, only the producer’s MQ backlogged messages need to be processed without affecting the consumer MQ.
  • Split MQ, producers an MQ, consumers an MQ, write a program to monitor the producer of MQ, a global and static variables recorded the last time consumption, if the last time and the current time only less than the processing time of consumers, is sent to a queue delay (you can use the dead-letter queue implementation) sent to the consumer of MQ, If messages are backlogged, only producer MQ’s backlogged messages need to be processed, leaving consumer MQ unaffected.
  • Using Redis’s List or ZSET for receiving message caching, write a program to periodically fetch messages from Redis and send them to MQ based on consumer processing time.
  • Set the expiration time of the message, put it into a dead-letter queue after expiration, and write a program to process the dead-letter message (re-queue or defer processing even if processed or logged to the database).
The delay queue is relatively simple and the service logic does not change much. RabbitMQ can be implemented using dead letter and rabbitmq_delayed_message_exchange. The plugin can be found on the official website:www.rabbitmq.com/community-p…