RabbitMQ (1) A brief introduction
MQ (Message Queue) message queue
Message queue middleware is an important component in distributed systems
Mainly solve problems such as asynchronous processing, application decoupling, traffic peak clipping, etc.
So as to achieve a high-performance, high-availability, scalable and eventually consistent architecture
Use more message queue products: RabbitMQ, RocketMQ, ActiveMQ, ZeroMQ, Kafka, etc.
Asynchronous processing
After registering, users need to send verification email and mobile phone verification code;
Write the registration information into the database, send the verification email, send the mobile phone, and return to the client after the three steps are completed
Application decoupling
Scenario: The order system needs to notify the inventory system
If the inventory system is abnormal, the order will fail to call the inventory, causing the order to fail
- Reason: the coupling between the order system and the inventory system is too high
Order system: After the user places an order, the order system completes the persistence processing, writes the message into the message queue, returns to the user, and the order is successfully placed;
Inventory system: subscribe to the message of placing an order, obtain order information, and the inventory system will perform inventory operations based on the order information;
If: When placing an order, the inventory system cannot operate normally, and it will not affect the order, because after the order is placed, the order system writes to the message queue and no longer cares about other follow-up operations, realizing the application of the order system and the inventory system Decoupling
So, the message queue is typical: producer consumer model
Producers continue to produce messages to the message queue, and consumers continue to obtain messages from the queue
Because the production and consumption of messages are asynchronous, and they only care about the sending and receiving of messages, there is no intrusion of business logic, so that the decoupling of producers and consumers is realized
Traffic peak
Panic buying, flash sales and other businesses, for high concurrency scenarios
Because the traffic is too large, the surge will cause the application to hang. To solve this problem, add a message queue at the front end
After receiving the user's request, the server first writes it to the message queue. If it exceeds the length of the queue, it will be discarded and a page that ends in a flash!
To put it bluntly, the users who enter the queue are those who succeed in the spike;
Background knowledge introduction
AMQP Advanced Message Queuing Protocol
Namely Advanced Message Queuing Protocol, an application layer standard advanced message queuing protocol that provides unified messaging services
Protocol: The rules that must be followed during data transmission
Clients based on this protocol can communicate with message middleware
Not restricted by conditions such as products and development languages
JMS
Java Message Server, Java Message Service Application Programming Interface, a specification, similar to the role played by JDBC
Is a message-oriented middleware API in the Java platform, used to send messages between two applications or in a distributed system for asynchronous communication
The connection between the two
JMS defines a unified interface, unified message operation; AMQP unified data exchange format through protocol
JMS must be the java language; AMQP is just a protocol and has nothing to do with language
Erlang language
Erlang (['ə:læŋ]) is a general-purpose concurrency-oriented programming language developed by CS-Lab under the jurisdiction of the Swedish telecommunications equipment manufacturer Ericsson. The purpose is to create a programming language that can handle large-scale concurrent activities And operating environment
It was originally designed by Ericsson specifically for communication applications, such as control switches or conversion protocols, so it is very suitable for building distributed, real-time soft parallel computing systems
The Erlang runtime environment is a virtual machine, a bit like a Java virtual machine, so that once the code is compiled, it can also be run anywhere
Why choose RabbitMQ
We started by saying that there are so many message queue products, why choose RabbitMQ?
First look at the naming: Rabbits move very fast and reproduce very crazy, so I used Rabbit as the name of this distributed software (it’s that simple)
Erlang development, the best partner of AMQP, simple installation and deployment, low barriers to use
Enterprise-level message queue, highly reliable after a lot of practical tests, a large number of successful application cases, such as Alibaba, NetEase and other major manufacturers have used
Have a powerful WEB management page
Strong community support provides impetus for technological progress
Support message persistence, support message confirmation mechanism, flexible task distribution mechanism, etc., support functions are very rich
Cluster expansion is easy, and performance can be doubled by adding nodes
Summary: If you want to use a highly reliable, powerful, and easy-to-manage message queuing system, then choose RabbitMQ. If you want to use a high-performance, but occasionally lose data, you can use kafka or zeroMQ.
The performance of kafka and zeroMQ is so explosive that it can definitely overwhelm RabbitMQ!
RabbitMQ component functions
-
Broker : message queue server entity
-
Virtual Host : virtual host
- Identify a batch of exchanges, message queues and related objects, forming a whole
- A virtual host is an independent server domain that shares the same authentication and encryption environment
- Each vhost is essentially a mini version of RabbitMQ server, with its own queue, switch, binding and permission mechanism
- Vhost is the basis of AMQP concept, RabbitMQ default vhost is /, which must be specified when linking
-
Exchange : Exchange (routing)
- Used to receive messages sent by producers and route these messages to the queue in the server
-
Queue : message queue
- Used to save the message until it is sent to the consumer
- It is the container of the message and the end of the message
- A message can be put into one or more queues
- The message has been in the queue, waiting for consumers to connect to this queue to take it away
-
Banding : binding, used for the association between the message queue and the exchange
-
Channel : Channel (channel)
- An independent bidirectional data stream channel in a multiplexed connection
- Channel is established within the real TCP connections virtual link
- AMQP commands are all sent through the channel, whether it is publishing messages, subscribing to queues or receiving messages, they are all done through the channel
- Because it is very expensive for the operating system to establish and destroy TCP connections, the concept of channels is introduced to reuse TCP connections
-
Connection : network connection, such as a TCP connection
-
Publisher : The producer of the message, and also a client application that publishes messages to the exchange
-
Consumer : The consumer of the message, which represents a client application that gets the message from the message queue
-
Message : message
- The message is anonymous, it is composed of message header and message body
- The message body is opaque, while the message header is composed of a series of optional attributes, these attributes include routing-key (routing key), priority (priority), delivery-mode (message may require persistent storage [message Routing mode]) etc.