javascript
【消息中间件】Spring Boot整合RabbitMQ
簡(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)者工程:
消費(fèi)者工程:
搭建生產(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: wanyuan2)綁定交換機(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)題。
- 上一篇: 【消息中间件】Spring整合Rabbi
- 下一篇: 【消息中间件】RabbitMQ 高级特性