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 ...
随机推荐
- Activiti工作流简单入门 (zhuan)
https://my.oschina.NET/Barudisshu/blog/309721 *********************************************** 摘要: 自j ...
- C++链接ODBC数据源:VS2013,Access
参考资料:1.http://wenku.baidu.com/view/a92d1a812cc58bd63186bd8d.html 2.http://blog.sina.com.cn/s/blog_68 ...
- 一款基于jQuery的联动Select下拉框
今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...
- Unix系统编程()深入探究文件IO概述
open调用将引入原子atomicity操作的概念. 将某一系统调用所要完成的各个动作作为不可中断的操作,一次性加以执行. 原子操作是许多系统调用得以正确执行的必要条件. 还介绍一个系统调用fcntl ...
- Java调用doNet webService方法
doNet的webService 浏览器访问测试地址:http://192.168.4.17/JLWWS/sendCommand.asmx,出现 点击getDeviceValue方法,出现 上图的xm ...
- setTranslatesAutoresizingMaskIntoConstraints和setFrame组合使用导致的异常
在用Ojbect-c开发OSX应用的时候需要用到自定义控件并用代码进行布局,很自然地就使用了setTranslatesAutoresizingMaskIntoConstraints和setFrame组 ...
- python ascii codec can't decode
提示错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 240: ordinal not in range ...
- finals的使用
//----------------------------------------Finals--------------------------- public class Finals { pu ...
- JAVA在语言级支持多线程
进程:任务 任务并发执行是一个宏观概念,微观上是串行的. 进程的调度是有OS负责的(有的系统为独占式,有的系统为共享式,根据重要性,进程有优先级). 由OS将时间分为若干个时间片. JAVA在语言级支 ...
- linux上限制用户进程数、cpu占用率、内存使用率
限制进程CPU占用率的问题,给出了一个shell脚本代码如下: renice +10 `ps aux | awk '{ if ($3 > 0.8 && id -u $1 > ...