日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【消息中间件】Spring Boot整合RabbitMQ

發(fā)布時(shí)間:2025/5/22 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【消息中间件】Spring Boot整合RabbitMQ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

在Spring項(xiàng)目中,可以使用Spring-Rabbit去操作RabbitMQ

尤其是在spring boot項(xiàng)目中只需要引入對(duì)應(yīng)的amqp啟動(dòng)器依賴即可,方便的使用RabbitTemplate發(fā)送消息,使用注解接收消息。

一般在開發(fā)過程中:

生產(chǎn)者工程:

  • application.yml文件配置RabbitMQ相關(guān)信息;
  • 在生產(chǎn)者工程中編寫配置類,用于創(chuàng)建交換機(jī)和隊(duì)列,并進(jìn)行綁定
  • 注入RabbitTemplate對(duì)象,通過RabbitTemplate對(duì)象發(fā)送消息到交換機(jī)
  • 消費(fèi)者工程:

  • application.yml文件配置RabbitMQ相關(guān)信息
  • 創(chuàng)建消息處理類,用于接收隊(duì)列中的消息并進(jìn)行處理
  • 搭建生產(chǎn)者工程

    添加依賴

    修改pom.xml文件內(nèi)容為如下:

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><groupId>cs.wanyuan</groupId><artifactId>springboot-rabbitmq-producer</artifactId><version>1.0-SNAPSHOT</version><dependencies><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></dependency></dependencies></project>
    啟動(dòng)類
    @SpringBootApplicationpublic class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class);}}
    配置RabbitMQ

    1)配置文件

    創(chuàng)建application.yml,內(nèi)容如下:

    spring:rabbitmq:host: localhostport: 5672virtual-host: /wanyuanusername: wanyuanpassword: wanyuan

    2)綁定交換機(jī)和隊(duì)列

    創(chuàng)建RabbitMQ隊(duì)列與交換機(jī)綁定的配置類RabbitMQConfig

    @Configurationpublic class RabbitMQConfig {//交換機(jī)名稱public static final String TOPIC_EXCHANGE = "topic_exchange";//隊(duì)列名稱public static final String QUEUE = "queue";//聲明交換機(jī)@Bean("topicExchange")public Exchange topicExchange(){return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();}//聲明隊(duì)列@Bean("queue")public Queue queue(){return QueueBuilder.durable(QUEUE).build();}//綁定隊(duì)列和交換機(jī)@Beanpublic Binding itemQueueExchange(@Qualifier("queue") Queue queue,@Qualifier("topicExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with("wanyuan.#").noargs();}}

    搭建消費(fèi)者工程

    添加依賴

    修改pom.xml文件內(nèi)容為如下:

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><groupId>cs.wanyuan</groupId><artifactId>springboot-rabbitmq-consumer</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies></project>
    啟動(dòng)類
    @SpringBootApplicationpublic class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class);}}
    配置RabbitMQ

    創(chuàng)建application.yml,內(nèi)容如下:

    spring:rabbitmq:host: localhostport: 5672virtual-host: /wanyuanusername: wanyuanpassword: wanyuan
    消息監(jiān)聽處理類

    編寫消息監(jiān)聽器MyListener

    @Componentpublic class MyListener {/*** 監(jiān)聽某個(gè)隊(duì)列的消息* @param message 接收到的消息*/@RabbitListener(queues = "queue")public void myListener1(String message){System.out.println("消費(fèi)者接收到的消息為:" + message);}}
    測(cè)試

    在生產(chǎn)者工程springboot-rabbitmq-producer中創(chuàng)建測(cè)試類,發(fā)送消息:

    @RunWith(SpringRunner.class)@SpringBootTestpublic class RabbitMQTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void test(){rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "wanyuan.insert", "商品新增,routing key 為wanyuan.insert");rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "wanyuan.update", "商品修改,routing key 為wanyuan.update");rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "wanyuan.delete", "商品刪除,routing key 為wanyuan.delete");}}

    先運(yùn)行上述測(cè)試程序(交換機(jī)和隊(duì)列才能先被聲明和綁定),然后啟動(dòng)消費(fèi)者;在消費(fèi)者工程springboot-rabbitmq-consumer中控制臺(tái)查看是否接收到對(duì)應(yīng)消息。

    另外;也可以在RabbitMQ的管理控制臺(tái)中查看到交換機(jī)與隊(duì)列的綁定

    總結(jié)

    以上是生活随笔為你收集整理的【消息中间件】Spring Boot整合RabbitMQ的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。