spring boot:rabbitmq用topic模式发送接收消息(spring boot 2.4.4)
一,安装rabbitmq:
参见:
https://blog.imgtouch.com/index.php/2023/05/27/fedora30-linux-an-zhuang-rabbitmq-3-8-14/
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/27/spring-boot-rabbitmq-yong-topic-mo-shi-fa-song-jie-shou/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息:
1,地址:
https://github.com/liuhongdi/rabbit
2,功能说明:演示了用topic模式订阅消息
3,项目结构:如图:
三,配置文件说明
1,pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2,application.yml
#rabbitmq spring: rabbitmq: host: 127.0.0.1 port: 5672 username: root password: 123456
四,java代码说明
1,config/TopicConfig.java
@Configuration public class TopicConfig { public static final String TOPIC_QUEUE1 = "topic.queue1"; public static final String TOPIC_QUEUE2 = "topic.queue2"; public static final String TOPIC_EXCHANGE = "topic.exchange"; @Bean public Queue topicQueue1() { return new Queue(TOPIC_QUEUE1); } @Bean public Queue topicQueue2() { return new Queue(TOPIC_QUEUE2); } @Bean public TopicExchange topicExchange() { return new TopicExchange(TOPIC_EXCHANGE); } @Bean public Binding topicBinding1() { return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.messge"); } @Bean public Binding topicBinding2() { //#表示0个或多个word return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#"); } }
2,controller/TopicController.java
@RestController @RequestMapping("/topic") public class TopicController { @Autowired private TopicSender topicSender; @GetMapping("/send") public String topicTest() { topicSender.sendTopicQueue(); return "消息已发送"; } }
3,pojo/Goods.java
public class Goods implements Serializable { private static final long serialVersionUID = 6629065135155452917L; private Long goodsId; private String goodsName; private Double goodsPrice; public Long getGoodsId() { return goodsId; } public void setGoodsId(Long goodsId) { this.goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public Double getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(Double goodsPrice) { this.goodsPrice = goodsPrice; } public Goods(Long goodsId, String goodsName, Double goodsPrice) { this.goodsId = goodsId; this.goodsName = goodsName; this.goodsPrice = goodsPrice; } public Goods() { } @Override public String toString() { return "Goods{" + "goodsId=" + goodsId + ", goodsName='" + goodsName + '\'' + ", goodsPrice=" + goodsPrice + '}'; } }
4,receiver/TopicReceiver.java
@Component public class TopicReceiver { @RabbitListener(queues = TopicConfig.TOPIC_QUEUE1) public void receiveTopic1(Goods goods) { System.out.println("receiveTopic1收到消息:" + goods.toString()); } @RabbitListener(queues = TopicConfig.TOPIC_QUEUE2) public void receiveTopic2(Goods goods) { System.out.println("receiveTopic2收到消息:" + goods.toString()); } }
5,sender/TopicSender.java
@Component public class TopicSender { //private static final Logger log = LoggerFactory.getLogger(TopicSender.class); @Autowired private AmqpTemplate amqpTemplate; public void sendTopicQueue() { Goods goods1 = new Goods(1L,"测试商品1",98.6); Goods goods2 = new Goods(2L,"测试商品2",100.0); System.out.println("TopicSender已发送消息"); // 第一个参数:TopicExchange名字 // 第二个参数:Route-Key // 第三个参数:要发送的内容 this.amqpTemplate.convertAndSend(TopicConfig.TOPIC_EXCHANGE, "topic.messge", goods1 ); this.amqpTemplate.convertAndSend(TopicConfig.TOPIC_EXCHANGE, "topic.messge2", goods2); } }
五,测试效果
1,访问:
http://127.0.0.1:8080/topic/send
返回:
消息已发送
查看控制台:
TopicSender已发送消息 receiveTopic2收到消息:Goods{goodsId=1, goodsName='测试商品1', goodsPrice=98.6} receiveTopic2收到消息:Goods{goodsId=2, goodsName='测试商品2', goodsPrice=100.0} receiveTopic1收到消息:Goods{goodsId=1, goodsName='测试商品1', goodsPrice=98.6}
六,查看spring boot版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.4)