Concepts related to MQ
What is the MQ
MQ(Message Queue), literally, is essentially a queue, FIFO first in first out, but the contents of the queue are messages, or a cross-process communication mechanism for upstream and downstream messages ==. In the Internet architecture, MQ is a very common upstream and downstream “logical decoupling + physical decoupling” message communication service. With MQ, message sending upstream only needs to rely on MQ and no other services.
Why use MQ
Traffic peak clipping
For example, if the order system can handle up to 10,000 orders, this is more than enough capacity to handle normal times when we place orders and return results a single second later. However, at peak times, if there are 20,000 orders, the operating system can not handle it, so it can only limit the number of orders beyond 10,000 and not allow users to place orders. By using message queue as buffer, we can cancel this limitation and divide the orders placed within one second into a period of time to process. At this time, some users may receive the successful operation after placing an order more than ten seconds, but the experience is better than that of not placing an order.
The application of decoupling
Take e-commerce application as an example, the application includes order system, inventory system, logistics system and payment system. After the user creates an order, if the coupling calls the inventory system, logistics system and payment system, the failure of any subsystem will cause the abnormal ordering operation. When switching to a message queue-based approach, intersystem calls are much less problematic, such as when a logistics system fails and takes a few minutes to fix. In these minutes, the memory to be processed by the logistics system is cached in the message queue, and the user’s order operation can be completed normally. After the logistics system recovers, you can continue to process the order information, so that the single user does not feel the failure of the logistics system, improving the availability of the system.
Asynchronous processing
Some calls between services are asynchronous. For example, WHEN A calls B, B takes A long time to execute, but A needs to know when B can complete the execution. In the past, there were two ways: A calls B’s query API query after A period of time. Or A provides A callback API that B calls to notify A when it’s done. Both methods are not very elegant. Using the message bus can solve this problem easily. When A calls service B, it only needs to listen for the message that service B has processed. When service B has processed the message, it sends A message to MQ, which forwards the message to service A. In this way, service A does not have to loop through B’s query API or provide callback API. The B service does not have to do this either. The A service also receives A timely message about the success of the asynchronous processing.
The classification of MQ
- ActiveMQ
- Kafka
- RocketMQ
- RabbitMQ
Introduction of the RabbitMQ
RabbitMQ core
- Simple mode;
- Work queue mode;
- Publish and subscribe model;
- Routing mode;
- Theme mode;
- Release-confirm mode;
How RabbitMQ works
- Broker: An application that receives and distributes messages. RabbitMQ Server is the Message Broker.
- Connection: Publisher/consumer and broker ==TCP Connection ==;
- Channel: If a Connection is established for each RabbitMQ access, it is expensive and inefficient to establish a TCPConnection when messages are heavy. ==Channel is a logical connection established within a connection. If the application supports multiple threads, it is common for each thread to create a separate Channel for communication. The AMQP Method contains a channel ID to help the client and Message Broker identify channels, so they are completely isolated from each other. ** Channels as lightweight connections greatly reduce the overhead of establishing TCP connections for the operating system **
- Exchange: The first destination of the message to the broker, which matches the routing key in the query table to the queue according to the distribution rules. Common types are: Direct (point-to-point), topic (publish-subscribe), and fanout(multicast).
- Queue: This is where the message is finally sent to be picked up by the consumer;
- Binding: a virtual link between an exchange and a queue. Binding can contain routing keys. Binding information is stored in the exchange query table and used for message distribution.
The installation of the RabbitMQ
Prepare the Erlang and RabbitMQ installation packages
Installation steps
RPM -ivh Erlang -21.3-1.el7.x86_64. RPM yum install socat -y RPM -ivh rabbitmq-server-3.8.8-1.el7.noarch. RPMCopy the code
Common commands
- Run the following command to automatically start the MQ service upon system startup: chkconfig rabbitmq-server on;
- To start the Rabbitmq service, run the /sbin/service rabbitmq-server start command.
- To check the service status, run the /sbin/service rabbitmq-server status command.
- Command to stop the rabbitmq-server service: /sbin/service rabbitmq-server stop;
- Rabbitmq-plugins enable rabbitmq_management;
Access the address with the default account password (guest)http://192.168.37.139:15672 permissions problems;
- Add a new user command
- Rabbitmqctl add_user admin 123;
- Set user roles: rabbitmqctl set_user_tags admin administrator;
- Set user permissions: rabbitmqctl set_permissions -p “/” admin “.“”.“”. *”;
- Rabbitmqctl list_users;
Log in again.
- Reset command:
- To disable the app, run the rabbitmqctl stop_app command.
- Clear command: rabbitmqctl reset;
- Restart command: rabbitmqctl start_app;