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.
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
Table of contents
3. SpringAMQP
3. Publish and subscribe model-Fanout
Publish (Publish), Subscribe (Subscribe)
The difference between the publish and subscribe model and the previous case is that it allows the same message to be sent to multiple consumers. The implementation is to add exchange (switch).
Common exchange types include:
Fanout: Broadcast
Direct: Routing
Topic: Topic
Note : Exchange is responsible for message routing, not storage. If routing fails, the message will be lost.
Publish and subscribe - Fanout Exchange
Fanout Exchange will broadcast the received message to each queue bound to it.
Case: Using SpringAMQP to demonstrate the use of FanoutExchange
The implementation idea is as follows:
In the consumer service, use code to declare the queue and switch, and bind the two.
In the consumer service, write two consumer methods to listen to fanout.queue1 and fanout.queue2 respectively.
Write a test method in the publisher. Send a message to itcast.fanout
Step 1: Declare Exchange, Queue, and Binding in the consumer service
SpringAMQP provides APIs for declaring switches, queues, and binding relationships.
In a common class in the consumer service, add the @Configuration annotation and declare FanoutExchange, Queue and binding relationship object Binding. The code is as follows:
package cn.itcast.mq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConfig {
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("itcast.fanout");
}
@Bean
public Queue fanoutQueue1() {
return new Queue("fanout.queue1");
}
@Bean
public Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
}
@Bean
public Queue fanoutQueue2() {
return new Queue("fanout.queue2");
}
@Bean
public Binding bingingQueue1(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
}
}
Step 2: Declare two consumers in the consumer service.
In the SpringRabbitListener class of the consumer service, add two methods to listen to fanout.queue1 and fanout.queue2 respectively:
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String msg) throws InterruptedException {
System.out.println("消费者1接收到Fanout消息:【"+ msg +"】");
}
@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String msg) throws InterruptedException {
System.out.println("消费者2接收到Fanout消息:【"+ msg +"】");
}
Step 3: Send a message to FanoutExchange in the publisher service.
Add a test method in the SpringAmqpTest class of the publisher service:
@Test
public void testFanoutExchange() {
String exchangeName = "itcast.fanout";
String message = "hello, everyone";
rabbitTemplate.convertAndSend(exchangeName, "", message);
}
Summarize:
- What is the role of a switch?
Receive the message sent by the publisher
and route the message to the queue bound to it according to the rules.
The message cannot be cached, the routing fails, and the message is lost.
FanoutExchange will route the message to each bound queue.- What are the beans that declare queues, switches, and binding relationships?
Queue
FanoutExchange
Binding