SpringBoot与消息(RabbitMQ)
1. JMS和AMQP
- JMS(Java Message Service):
- ActiveMQ是JMS实现;
- AMQP(Advanced Message Queuing Protocol)
- 兼容JMS
- RabbitMQ是AMQP的实现
2. RabbitMQ 简介
Message:由消息头和消息体组成,消息体是不透明的,而消息头则由一系列的可选属性组成;Publisher:一个向交换器发布消息的客户端应用程序;Exchange:用来接收生产者发送的消息并将这些消息路由给服务器中的队列;- 有四种类型:direct(默认),fanout,topic和headers;
Queue:用来保存消息直到发送给消费者,是消息的容器;Binding:用于消息队列和交换器之间的关联;Connection:网络连接,比如一个TCP连接;Channel:多路复用连接中的一条独立的双向数据流通道;Consumer:消息的消费者,表示一个从消息队列中取得消息的客户端应用程序;Virtual Host:虚拟主机,表示一批交换器,消息队列和相关对象;每个vhost本质上就是一个mini版的RabbitMQ服务器;Broker:表示消息队列服务器实体;


3. RabbitMQ 整合(SpringBoot)
- 自动配置:
RabbitAutoConfiguration- 自动配置了连接工厂
ConnectionFactory; RabbitProperties封装了RabbitMQ的配置;RabbitTemplate:给RabbitMQ发送和接收消息;AmpqAdmin:RabbitMQ系统管理功能组件;@EnableRabbit:开启基于注解的RabbitMQ模式;@EnableRabbit和@RabbitListener用于监听消息队列的内容;
// application.properties 配置文件
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
// 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests{
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void contextLoads(){
// 点对点消息
// Message 需要自己构造一个,定义消息体内容和消息头
// rabbitTemplate.send(exchange, routeKey, message);
// rabbitTemplate.convertAndSend(exchange, routeKey, object)
// 只需要传入要发送的对象, 会自动序列化发送给rabbitmq, object 默认当成消息体
Map<String, Object> map = new HashMap<>();
map.put("msg","匆匆的我来了...");
map.put("data",Arrays.asList("777477",232,true));
rabbitTemplate.convertAndSend("exchange.direct", "atnoodles.news",map);
}
// 接收消息
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atnoodles.news");
System.out.println(o.getClass()); // Class java.util.HashMap
System.our.println(o);
}
}
// 如果需要将发送的数据自动转换为JSON,发送出去
// com.noodles.springboot.rabbitmq.config.MyAMQPConfig.java
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAMQPConfig{
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
4. AmpqAdmin
- 创建和删除 Queue, Exchange, Binding
// 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests{
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
// 创建 Exchange
amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
System.out.println("创建完成...");
// 创建 Queue
amqpAdmin.declareQueue(new Queue("amqpAdmin.queue", true));
}
}
参考资料:
SpringBoot与消息(RabbitMQ)的更多相关文章
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- SpringBoot系列之RabbitMQ使用实用教程
SpringBoot系列之RabbitMQ使用实用教程 @ 目录 1. 消息队列概述 1.1 MQ的概述 1.2 MQ目的地形式 2. 消息队列实现方式 2.1 常见MQ框架 2.2 MQ实现方式 3 ...
- SpringBoot应用操作Rabbitmq(fanout广播高级操作)
一.广播模式fanout.不需要指定路由key. 注:与topic和direct区别是:fanout广播模式会两个队列同时发送相同的消息,并非由交换器转发到某一个队列 二.实战(广播模式) 1.引入m ...
- SpringBoot应用操作Rabbitmq(topic交换器高级操作)
一.topic交换器为主题交换器,可以根据路由key模糊匹配 实现模型图 二.实战 1.引入maven <dependency> <groupId>org.springfram ...
- SpringBoot应用操作Rabbitmq(direct高级操作)
一.首先声明完成任务架构,通过direct订阅/发布的模式进行生产消费. a.消息生产指定交换器和路由key b.消费者绑定交换器,路由key和队列的关系(集群监控收到的消息不重复) 二.实战演练 1 ...
- SpringBoot应用操作Rabbitmq
记录RabbitMQ的简单应用 1.springboot项目中引入maven包,也是springboot官方的插件 <dependency> <groupId>org.spri ...
- springboot入门系列(五):SpringBoot连接多RabbitMQ源
SpringBoot连接多RabbitMQ源 在实际开发中,很多场景需要异步处理,这时就需要用到RabbitMQ,而且随着场景的增多程序可能需要连接多个RabbitMQ.SpringBoot本身提供了 ...
- 新鲜出炉,这是全网讲的最详细的springboot整合消息服务了吧,建议收藏!
springboot整合activeMq ActiveMq是Apache提供的开源消息系统采用java实现, 很好地支持JMS(Java Message Service,即Java消息服务) 规范 A ...
- springboot入门系列(三):SpringBoot教程之RabbitMQ示例
SpringBoot教程之RabbitMQ示例 SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用Rabb ...
随机推荐
- spring oauth2相关资料
理解OAuth 2.0 *****http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html Secure REST API with oauth2 ...
- 亿级日PV的魅族云同步的核心协议与架构实践(转)
云同步的业务场景 这是魅族云同步的演进,第一张是M8.M9,然后到后面的是MX系统,M9再往后发展,我们的界面可以看到基本上是没有什么变化的,但本质发生了很大的变化,我们经过了一些协议优化,发展到今天 ...
- notification 报错the method build() is undefined for the type Notificatin.Builder
notification 报错the method build() is undefined for the type Notificatin.Builder 这事api版本号太低导致的 Notifi ...
- selenuim爬虫实战 (下)
SuperLOFTERDownloader7.java package test; import java.io.IOException; import java.util.ArrayList; im ...
- CRC16
http://www.stmcu.org/chudonganjin/blog/12-08/230184_515e6.html 1.循环校验码(CRC码): 是数据通信领域中最常用的一种差错校验码,其特 ...
- Javascript中暂停功能的实现
<script language="javascript"> /*Javascript中暂停功能的实现 Javascript本身没有暂停功能(sleep不能使用)同时 ...
- imx6 uboot logo 更改
最近需要更改im6 uboot的开机logo,使用10.1inch, 1024x600,18bit的LCD,期间遇到了很多的问题,记录于此. 参考链接 https://community.nxp.co ...
- ActionContextCleanUp
ActionContextCleanUp作用 延长action中属性的生命周期,包括自定义属性,以便在jsp页面中进行访问,让actionContextcleanup过滤器来清除属性,不让acti ...
- A*寻路算法(曼哈顿距离)
前一些天,在群有人问到A*算法的问题. 之前我已经有实现过,并将之放到github上(https://github.com/XJM2013/A_Star).有兴趣的能够下载下来看看. 这里上传了一个相 ...
- STL map 的 key 元素
在做 compiler 语义分析时, 需要用到 map<?,?> 在别人的代码上做扩展, 所以有些代码是不能动的 这时, 需要一个 map<symbol,int> 的数据结构, ...