简介
在spring项目中,可以使用spring-rabbit去操作rabbitmq
尤其是在spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate发送消息,使用注解接收消息。
一般在开发过程中:
生产者工程:
application.yml文件配置RabbitMQ相关信息;
在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定
注入RabbitTemplate对象,通过RabbitTemplate对象发送消息到交换机
消费者工程:
application.yml文件配置RabbitMQ相关信息
创建消息处理类,用于接收队列中的消息并进行处理
生产端
1. 创建生产者SpringBoot工程(maven)2. 引入start,依赖坐标 org.springframework.boot spring-boot-starter-amqp 3. 编写yml配置,基本信息配置4. 定义交换机,队列以及绑定关系的配置类5. 注入RabbitTemplate,调用方法,完成消息发送
添加依赖
修改pom.xml文件内容为如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.itheima springboot-rabbitmq-producer 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-test
登录后复制
启动类
package com.itheima.rabbitmq;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class); }}
登录后复制
配置RabbitMQ
配置文件
创建application.yml,内容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
绑定交换机和队列
创建RabbitMQ队列与交换机绑定的配置类com.itheima.rabbitmq.config.RabbitMQConfig
package com.itheima.rahhitmq.config;import org.springframework.amqp.core.*;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration /// 配置类public class RabbitMQConfig { public static final String EXCHAGE_NAME = "boot_topic_exchange"; public static final String QUEUE_NAME = "boot_queue"; // 交换机 @Bean("bootExchange") public Exchange bootExchange(){ // 构建交换机对象 return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build(); } //Queue 队列 @Bean("bootQueue") public Queue bootQueue(){ return QueueBuilder.durable(QUEUE_NAME).build(); } //队列和交换机的关系 Binding /** * 1 知道那个队列 * 2 知道那个交换机 * 3 routingKey */ @Bean public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs(); }}
登录后复制
搭建消费者工程
创建工程
生产端
1. 创建生产者SpringBoot工程
2. 引入start,依赖坐标
org.springframework.boot
spring-boot-starter-amqp
编写yml配置,基本信息配置
定义交换机,队列以及绑定关系的配置类
注入RabbitTemplate,调用方法,完成消息发送
添加依赖
修改pom.xml文件内容为如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.itheima springboot-rabbitmq-consumer 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-amqp
登录后复制
启动类
package com.itheima.rabbitmq;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class); }}
登录后复制
配置RabbitMQ
创建application.yml,内容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
消息监听处理类
编写消息监听器com.itheima.rabbitmq.listener.MyListener
package com.itheima.rabbitmq.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Componentpublic class MyListener { /** * 监听某个队列的消息 * @param message 接收到的消息 */ @RabbitListener(queues = "item_queue") public void myListener1(String message){ System.out.println("消费者接收到的消息为:" + message); }}
登录后复制
测试
在生产者工程springboot-rabbitmq-producer中创建测试类,发送消息:
package com.itheima.rabbitmq;import com.itheima.rabbitmq.config.RabbitMQConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class RabbitMQTest { @Autowired private RabbitTemplate rabbitTemplate; @Test public void test(){ rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 为item.insert"); rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 为item.update"); rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品删除,routing key 为item.delete"); }}
登录后复制
先运行上述测试程序(交换机和队列才能先被声明和绑定),然后启动消费者;在消费者工程springboot-rabbitmq-consumer中控制台查看是否接收到对应消息。
SpringBoot提供了快速整合RabbitMQ的方式
基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
生产端直接注入RabbitTemplate完成消息发送
消费端直接使用@RabbitListener完成消息接收
以上就是SpringBoot整合消息队列RabbitMQ的方法是什么的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2590830.html