日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

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

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

簡(jiǎn)介

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

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

一般在開(kāi)發(fā)過(guò)程中:

生產(chǎn)者工程:

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

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

    添加依賴(là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-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)類(lèi)
    @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ī)綁定的配置類(lèi)RabbitMQConfig

    @Configurationpublic class RabbitMQConfig {//交換機(jī)名稱(chēng)public static final String TOPIC_EXCHANGE = "topic_exchange";//隊(duì)列名稱(chēng)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)者工程

    添加依賴(là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)類(lèi)
    @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)聽(tīng)處理類(lèi)

    編寫(xiě)消息監(jiān)聽(tīng)器MyListener

    @Componentpublic class MyListener {/*** 監(jiān)聽(tīng)某個(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è)試類(lèi),發(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èn)題。

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