SpringBoot整合RabbitMq
SpringBoot整合RabbitMq
RabbitMQ是基于 erlang 语言开发的, 为了使用 rabbitMQ 需要安装 erlang环境。
安装erlang 环境
- 下载地址 https://www.erlang.org/downloads
选择自己适合的操作系统下载 - 配置环境变量
- 测试是否安装成功
win+r打开cmd,输入erl,出现如图所示内容表示安装成功
安装RabbitMQ
1.下载地址https://www.rabbitmq.com/download.html
选择自己的盘符默认下一步安装就行
2.配置插件
win+r打开cmd输入以下命令
“C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin\rabbitmq-plugins.bat” enable rabbitmq_management
出现如图所示表示插件配置成功。
3.重启 RabbitMQ
以管理员身份运行以下命令:
net stop RabbitMQ && net start RabbitMQ
4.访问管理界面
http://127.0.0.1:15672
输入账号: guest,密码: guest
登陆成功页面如下
至此环境准备好,下面进行springboot和rabbitmq的整合
整合SprinBoot和RabbitMQ
1.创建springboot项目,pom.xml引入下面依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version>1.5.2.RELEASE</version> </dependency>
2.创建application.properties配置文件
#配置rabbitMQ的支持 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
3.创建RabbitmqConfig配置类
/** *创建fanout模式队列 */ @Bean public Queue queueA(){ return new Queue("queueA"); } @Bean public Queue queueB(){ return new Queue("queueB"); } /** *创建topic模式队列 */ @Bean public Queue queueMessage() { return new Queue("topic.message"); } @Bean public Queue queueMessages() { return new Queue("topic.messages"); } /** *创建交换器 */ @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("fanoutExchange"); } @Bean public TopicExchange topicExchange(){ return new TopicExchange("topicExchange"); } /** * 交换器和队列绑定 fanout模式 * */ @Bean public Binding bindingExchangeQueueFanoutA(Queue queueA,FanoutExchange fanoutExchange){ return BindingBuilder.bind(queueA).to(fanoutExchange); } @Bean public Binding bindingExchangeQueueFanoutB(Queue queueB,FanoutExchange fanoutExchange){ return BindingBuilder.bind(queueB).to(fanoutExchange); } /** * 交换器和队列绑定 topic模式 * */ @Bean public Binding bindingExchangeMessage(Queue queueMessage,TopicExchange topicExchange){ return BindingBuilder.bind(queueMessage).to(topicExchange).with("topic.message"); } @Bean Binding bindingExchangeMessages(Queue queueMessages, TopicExchange topicExchange) { return BindingBuilder.bind(queueMessages).to(topicExchange).with("topic.#"); }
4.创建生产者
@Component public class Producer { @Autowired private RabbitTemplate rabbitTemplate; /** * fanout模式生产消息 */ public void fanoutProduction() { for (int i = 0; i < 10; i++) { String message = "消息" + i; rabbitTemplate.convertAndSend("fanoutExchange","", message); } } /** * topic模式生产消息 */ public void topicProduction() { String message = "topic.message"; rabbitTemplate.convertAndSend("topicExchange", "topic.message", message); String messages = "topic.messages"; rabbitTemplate.convertAndSend("topicExchange", "topic.messages", messages); } }
5.创建消费者
fanout的模式这里只实现了单个生产者对多个消费者的情况
两个消费者如下:
@Component @RabbitListener(queues = "queueA") public class FanoutConsumeA { @RabbitHandler public void consume(String msg){ System.out.println("queueA:" + msg) ; } }
@Component @RabbitListener(queues = "queueB") public class FanoutConsumeB { @RabbitHandler public void consume(String msg){ System.out.println("queueB:" + msg); } }
topic模式消费者如下
@Component @RabbitListener(queues = "topic.message") public class TopicConsumeMessage { @RabbitHandler public void consume(String msg){ System.out.println("topic.message:" + msg) ; } }
@Component @RabbitListener(queues = "topic.messages") public class TopicConsumeMessages { @RabbitHandler public void consume(String msg){ System.out.println("topic.messages:" + msg) ; } }
6.创建启动类访问
@RestController @RequestMapping("/rabbit") public class RabbitController { @Autowired private Producer producer; @GetMapping("/fanout") public void fanout(){ producer.fanoutProduction(); } @GetMapping("/topic") public void topic(){ producer.topicProduction(); } }
启动项目
访问 http://127.0.0.1:8080/rabbit/fanout
访问 http://127.0.0.1:8080/rabbit/topic
完整代码
https://gitee.com/zhching/springboot-rabbitmq
- 点赞
- 收藏
- 分享
- 文章举报
- springboot 学习之路 20 (整合RabbitMQ)
- springboot整合RabbitMQ
- Spring Boot整合RabbitMQ
- RabbitMQ和Springboot整合 入门案例
- springboot-整合rabbitmq-生产者-消费者
- RabbitMQ 实战(二)Spring Boot 整合 RabbitMQ
- SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
- rabbitmq学习(五):springboot整合rabbitmq
- Spring boot 整合RabbitMq有简单到深入
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置
- RabbitMQ的安装及和springboot的整合
- spring boot整合RabbitMQ(Topic模式)
- SpringBoot整合RabbitMQ之 典型应用场景实战一
- rabbitmq 整合springboot
- spring boot实战(第十四篇)整合RabbitMQ源码分析前言
- RabbitMQ实战(一)Spring Boot 整合 RabbitMQ
- springboot整合rabbitmq
- springboot整合rabbitmq,支持消息确认机制
- spring boot整合RabbitMQ(Direct模式)
- springboot整合rabbitmq,动态创建queue和监听queue