SpringBoot整合RabbitMQ之發(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 { //發(fā)布訂閱模式 public static final String FANOUT_QUEUE1 = "fanout.queue1"; public static final String FANOUT_QUEUE2 = "fanout.queue2"; public static final String FANOUT_EXCHANGE = "fanout.exchange"; }
聲明隊(duì)列和交換機(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("fanoutQueue1") public Queue fanoutQueue1(){ return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build(); } @Bean("fanoutQueue2") public Queue fanoutQueue2(){ return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build(); } @Bean("fanoutExchange") public FanoutExchange fanoutExchange(){ return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build(); } @Bean("fanoutQueueBinding1") public Binding fanoutQueueBinding1(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue1") Queue queue){ return BindingBuilder.bind(queue).to(fanoutExchange); } @Bean("fanoutQueueBinding2") public Binding fanoutQueueBinding2(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue2") Queue queue){ return BindingBuilder.bind(queue).to(fanoutExchange); } }
編寫生產(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("/fanout") public String fanout(){ rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"", "hello spring amqp:fanout..."); return "發(fā)送成功"; } }
編寫消費(fèi)者代碼(含兩個(gè)隊(duì)列)
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import rabbitmq.constant.Constants; @Component public class FanoutListener { @RabbitListener(queues = Constants.FANOUT_QUEUE1) public void queueListener1(String message){ System.out.println("隊(duì)列["+Constants.FANOUT_QUEUE1+"] 接收到消息:" +message); } @RabbitListener(queues = Constants.FANOUT_QUEUE2) public void queueListener2(String message){ System.out.println("隊(duì)列["+Constants.FANOUT_QUEUE2+"] 接收到消息:" +message); } }
生產(chǎn)消息
消費(fèi)消息
兩個(gè)隊(duì)列都收到并消費(fèi)了消息,且結(jié)果符合預(yù)期。
到此這篇關(guān)于SpringBoot整合RabbitMQ之發(fā)布訂閱模式的文章就介紹到這了,更多相關(guān)SpringBoot RabbitMQ發(fā)布訂閱模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合rabbitmq的示例代碼
- springboot集成rabbitMQ之對(duì)象傳輸?shù)姆椒?/a>
- Springboot 配置RabbitMQ文檔的方法步驟
- SpringBoot+RabbitMq具體使用的幾種姿勢(shì)
- springboot + rabbitmq 如何實(shí)現(xiàn)消息確認(rèn)機(jī)制(踩坑經(jīng)驗(yàn))
- SpringBoot中RabbitMQ集群的搭建詳解
- SpringBoot中連接多個(gè)RabbitMQ的方法詳解
- 一文掌握Springboot集成RabbitMQ的方法
- SpringBoot實(shí)現(xiàn)RabbitMQ監(jiān)聽消息的四種方式
- SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
相關(guān)文章
MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用
這篇文章主要介紹了MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot之HttpWebServiceMessageSenderBuilder用法詳解
這篇文章主要介紹了SpringBoot之HttpWebServiceMessageSenderBuilder用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04springboot中動(dòng)態(tài)權(quán)限實(shí)時(shí)管理的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)一個(gè)在springboot中動(dòng)態(tài)權(quán)限的實(shí)時(shí)管理,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-10-10Springboot實(shí)現(xiàn)添加本地模塊依賴方式
這篇文章主要介紹了Springboot實(shí)現(xiàn)添加本地模塊依賴方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Post請(qǐng)求參數(shù)是數(shù)組或者List時(shí)的請(qǐng)求處理方式
這篇文章主要介紹了Post請(qǐng)求參數(shù)是數(shù)組或者List時(shí)的請(qǐng)求處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05