1.生產者工程
-
pom.xml里引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> -
application.yml里配置基本資訊
spring: rabbitmq: host: localhost port: 5672 username: ****** password: ****** virtual-host: /test -
在配置類里創建交換機,佇列,系結交換機和佇列
package com.min.config; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { // 創建交換機 @Bean("topicExchange") public Exchange exchange() { return ExchangeBuilder.topicExchange("springboot-topic-exchange").durable(true).build(); } // 創建佇列 @Bean("queue1") public Queue queue1() { return QueueBuilder.durable("springboot-queue1").build(); } @Bean("queue2") public Queue queue2() { return QueueBuilder.durable("springboot-queue2").build(); } // 系結佇列和交換機 @Bean public Binding BindQueue1Exchange(@Qualifier("queue1") Queue queue, @Qualifier("topicExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("#.error").noargs(); } @Bean public Binding BindQueue2Exchange(@Qualifier("queue2") Queue queue, @Qualifier("topicExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("order.*").noargs(); } } -
注入RabbitTemplate發訊息
package com.min; import org.junit.jupiter.api.Test; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringbootRabbitmqProducerApplicationTests { @Autowired private RabbitTemplate rabbitTemplate; @Test void testSend1() { rabbitTemplate.convertAndSend("springboot-topic-exchange","boot.error","boot.error的key發送的訊息"); } @Test void testSend2() { rabbitTemplate.convertAndSend("springboot-topic-exchange","order.error","order.error的key發送的訊息"); } @Test void testSend3() { rabbitTemplate.convertAndSend("springboot-topic-exchange","order.insert","order.insert的key發送的訊息"); } }
2.消費者工程
-
引入依賴并且配置基本資訊,和生產者一樣
-
創建兩個監聽器,分別監聽兩個佇列
package com.min.listener; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class RabbitMQListener1 implements MessageListener { @Override @RabbitListener(queues = "springboot-queue1") public void onMessage(Message message) { System.out.println("springboot-queue1接收到訊息:" + new String(message.getBody())); } }package com.min.listener; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class RabbitMQListener2 implements MessageListener { @Override @RabbitListener(queues = "springboot-queue2") public void onMessage(Message message) { System.out.println("springboot-queue2接收到訊息:" + new String(message.getBody())); } } -
啟動消費者工程,然后分別運行生產者工程里的三個測驗方法,結果如下
springboot-queue1接收到訊息:boot.error的key發送的訊息springboot-queue1接收到訊息:order.error的key發送的訊息 springboot-queue2接收到訊息:order.error的key發送的訊息springboot-queue2接收到訊息:order.insert的key發送的訊息從以上結果可以看出,routingkey為boot.error的訊息只能被springboot-queue1接收到,routingkey為order.insert的訊息只能被springboot-queue2接收到,而routingkey為order.error的訊息兩個佇列都能接收到,(注意:本案例使用的是rabbitmq的topic作業模式)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/556836.html
標籤:其他
上一篇:【7月最新實作】使用Python獲取全網招聘資料,實作可視化分析
下一篇:返回列表
