I haven’t updated my blog for a long time. After I came to Hangzhou, working overtime was common. The company had a project that was supposed to be completed and launched in September, but it was pushed forward to the middle of August. Since August, almost every day at 11 p.m. In fact, a little class to nothing, programmers all know, write code, touch fish, intensity is also good. However, overtime work resulted in less time for study every day. I originally wanted to study for one hour every day, but it seemed to be a little difficult, so my study became intermittent and I went home again last week. I’ve been working on RabbitMQ for a while now, and I’ve been working on it for a while now. In my last blog POST, I shared how PHP handles message queues. Of course, this is just a simple application. Here are some of the things I’ve learned in my time. This article introduces the six modes of rabbitMQ and how they work.

First, concept explanation

I. Channel

Concept: A channel is a channel through which a producer publishes or a consumer subscribes to a queue. A channel is a virtual connection over a TCP connection. What does that mean? This means that RabbitMQ establishes hundreds or thousands of channels over a single TCP for multi-thread processing. The TCP is shared by multiple threads, each of which has a unique channel ID on rabbit to ensure that the channel is private and used by a unique thread.

Question: Why not establish multiple TCP connections? The reason for this is that Rabbit guarantees performance, and it is very expensive for the system to open one TCP per thread, and hundreds of thousands of TCP creation and destruction per second can be very costly to the system. So RabbitMQ chooses to establish multiple channels (over TCP virtual connections) to Rabbit. Similar concepts: TCP is a cable, and a channel is an optical fiber inside it. Each optical fiber is independent and does not affect each other.Copy the code

Exchange routing key (this is related to the following working mode)

Exchange functions as a router. The server uses the routing key to route messages from the exchange to a queue.

There are several types of Exchange, including Direct, Fanout, and Topic. (Direct) 1:1, (fanout) 1: N, (topic) N:1

Direct: 1:1 Similar to complete match

Fanout: 1: N Can publish a message to multiple queues in parallel. Simply put, when multiple queues are bound to a FANout switch, the switch copies multiple messages to each of the bound queues, and each queue has a copy of the message.

Ps: This can realize the parallel processing of multiple tasks in our business, for example, users to upload pictures function, when the message arrives at the switch, it can be routed to the integral at the same time increase the queue and other queue, achieve the purpose of parallel processing, and easy to extend, hereafter have what parallel tasks, tied directly to the fanout exchange does not demand change before the code.Copy the code

Topic N:1, multiple switches can route messages to the same queue. According to fuzzy matching, for example, a queue whose routing key is *. Test will be routed to any message that reaches the exchange.

3. Queue

1, push mode: through AMQP basic.consume command subscription, messages will be automatically received, high throughput

2, pull mode: through the BSAI.get command of AMQP

Note: When a queue has multiple consumers, messages received by the queue are sent to consumers in a circular fashion. Each message is sent to only one subscriber consumer

Duration (duration)

To enable the persistence function, ensure that the message delivery mode is set to persistence, the exchange is set to persistence, and the queue is set to persistence

V. Confirmation Mechanism (ACK)

1. Sender acknowledgement mode: message sent to exchange – sent to completion – > Message posted to queue or persisted to disk asynchronous callback notifies producer

2, consumer confirmation mechanism: message delivery consumer – ACK – delete this message – delivery next

Note: Until an ACK is received, the message will not be sent to the consumer again, but the next message will be sent to other consumers

Ii. Working mode

I. Simple mode (namely, the simplest sending and receiving mode)

2. The message of consumers (consumer) to monitor the message queue, if there is a message queue, is consumed, after the news was taken, automatically deleted from the queue (hidden message may not have been properly handle consumer, has disappeared from the queue, causing the loss of the news, here can be set to manual ack, but if set to manual ack, After processing, send AN ACK message to the queue in a timely manner; otherwise, memory will overflow.

Ii. Work Mode (Competition for resources)

There can be multiple consumers. Consumer 1 and consumer 2 listen on the same queue at the same time, and the message is consumed. C1 and C2 compete for the contents of the current message queue, and the one who gets the message first is responsible for consuming the message. (Hidden danger: In the case of high concurrency, a message will be used by multiple consumers by default. You can set a syncronize to ensure that one message can be used by only one consumer.)

Publish /subscribe

1. Each consumer listens to his own queue;

2. The producer sends the message to the broker, which forwards the message to each queue bound to the switch. Each queue bound to the switch receives the message.

4. Routing Mode

The message producer sends the message to the switch according to the route. The route is a string (INFO). The message that is currently generated carries a routing character (object method).

2. Define the route string based on the service function

3. Obtain the corresponding function string from the system’s code logic and throw the message task to the corresponding queue.

4. Service scenario: Error notification; EXCEPTION; Error notification functionality; Error notification in the traditional sense; Customer notification; By using key routing, errors in the program can be encapsulated as messages to the message queue. Developers can customize consumers to receive errors in real time.

Topic Mode (one of the routing modes)

1. Asterisks (asterisks) represent wildcards

2. Asterisks stand for multiple words and pound signs stand for one word

3. Add fuzzy matching for routing

4. The message producer generates the message and delivers the message to the switch

5. The switch fuzzy matches the key to the corresponding queue, and the listening consumer of the queue receives the message consumption

(In my understanding, it is a fuzzy matching of routing query, similar to the fuzzy query of SQL)

6. The RPC model

I don’t quite understand it yet, next update…