RabbitMQ——SpringBoot集成RabbitMQ
文章目录:
1.创建一个SpringBoot工程——消息发送者
1.创建一个SpringBoot工程——消息接收者
3.测试结果
3.1 direct
3.2 fanout
3.3 topic
3.4 RabbitMQ管控台中查看SpringBoot工程创建的交换机和消息队列
1.创建一个SpringBoot工程——消息发送者
前两步都是一样的,只不过在依赖项页面中,要勾选RabbitMQ这个选项。
在核心配置文件中,配置RabbitMQ的相关连接信息。
#配置RabbitMQ的相关连接信息
spring.rabbitmq.host=192.168.40.130
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
编写实现消息发送的接口和实现类。
接口中的三个方法分别对应 direct、fanout、topic三种类型的交换机,我这里测试这三种类型的交换机来发送接收消息。
package com.szh.springboot.rabbitmq.service;/****/
public interface SendService {void sendMessage(String message);void sendFanout(String message);void sendTopic(String message);
}
package com.szh.springboot.rabbitmq.service.impl;import com.szh.springboot.rabbitmq.service.SendService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/****/
@Service("sendService")
public class SendServiceImpl implements SendService {@Autowiredprivate AmqpTemplate amqpTemplate;@Overridepublic void sendMessage(String message) {/*** 发送消息* 参数1:交换机名称* 参数2:RoutingKey* 参数3:具体发送的消息内容*/amqpTemplate.convertAndSend("springbootDirectExchange","springbootDirectRouting",message);}@Overridepublic void sendFanout(String message) {amqpTemplate.convertAndSend("fanoutExchange","",message);}@Overridepublic void sendTopic(String message) {amqpTemplate.convertAndSend("topicExchange","aa.bb.cc",message);}
}
然后写一个关于三种类型交换机的配置类。
package com.szh.springboot.rabbitmq.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/****/
@Configuration
public class RabbitMQConfig {//配置一个Direct类型的交换机@Beanpublic DirectExchange directExchange() {return new DirectExchange("springbootDirectExchange");}//配置一个队列@Beanpublic Queue directQueue() {return new Queue("springbootDirectQueue");}/*** 配置一个队列和交换机的绑定* @param directQueue : 需要绑定的队列对象,参数名必须和某个@Bean的方法名完全相同,这样就会进行自动注入,对应 .bind()* @param directExchange : 需要绑定的交换机对象,参数名必须和某个@Bean的方法名完全相同,这样就会进行自动注入,对应 .to()* .with() 方法对应的RoutingKey* @return*/@Beanpublic Binding directBinding(Queue directQueue,DirectExchange directExchange) {return BindingBuilder.bind(directQueue).to(directExchange).with("springbootDirectRouting");}//配置一个Fanout类型的交换机@Beanpublic FanoutExchange fanoutExchange() {return new FanoutExchange("fanoutExchange");}//配置一个Topic类型的交换机@Beanpublic TopicExchange topicExchange() {return new TopicExchange("topicExchange");}
}
最后是SpringBoot项目的启动入口类。
这里首先是通过ApplicationContext获取到了Spring容器,然后从容器中拿到sendService这个对象,最后的三行代码分别对应的是测试这三种类型的交换机。
package com.szh.springboot.rabbitmq;import com.szh.springboot.rabbitmq.service.SendService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;@SpringBootApplication
public class Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Application.class, args);SendService service= (SendService) context.getBean("sendService");service.sendMessage("SpringBoot集成RabbitMQ的测试数据");//service.sendFanout("SpringBoot集成RabbitMQ的测试数据");//service.sendTopic("SpringBoot集成RabbitMQ的测试数据");}}
1.创建一个SpringBoot工程——消息接收者
前两步都是一样的,只不过在依赖项页面中,要勾选RabbitMQ这个选项。
在核心配置文件中,配置RabbitMQ的相关连接信息。
#配置RabbitMQ的相关连接信息
spring.rabbitmq.host=192.168.40.130
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
编写实现消息接收的接口和实现类。
接口中的这些方法分别对应 direct、fanout、topic三种类型的交换机,我这里测试这三种类型的交换机来发送接收消息。
package com.szh.sprringboot.rabbitmq.service;/****/
public interface ReceiveService {void receiveMessage();void directReceive(String message);void fanoutReceive01(String message);void fanoutReceive02(String message);void topicReceive01(String message);void topicReceive02(String message);void topicReceive03(String message);
}
package com.szh.sprringboot.rabbitmq.service.impl;import com.szh.sprringboot.rabbitmq.service.ReceiveService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/****/
@Service("receiveService")
public class ReceiveServiceImpl implements ReceiveService {@Autowiredprivate AmqpTemplate amqpTemplate;/*** receiveAndConvert()这个方法,每执行一次只能接收一次消息* 如果有消息进入,则不会自动接收消息(不建议使用)*/@Overridepublic void receiveMessage() {
// String message= (String) amqpTemplate.receiveAndConvert("springbootDirectQueue");
// System.out.println(message);}/*** @RabbitListener : 用于标记当前方法是一个RabbitMQ的消息监听方法,可以持续性的自动接收消息* @param message* 该方法不需要手动调用,Spring会自动运行这个监听方法** 注意:如果该监听方法正常结束,那么Spring会自动确认消息* 如果出现异常,则Spring不会确认消息,该消息一直存在于消息队列中*/@Override@RabbitListener(bindings = {@QueueBinding(value = @Queue(name = "springbootDirectQueue"),exchange = @Exchange(name = "springbootDirectExchange"),key = {"springbootDirectRouting"})})public void directReceive(String message) {System.out.println(message);}@Override@RabbitListener(bindings = {@QueueBinding( //完成队列和交换机的绑定value = @Queue(), //创建一个队列,没有name属性,表示创建一个随即名称的消息队列exchange = @Exchange(name = "fanoutExchange",type = "fanout") //创建一个交换机)})public void fanoutReceive01(String message) {System.out.println(message);}@Override@RabbitListener(bindings = {@QueueBinding( //完成队列和交换机的绑定value = @Queue(), //创建一个队列,没有name属性,表示创建一个随即名称的消息队列exchange = @Exchange(name = "fanoutExchange",type = "fanout") //创建一个交换机)})public void fanoutReceive02(String message) {System.out.println(message);}@Override@RabbitListener(bindings = {@QueueBinding(value = @Queue("topic01"),exchange = @Exchange(name = "topicExchange",type = "topic"),key = {"aa"})})public void topicReceive01(String message) {System.out.println("topic01 接收到的数据:" + message);}@Override@RabbitListener(bindings = {@QueueBinding(value = @Queue("topic02"),exchange = @Exchange(name = "topicExchange",type = "topic"),key = {"aa.*"})})public void topicReceive02(String message) {System.out.println("topic02 接收到的数据:" + message);}@Override@RabbitListener(bindings = {@QueueBinding(value = @Queue("topic03"),exchange = @Exchange(name = "topicExchange",type = "topic"),key = {"aa.#"})})public void topicReceive03(String message) {System.out.println("topic03 接收到的数据:" + message);}}
最后是SpringBoot项目的启动入口类。
package com.szh.sprringboot.rabbitmq;import com.szh.sprringboot.rabbitmq.service.ReceiveService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;@SpringBootApplication
public class Application {public static void main(String[] args) {ApplicationContext context=SpringApplication.run(Application.class, args);ReceiveService service= (ReceiveService) context.getBean("receiveService");//service.receiveMessage();}}
3.测试结果
3.1 direct
先启动消息发送者工程,再启动消息接收者工程。
3.2 fanout
先启动消息接收者工程,再启动消息发送者工程。
因为这里fanout交换机中定义了两个消息队列,它是一对多、不需要绑定RoutingKey的,所以这些消息队列都会接收到消息数据。
3.3 topic
先启动消息接收者工程,再启动消息发送者工程。
因为这里topic交换机中定义了三个消息队列,它是一对多、需要绑定RoutingKey的,根据RoutingKey的不同会限制哪些消息队列能够接收到消息、哪些不能。当绑定的RoutingKey为aa时,只有BingKey为 aa、aa.# 这两个消息队列可以接收到(aa顾名思义、而aa.#是因为#表示0个或多个单词,aa.*接收不到是因为*仅能表示1个单词)。
3.4 RabbitMQ管控台中查看SpringBoot工程创建的交换机和消息队列
这里的消息队列只有direct、topic的,至于为什么没有fanout的,是因为fanout类型的交换机在消息发送/接收服务停止之后,对应的交换机还在,但是消息队列会自动清除掉。
RabbitMQ——SpringBoot集成RabbitMQ相关推荐
- springboot 集成rabbitmq 实例
springboot 集成rabbitmq 实例 个人在学习rabbitmq时发现网上很少有系统性介绍springboot和rabbitmq如何集成的,其他人总结的都片段化,所以结合个人调研过程,整理 ...
- SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门
1.Windows下安装RabbitMQ的步骤详解+图解(erlang+RabbitMQ) 2.SpringBoot集成RabbitMQ参考文章 1.RabbitMQ介绍 RabbitMQ是实现AMQ ...
- Springboot集成RabbitMQ一个完整案例
springboot 集成 RabbitMQ 非常简单,如果只是简单的使用配置非常少,springboot 提供了 spring-boot-starter-amqp 对消息各种支持. 1.配置pom文 ...
- springboot 集成 RabbitMQ confirm 确认模式和 return 回退模式以及Consumer Ack模式
springboot 集成 RabbitMQ confirm 确认模式和 return 回退模式以及Consumer Ack模式 说明: RabbitMQ消息的可靠投递 在使用 RabbitMQ 的时 ...
- Springboot集成rabbitmq实现延时队列
Springboot集成rabbitmq实现延时队列 什么是延时队列? 列举几个使用场景: 常见的种类有: 延时任务-实现方式: 详细信息:[https://www.cnblogs.com/JonaL ...
- Springboot集成rabbitMQ之mandatory和备份交换机
Springboot集成rabbitMQ之mandatory和备份交换机 mandatory 之前编写的消息队列代码中,通过重写ConfirmCallback中的confirm方法实现了消息送达的确认 ...
- springboot集成rabbitmq商品秒杀业务实战(流量削峰)
消息队列如何实现流量削峰? 要对流量进行削峰,最容易想到的解决方案就是用消息队列来缓冲瞬时流量,把同步的直接调用转换成异步的间接推送,中间通过一个队列在一端承接瞬时的流量洪峰,在另一端平滑地将消息推送 ...
- SpringBoot集成rabbitmq错误:org.springframework.amqp.AmqpConnectException: java.net.ConnectException的解决办法
在集成rabbitmq后,运行项目,报错日志: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Co ...
- springboot集成rabbitmq,根据查询的信息创建多个消息中心和消息队列,并实现不同的消息发送到不同的消息中心
今天接到一个需求,就是在发送消息到rabbitmq消息中心的时候,需要根据设备类型,将消息发送到不同的消息队列,因此要创建不同的消息队列. 修改之前是把配置信息写在配置文中,项目启动时,获 ...
最新文章
- flume高可用-balance-测试运行
- 学习vim 从常用按键开始
- C#使用CLR/C++的DLL间接调用Native C++的DLL
- 2021 Chrome Devtools 新特性
- react-native 开发在Android模拟器上运行
- 如何下载秦皇岛市卫星地图高清版大图
- 2023最新毕业设计选题 -python毕设选题推荐 - 如何选题 避免被坑
- python灰色预测_【数学建模】灰色预测及Python实现
- java软件工程师工作业绩_JAVA软件工程师简历自我评价
- 恋爱法则在学英语背单词中的应用
- Reids面试题集合 数据结构+穿透雪崩+持久化+内存淘汰策略+数据库双写+哨兵
- PARSEC benchmark 编译
- 【毕业设计项目】基于ESP32的家庭气象站系统 - stm32 物联网 嵌入式 单片机
- 【安装系统】U盘安装系统教程,使用UltraISO制作U盘启动盘
- 解压上传zip文件并获取excel表数据
- JavaScript垃圾收集
- 浅析HTML5中标签del和ins以及HTML5中被舍弃的标签
- 制作一款简易的可燃气体报警器,你来吗?
- LinuxC++:网络编程(一)最原始服务端及客户端代码实现和函数释义
- a20+android4.4,6.6【落叶出品】开博尔全志A20双核系列KIUI7.0_Android4.4固件