Service asynchronous communication-Topic

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

5. Publish and subscribe model-Topic

RabbitMQ is an efficient, reliable, and flexible message queue middleware used to implement data exchange and message processing in distributed systems. In RabbitMQ, Topic is a common message routing pattern that allows producers to send messages to multiple queues, while allowing multiple consumers to receive and process messages from these queues.

1. Overview of RabbitMQ Topic mode
RabbitMQ's Topic mode means that messages are routed to one or more queues according to certain rules, and consumers receive and process messages by subscribing to these queues. In Topic mode, producers send messages to one or more Topics, and each Topic can have one or more consumer subscriptions. When a message is published to a Topic, RabbitMQ will route the message to all consumers subscribed to the Topic.

2. Working principle of RabbitMQ Topic mode

  • message routing

In Topic mode, the producer sends messages to a specific Topic. Consumers receive and process messages by subscribing to the Topic. 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 Topic mode supports persistent storage of messages, that is, messages can be saved on disk to prevent data loss when the system crashes. In addition, Topic mode also supports memory caching of messages to improve reading efficiency.

3. Advantages of RabbitMQ Topic mode

(1) Publish/subscribe mode: Topic mode implements the publish/subscribe message routing method, decoupling producers and consumers, and consumers can dynamically subscribe or unsubscribe from topics, making the system more flexible and scalable get improved.
(2) Multicast: Topic mode can send messages to multiple queues, thereby achieving the effect of multicast, so that multiple consumers can receive and process the same message at the same time.
(3) Load balancing: Since messages can be routed to multiple queues, different consumers can process messages in parallel, thereby improving the system's throughput and load balancing capabilities.
(4) Persistent storage: Topic mode supports persistent storage of messages, which improves the reliability and stability of the system.

3. Disadvantages of RabbitMQ Topic mode

(1) Message duplication: Since Topic can route messages to multiple queues, the same message may be received and processed by multiple consumers, resulting in duplicate processing results.
(2) Subscription management: Topic mode requires consumers to manually subscribe or unsubscribe from topics, which increases the complexity of the system and the difficulty of management.

5. RabbitMQ Topic mode application scenarios

  • Distributed system: In a distributed system, messages usually need to be sent to multiple nodes for processing. Topic mode can route messages to multiple queues to achieve distributed processing effects.
  • Decoupling and flexibility: In some scenarios, producers and consumers need to be decoupled so that consumers can dynamically join or leave the system. The Topic model can meet this demand, and consumers can subscribe or unsubscribe from a Topic at any time.
  • Multi-channel communication: In multi-channel communication scenarios, different channels need to process different types of messages. Topic mode can route different types of messages to different queues to achieve multi-channel communication.
  • Data stream processing: In a data stream processing scenario, the data stream needs to be divided into different parts for processing. Topic mode can route the data stream to different queues to achieve distributed processing of the data stream.

Summarize

RabbitMQ's Topic mode is a flexible message routing mode that implements publish/subscribe message routing and supports features such as multicast, load balancing, and persistent storage. However, the Topic model also has shortcomings such as message duplication and complex subscription management. In practical applications, it is necessary to select an appropriate message routing mode and optimize it according to specific scenarios.

Publish and subscribe-TopicExchange

TopicExchange is similar to DirectExchange, except that routingKey must be a list of multiple words, separated by .
Wildcards can be used when specifying BindingKey in Queue and Exchange:

#: refers to 0 or more words
*: refers to one word

Case: Using SpringAMQP to demonstrate the use of TopicExchange

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 topic.queue1 and topic.queue2 respectively.
Write a test method in the publisher to send messages to itcast.topic.

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

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

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

@RabbitListener(bindings = @QueueBinding(
         value = @Queue(name = "topic.queue3"),
         exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
         key = "china.*"
 ))
public void listenTopicQueue3(String msg){
    
    
    System.out.println("消费者接收到topic.queue3的消息:【" + msg + "】");
}

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

@Test
public void testTopicExchange() {
    
    
    String exchangeName = "itcast.topic";
    String message = "hello, jack";
    rabbitTemplate.convertAndSend(exchangeName, "china.new", message);
}

Insert image description here

Summarize:

Describe the difference between Direct switches and Topic switches?
The message RoutingKey received by the Topic switch must be multiple words. When splitting
the bindingKey between the Topic switch and the queue, you can specify the wildcard character
#: represents 0 or more words
*: represents 1 word

Guess you like

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