SpringBoot整合RabbitMQ實現(xiàn)通配符模式
更新時間:2025年06月12日 09:26:01 作者:新綠MEHO
本文主要介紹了SpringBoot整合RabbitMQ實現(xiàn)通配符模式,包括依賴添加、配置、隊列與交換機(jī)聲明及綁定,生產(chǎn)者發(fā)送消息,兩個消費者分別接收并處理,驗證消息正確分發(fā)至不同隊列,感興趣的可以了解一下
通配符模式
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency>
添加配置
spring: application: name: rabbitmq-springboot rabbitmq: addresses: amqp://study:study@47.98.109.138:5672/aaa
常量類
public class Constants { //通配符模式 public static final String TOPIC_QUEUE1 = "topic.queue1"; public static final String TOPIC_QUEUE2 = "topic.queue2"; public static final String TOPIC_EXCHANGE = "topic.exchange"; }
聲明隊列和交換機(jī)并綁定二者關(guān)系
import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import rabbitmq.constant.Constants; @Configuration public class RabbitMQConfig { //通配符模式 @Bean("topicQueue1") public Queue topicQueue1(){ return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build(); } @Bean("topicQueue2") public Queue topicQueue2(){ return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build(); } @Bean("topicExchange") public TopicExchange topicExchange(){ return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build(); } @Bean("topicQueueBinding1") public Binding topicQueueBinding1(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue1") Queue queue){ return BindingBuilder.bind(queue).to(topicExchange).with("*.orange.*"); } @Bean("topicQueueBinding2") public Binding topicQueueBinding2(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue2") Queue queue){ return BindingBuilder.bind(queue).to(topicExchange).with("*.*.rabbit"); } @Bean("topicQueueBinding3") public Binding topicQueueBinding3(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue2") Queue queue){ return BindingBuilder.bind(queue).to(topicExchange).with("lazy.#"); } }
編寫生產(chǎn)者代碼
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import rabbitmq.constant.Constants; @RequestMapping("/producer") @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @RequestMapping("/topic/{routingKey}") public String topic(@PathVariable("routingKey") String routingKey){ rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring amqp:topic, my routing key is "+routingKey); return "發(fā)送成功"; } }
編寫消費者代碼(含兩個隊列)
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import rabbitmq.constant.Constants; @Component public class TopicListener { @RabbitListener(queues = Constants.TOPIC_QUEUE1) public void queueListener1(String message){ System.out.println("隊列["+Constants.TOPIC_QUEUE1+"] 接收到消息:" +message); } @RabbitListener(queues = Constants.TOPIC_QUEUE2) public void queueListener2(String message){ System.out.println("隊列["+Constants.TOPIC_QUEUE2+"] 接收到消息:" +message); } }
生產(chǎn)消息
消費消息
兩個隊列都收到并消費了消息,且結(jié)果符合預(yù)期。
到此這篇關(guān)于SpringBoot整合RabbitMQ實現(xiàn)通配符模式的文章就介紹到這了,更多相關(guān)SpringBoot RabbitMQ通配符模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- springboot整合rabbitmq的示例代碼
- springboot集成rabbitMQ之對象傳輸?shù)姆椒?/a>
- springboot實現(xiàn)rabbitmq的隊列初始化和綁定
- Springboot 配置RabbitMQ文檔的方法步驟
- SpringBoot集成RabbitMQ的方法(死信隊列)
- SpringBoot+RabbitMq具體使用的幾種姿勢
- SpringBoot中RabbitMQ集群的搭建詳解
- SpringBoot中連接多個RabbitMQ的方法詳解
- 一文掌握Springboot集成RabbitMQ的方法
- SpringBoot實現(xiàn)RabbitMQ監(jiān)聽消息的四種方式
- SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
相關(guān)文章
Java POI讀取excel中數(shù)值精度損失問題解決
這篇文章主要介紹了Java POI讀取excel中數(shù)值精度損失問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/i
這篇文章主要介紹了IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Springboot輕量級的監(jiān)控組件SpringbootAdmin
這篇文章主要為大家介紹了Springboot輕量級的監(jiān)控組件SpringbootAdmin使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02