preface

Recently, a large number of people must be: job-hopping, salary increase, and some people may be worried about the interview! RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ: RabbitMQ Focus on the public number [procedures yuan xiao Wan] can be obtained.

Introduction of the RabbitMQ

RabbitMQ is an open source implementation of AMQP developed in the Erlang language. AMQP (Advanced Message Queuing Protocol) is an open standard of application-layer protocols designed for message-oriented middleware. Message-oriented middleware is primarily used for decoupling between components so that the sender of a message does not need to be aware of the message consumer and vice versa.

The main characteristics of AMQP are message orientation, queue, routing (including point-to-point and publish/subscribe), reliability, and security.

RabbitMQ is an open source IMPLEMENTATION of AMQP. The server is written in Erlang and supports a variety of clients, such as Python, Ruby,.NET, Java, JMS, C, PHP, ActionScript, XMPP, STOMP, and AJAX. It is used to store and forward messages in distributed systems, and has good performance in ease of use, scalability, and high availability.

When it comes to RabbitMQ, you have to mention the AMQP protocol. AMQP protocol is a binary protocol with modern characteristics. Advanced Message queue protocol is an application layer standard that provides unified messaging services. It is an open standard of application layer protocol designed for message-oriented middleware.

Let’s take a look at some important concepts in the AMQP protocol:

Server

Receive client connections to implement AMQP entity services.

Connection

Connection, the network connection between the application and the Server, TCP connection.

Channel

Channel, message read and write operations in the channel. Clients can establish multiple channels, each representing a session task.

Message

Messages, the data that passes between the application and the server, can be very simple or very complex. There are Properties and Body components. Properties is a wrapper that allows you to modify the message with advanced features such as priority, latency, and so on. The Body is the message Body content.

Virtual Host

Virtual hosts for logical isolation. A virtual host can have multiple Exchanges and queues. A virtual host cannot have exchanges or queues with the same name.

Exchange

An exchange that receives messages and routes them to one or more queues according to routing rules. If the route is not available, it is either returned to the producer or discarded. The switch types used by RabbitMQ are direct, Topic, FANout, and headers

Binding

A binding, a virtual connection between an exchange and a message queue, that can contain one or more Routingkeys.

RoutingKey

RoutingKey. When a producer sends a message to the exchange, it sends a RoutingKey that specifies the routing rules so that the exchange knows which queue to send the message to. The routing key is usually a “. A split character string, for example, com.rabbitmq.

Queue

Message queues, used to hold messages for consumption by consumers.

Why introduce channels when you can just use Connection?

The main purpose of the channel is to solve the problem that many threads in an application need to consume or produce messages from RabbitMQ, so there must be many connections, that is, many TCP connections. However, setting up and destroying TCP connections is very expensive for an operating system, and performance bottlenecks can occur if usage peaks. RabbitMQ uses TCP connection multiplexing to reduce performance overhead and facilitate management.

Protocol model of AMQP:

As you can see in the figure, the AMQP protocol model has three parts: producer, consumer, and server.

The producer is the party that delivers the message. It first connects to the Server, establishes a connection, and opens a channel. The producer then declares the switch and queue, sets the associated properties, and binds the switch and queue using routing keys. Similarly, consumers also need to establish connections, open channels and other operations to receive messages.

The producer can then send the message to the virtual host on the server, where the switch selects routing rules based on the routing key, and then send it to a different message queue, so that consumers who subscribe to the message queue can get the message and consume it.

Finally, close channels and connections.

RabbitMQ is implemented using the AMQP protocol, which is exactly the same as the AMQP protocol shown below.

Common switch

The common switch types for RabbitMQ are Direct, Topic, FANout, and headers.

Direct 

The rules for Direct are simple. Before Posting messages, you need to bind exchange and Queue. If the RoutingKey is the same as the binding key when the message is published. The message is delivered to the queue. If no corresponding queue exists, the message is discarded.

fanout

As long as the exchange is bound to the queue. Published messages go to the queue. Each message sent to a FANout type exchange is sent to all bound queues. The FANout switch does not handle routing keys and simply binds queues to the switch.

topic

Compare to the direct type. Topic is equivalent to fuzzy matching, while direct is congruent. Similar to mysql ‘like’ keyword

Application scenarios

Asynchronous processing.

To improve the performance of system services, some operations that do not need to take effect immediately can be separated and executed asynchronously. Order system to send SMS, for example, in the order of main process (such as deductions inventory, generate the corresponding documents) after sending a message to the MQ for main process is fast, while the other separate thread pull MQ message (or messages) by MQ push, when found that texting of MQ message, such as to perform the corresponding business logic.

Flow peak clipping.

For example, the second kill activity, in a short period of time the traffic increases dramatically, the use of message queue, when the message queue is full, reject the response, jump to the error page, so that the system will not crash due to overload.

Log processing

Apply decoupling.

Suppose A service A needs to send messages to many services (B, C, and D). When A service (such as B) does not need to send messages, service A needs to change the code and deploy again. When A new service (service E) needs A message from service A, it needs to change the code and redeploy. In addition, service A should also consider that other services are down, and how to do if no message is received? Do you want to resend it? Service A only produces messages to SEND to MQ, B, C, and D read messages from MQ, subscribes to messages that need A, and unsubscribes when they don’t. Service A doesn’t have to worry about anything else. This way, the coupling between services or systems can be reduced.

conclusion

My side to sort out a: the RabbitMQ related documents, Spring series of buckets, Java, systematic data (including the Java core knowledge, interview project and 20 years the latest Internet bo, e-books, etc.) a friend in need can be focused on the public number available procedures luca brasi small wan 】 【.