springBoot集成rabbitmq 之主题模式(topics)
springBoot整合rabbitmq的例子: https://blog.csdn.net/weixin_45730866/article/details/128971917,建议先看。
consumer服务
配置类
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitTopicConfig {
/**
* Queue 可以有4个参数
* 1.name 队列名
* 2.durable 持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
* 3.auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
* 4.exclusive 表示该消息队列是否只在当前connection生效,默认是false
*/
@Bean
public Queue createTopicQueueA() {
return new Queue("QueueTopicA",true);
}
@Bean
public Queue createTopicQueueB() {
return new Queue("QueueTopicB",true);
}
@Bean
public TopicExchange topicExchange() {
//配置广播路由器
return new TopicExchange("ExchangeTopic");
}
//*(星号):可以(只能)匹配一个单词
//#(井号):可以匹配多个单词(或者零个)
@Bean
public Binding bingQueueAToTopicExchange() {
return BindingBuilder.bind(createTopicQueueA()).to(topicExchange()).with("topic.*");
}
@Bean
public Binding bingQueueBToTopicExchange() {
return BindingBuilder.bind(createTopicQueueB()).to(topicExchange()).with("topic.#");
}
}
接收者A B
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@RabbitListener(queues = "QueueTopicA")
public class TopicReceiverA {
@RabbitHandler
public void process(String message) {
System.out.println("接受者A : " + message +","+ new Date());
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@RabbitListener(queues = "QueueTopicB")
public class TopicReceiverB {
@RabbitHandler
public void process(String message) {
System.out.println("接受者B : " + message +","+ new Date());
}
}
producer服务
发送者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class TopicSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(int i) {
String contentA = "主题交换机 A="+ i+"," + new Date() ;
//消息发送,使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
//但不指定routingKey。因为FanoutExchange类型的交换机,routingKey不起作用,它向所有的队列发送广播,只要队列绑定到该交换机即接受消息。
rabbitTemplate.convertAndSend("Exchange_Topic","topic.n",contentA);
System.out.println("发送成功,"+new Date()+","+contentA);
String contentB = "主题交换机 B="+ i+"," + new Date() ;
//消息发送,使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
//但不指定routingKey。因为FanoutExchange类型的交换机,routingKey不起作用,它向所有的队列发送广播,只要队列绑定到该交换机即接受消息。
rabbitTemplate.convertAndSend("Exchange_Topic","topic.n.n",contentB);
System.out.println("发送成功,"+new Date()+","+contentB);
}
测试
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class TopicTestController {
@Autowired
private TopicSender topicSender;
@GetMapping("topic")
public String topic() {
topicSender.send(9999);
return "topicOk";
}
}
请求http://127.0.0.1:8080/topic,
producer控制台输出
发送成功,Mon Feb 13 11:46:20 CST 2023,主题交换机 A=9999,Mon Feb 13 11:46:20 CST 2023
发送成功,Mon Feb 13 11:46:20 CST 2023,主题交换机 B=9999,Mon Feb 13 11:46:20 CST 2023
consumer控制台输出
接受者B : 主题交换机 A=9999,Mon Feb 13 11:46:20 CST 2023,Mon Feb 13 11:46:20 CST 2023
接受者A : 主题交换机 A=9999,Mon Feb 13 11:46:20 CST 2023,Mon Feb 13 11:46:20 CST 2023
接受者B : 主题交换机 B=9999,Mon Feb 13 11:46:20 CST 2023,Mon Feb 13 11:46:20 CST 2023
生产者共计生产了 2 个消息,其它一条消息被消费者 A、 B 各消费了一次,另一条消息被消费者 B 消费了一次。