当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
【消息中间件】Spring Boot整合RabbitMQ
生活随笔
收集整理的这篇文章主要介绍了
【消息中间件】Spring Boot整合RabbitMQ
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
简介
在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ
尤其是在spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate发送消息,使用注解接收消息。
一般在开发过程中:
生产者工程:
消费者工程:
搭建生产者工程
添加依赖
修改pom.xml文件内容为如下:
<?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>启动类
@SpringBootApplicationpublic class ProducerApplication {public static void main(String[] args) {SpringApplication.run(ProducerApplication.class);}}配置RabbitMQ
1)配置文件
创建application.yml,内容如下:
spring:rabbitmq:host: localhostport: 5672virtual-host: /wanyuanusername: wanyuanpassword: wanyuan2)绑定交换机和队列
创建RabbitMQ队列与交换机绑定的配置类RabbitMQConfig
@Configurationpublic class RabbitMQConfig {//交换机名称public static final String TOPIC_EXCHANGE = "topic_exchange";//队列名称public static final String QUEUE = "queue";//声明交换机@Bean("topicExchange")public Exchange topicExchange(){return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();}//声明队列@Bean("queue")public Queue queue(){return QueueBuilder.durable(QUEUE).build();}//绑定队列和交换机@Beanpublic Binding itemQueueExchange(@Qualifier("queue") Queue queue,@Qualifier("topicExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with("wanyuan.#").noargs();}}搭建消费者工程
添加依赖
修改pom.xml文件内容为如下:
<?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>启动类
@SpringBootApplicationpublic class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class);}}配置RabbitMQ
创建application.yml,内容如下:
spring:rabbitmq:host: localhostport: 5672virtual-host: /wanyuanusername: wanyuanpassword: wanyuan消息监听处理类
编写消息监听器MyListener
@Componentpublic class MyListener {/*** 监听某个队列的消息* @param message 接收到的消息*/@RabbitListener(queues = "queue")public void myListener1(String message){System.out.println("消费者接收到的消息为:" + message);}}测试
在生产者工程springboot-rabbitmq-producer中创建测试类,发送消息:
@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");}}先运行上述测试程序(交换机和队列才能先被声明和绑定),然后启动消费者;在消费者工程springboot-rabbitmq-consumer中控制台查看是否接收到对应消息。
另外;也可以在RabbitMQ的管理控制台中查看到交换机与队列的绑定
总结
以上是生活随笔为你收集整理的【消息中间件】Spring Boot整合RabbitMQ的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【消息中间件】Spring整合Rabbi
- 下一篇: 【面试题】Spring,SpringMV