Service asynchronous communication-Direct

Please add image description
Personal business card:

Blogger: Drunkardᝰ.
Personal profile: Indulge in wine, and use the energy of wine to fight for a future.
This article is inspirational: When three of us travel together, we must be my teacher.

Please add image description
This project is based on Java "SpringCloud Microservice Technology Stack" by the dark horse programmer of station B , SpringCloud+RabbitMQ+Docker+Redis+search+distributed

[SpringCloud+RabbitMQ+Docker+Redis+search+distributed, detailed system explanation springcloud microservices technology stack course | Dark horse programmer Java microservices] Click to watch

3. SpringAMQP

4. Publish and subscribe model-Direct

RabbitMQ is an efficient, reliable, and flexible message queue middleware used to implement data exchange and message processing in distributed systems. In RabbitMQ, Direct mode is a common message routing mode. It implements point-to-point delivery of messages and ensures that each message is received and processed by only one consumer.

1. Overview of RabbitMQ Direct mode
RabbitMQ's Direct mode means that messages are routed according to the one-to-one principle. Each message has only one target queue, and only one consumer can receive the message. In Direct mode, producers send messages to specific queues, and RabbitMQ routes the messages to one or more consumers as needed. This mode is suitable for scenarios where orderly and reliable processing of each message is required.

2. Working principle of RabbitMQ Direct mode

  • message routing

In Direct mode, RabbitMQ will route messages to a specific queue based on the attributes of the message. Consumers receive and process messages by subscribing to this queue. When a consumer successfully processes a message, RabbitMQ deletes the message from the queue so that it cannot be received by other consumers.

  • Message persistence

RabbitMQ's Direct mode supports persistent storage of messages, that is, messages can be saved on disk to prevent data loss when the system crashes. In addition, Direct mode also supports memory caching of messages to improve reading efficiency.

3. Advantages of RabbitMQ Direct mode

(1) One-to-one message delivery: Direct mode implements one-to-one message delivery, avoiding competition and repeated processing among multiple consumers.
(2) High reliability: Since each message has only one target consumer, it can be ensured that each message is processed correctly by only one consumer, which improves the reliability of the system.
(3) Sequentiality: Direct mode ensures that messages are received and processed by consumers one by one in the order they are sent, and is suitable for situations where orderly processing is required.
(4) Persistent storage: Direct mode supports persistent storage of messages, which improves the reliability and stability of the system.

4. Disadvantages of RabbitMQ Direct mode

(1) Load balancing: Since each message is only received and processed by one consumer, load balancing of messages cannot be achieved, which limits the throughput of the system.
(2) Limit on the number of consumers: Each queue only allows one consumer to receive messages, which limits the number of consumers that can be processed at the same time.

5. RabbitMQ Direct mode application scenarios

  • Task queue: In a task queue scenario, a producer publishes tasks to a specific queue, and then multiple consumers process the tasks in the queue in parallel. Since each task is processed by only one consumer, the reliability and integrity of the task can be guaranteed.
  • Point-to-point communication: In point-to-point communication scenarios, RabbitMQ's Direct mode can achieve reliable delivery and orderly processing of messages. For example, in chat applications, Direct mode can ensure that each message can only be received by one user, avoiding duplication and competition of messages.
  • Transaction processing: In transaction processing scenarios, it is necessary to ensure the atomicity and consistency of each transaction. Direct mode can realize one-to-one message passing and ensure that each transaction is processed by only one node, thus improving the consistency and reliability of transaction processing.
  • Order processing: In order processing scenarios, each order needs to be processed individually, and the order and reliability of processing must be guaranteed. Direct mode can realize one-to-one delivery of order messages, ensuring that each order is correctly processed by only one processor.

Summarize

RabbitMQ's Direct mode is a simple and reliable message routing mode, suitable for scenarios that require orderly and reliable processing of each message. It implements one-to-one delivery of messages and ensures that each message is correctly processed by only one consumer. However, Direct mode also has shortcomings such as load balancing and limit on the number of consumers. In applications, selection and optimization need to be based on actual needs.

Publish and subscribe-DirectExchange

Direct Exchange will route the received message to the specified Queue according to the rules, so it is called routing mode (routes).

Each Queue has a BindingKey
publisher set up with Exchange. When sending a message, the RoutingKey of the message is specified.
Exchange will route the message to the queue whose BindingKey is consistent with the message RoutingKey.

Case: Using SpringAMQP to demonstrate the use of DirectExchange

The implementation idea is as follows:
Use @RabbitListener to declare Exchange, Queue, and RoutingKey
in the consumer service, write two consumer methods to listen to direct.queue1 and direct.queue2 respectively,
write a test method in the publisher, and send messages to itcast.direct

Step 1: Declare Exchange and Queue
in the consumer service. In the consumer service, write two consumer methods to listen to direct.queue1 and direct.queue2 respectively,
and use @RabbitListener to declare Exchange, Queue and RoutingKey.

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.queue1"),
        exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
        key = {
    
    "red", "blue"}
))
public void listenDirectQueue1(String msg){
    
    
    System.out.println("消费者接收到direct.queue1的消息:【" + msg + "】");
}

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.queue2"),
        exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
        key = {
    
    "red", "yello"}
))
public void listenDirectQueue2(String msg){
    
    
    System.out.println("消费者接收到direct.queue2的消息:【" + msg + "】");
}

Step 2: Send a message to DirectExchange in the publisher service.
Add a test method in the SpringAmqpTest class of the publisher service:

@Test
public void testDirectExchange() {
    
    
    String exchangeName = "itcast.direct";
    String message = "hello, direct";
    rabbitTemplate.convertAndSend(exchangeName, "yello", message);
}

Insert image description here

Summarize:

  1. Describe the difference between Direct switches and Fanout switches?
    The Fanout switch routes the message to each queue bound to it.
    The Direct switch determines which queue to route to based on the RoutingKey.
    If multiple queues have the same RoutingKey, the function is similar to Fanout.
  2. What are the common annotations for declaring queues and switches based on the @RabbitListener annotation?
    @Queue
    @Exchange

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/133106385