基于SpringBoot自定义Rabbitmq配置来发布订阅消息
程序员文章站
2022-03-07 15:24:54
...
代码库
https://gitee.com/liyghting/springrabbitmqdemo
原理
基于spring启动的时候加载发布订阅配置,发送基于json的消息。
step1:实现Spring接口
step2:生产者配置信息Spring的Bean生成
step3:消费者配置信息初始化和建立监听
具体代码请看RabbitmqConfig.java
发布消息例子
配置application.yml
rabbitmqProducerMap:
testProducer1:
exchangeName: testExchange
routingKey: test1
producerBeanName: test1Producer
testProducer2:
exchangeName: testExchange
routingKey: test2
producerBeanName: test2Producer
代码
ProducerUtil.send("test1Producer", user);
具体请看TestProducer.java
订阅消息例子
配置application.yml
rabbitmqBindingMap:
test1Binding:
queueName: test1Queue
exchangeName: testExchange
routingKey: test1
consumerBeanName: test1Consumer
test2Binding:
queueName: test2Queue
exchangeName: testExchange
routingKey: test2
consumerBeanName: test2Consumer
代码实现BaseConsumer两个方法
@Override
public ParameterizedTypeReference<ReceiveUser> getParameterizedTypeReference() {
return new ParameterizedTypeReference<ReceiveUser>() {
};
}
@Override
public void handleMsg(ReceiveUser msg) {
logger.info("Test1Consumer " + msg);
}
具体请看Test1Consumer.java