SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅。
1、概念:SpringBoot 整合消息服务
2、具体内容
对于异步消息组件在实际的应用之中会有两类:
· JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的;
· AMQP:直接利用协议实现的消息组件,其大众代表作:RabbitMQ,高性能代表作:Kafka。
2.1、SpringBoot 整合 ActiveMQ
1、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、 修改 application.yml 配置文件进行 activemq 的配置;
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
jms:
pub-sub-domain: false # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
activemq:
user: studyjava # 连接用户名
password: hello # 连接密码
broker-url: tcp://activemq-server:61616 # 消息组件的连接主机信息
3、 随后定义一个消息的消费者,消费者主要是进行一个监听控制,在 SpringBoot 里面可以直接利用注解@JmsListener进行监听:
package cn.study.microboot.consumer; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service; @Service
public class MessageConsumerService {
@JmsListener(destination="study.msg.queue")
public void receiveMessage(String text) { // 进行消息接收处理
System.err.println("【*** 接收消息 ***】" + text);
}
}
4、 随后建立消息的发送者服务,一般而言如果进行消息的发送往往会准备出一个业务接口来:
package cn.study.microboot.producer; public interface IMessageProducerService {
public void sendMessage(String msg) ;
}
5、 随后建立一个配置程序类,定义 ActiveMQ 的消息发送模版处理类:
package cn.study.microboot.config; import javax.jms.Queue; import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms; @Configuration
@EnableJms
public class ActiveMQConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("study.msg.queue") ;
}
}
6、 创建消息发送的子类实现消息发送处理:
package cn.study.microboot.producer.impl; import javax.annotation.Resource;
import javax.jms.Queue; import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import cn.study.microboot.producer.IMessageProducerService;
@Service
public class MessageProducerServiceImpl implements IMessageProducerService {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Queue queue;
@Override
public void sendMessage(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
} }
7、 编写测试类来观察消息的处理:
package cn.study.microboot.test; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import cn.study.microboot.StartSpringBootMain;
import cn.study.microboot.producer.IMessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
@Resource
private IMessageProducerService messageProducer;
@Test
public void testSend() throws Exception {
for (int x = 0; x < 10; x++) {
this.messageProducer.sendMessage("study - " + x);
}
}
}
基于 SpringBoot 配置的 JMS 的组件访问整体的处理十分简单
2.2、SpringBoot 整合 RabbitMQ
如果要进行 RabbitMQ 整合的时候一定要注意以下几个概念:交换空间、虚拟主机、队列信息。本次为了方便起见将项目分为 两个:RabbitMQ-Consumer、RabbitMQ-Producer。
1、 【两个项目】将 rabbitmq 的依赖支持包拷贝到项目之中;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、 【microboot-rabbitmq-producer、microboot-rabbitmq-consumer】修改 application.yml 配置文件,追加 rabbitmq 的相关配置项:
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
rabbitmq:
addresses: rabbitmq-server
username: studyjava
password: hello
virtual-host: /
3、 【microboot-rabbitmq-producer】建立一个消息的发送接口:
package cn.study.microboot.producer; public interface IMessageProducerService {
public void sendMessage(String msg) ;
}
4、 【microboot-rabbitmq-producer】为了可以正常使用 RabbitMQ 进行消息处理,你还需要做一个消息生产配置类;
package cn.study.microboot.config; import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ProducerConfig {
public static final String EXCHANGE = "study.microboot.exchange"; // 交换空间名称
public static final String ROUTINGKEY = "study.microboot.routingkey"; // 设置路由key
public static final String QUEUE_NAME = "study.microboot.queue"; // 队列名称
@Bean
public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ;
}
@Bean
public DirectExchange getDirectExchange() { // 使用直连的模式
return new DirectExchange(EXCHANGE, true, true);
}
@Bean
public Queue queue() { // 要创建的队列信息
return new Queue(QUEUE_NAME);
}
}
5、 【microboot-rabbitmq-producer】创建消息服务的实现子类:
package cn.study.microboot.producer.impl; import javax.annotation.Resource; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service; import cn.study.microboot.config.ProducerConfig;
import cn.study.microboot.producer.IMessageProducerService;
@Service
public class MessageProducerServiceImpl implements IMessageProducerService {
@Resource
private RabbitTemplate rabbitTemplate;
@Override
public void sendMessage(String msg) {
this.rabbitTemplate.convertAndSend(ProducerConfig.EXCHANGE,
ProducerConfig.ROUTINGKEY, msg);
} }
6、 【microboot-rabbitmq-consumer】依然需要做一个消费者的配置程序类,而这个程序类里面主要的目的依然是设置交换空间、 路由 KEY 等信息。
package cn.study.microboot.config; import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ConsumerConfig {
public static final String EXCHANGE = "study.microboot.exchange"; // 交换空间名称
public static final String ROUTINGKEY = "study.microboot.routingkey"; // 设置路由key
public static final String QUEUE_NAME = "study.microboot.queue"; // 队列名称
@Bean
public Queue queue() { // 要创建的队列信息
return new Queue(QUEUE_NAME);
}
@Bean
public DirectExchange getDirectExchange() { // 使用直连的模式
return new DirectExchange(EXCHANGE, true, true);
}
@Bean
public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ;
}
}
7、 【microboot-rabbitmq-consumer】实现监听处理类:
package cn.study.microboot.consumer; import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service; @Service
public class MessageConsumerService {
@RabbitListener(queues="study.microboot.queue")
public void receiveMessage(String text) { // 进行消息接收处理
System.err.println("【*** 接收消息 ***】" + text);
}
}
8、 【microboot-rabbitmq-producer】创建一个测试类实现消息的发送处理。
package cn.study.microboot.test; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import cn.study.microboot.StartSpringBootMain;
import cn.study.microboot.producer.IMessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
@Resource
private IMessageProducerService messageProducer;
@Test
public void testSend() throws Exception {
for (int x = 0; x < 100; x++) {
this.messageProducer.sendMessage("study - " + x);
}
}
}
9、 【microboot-rabbitmq-consumer】编写消息接收测试类,这里面不需要编写代码,只需要做一个休眠即可:
package cn.study.microboot; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class AppTest {
@Test
public void testStart() throws Exception {
Thread.sleep(Long.MAX_VALUE);
}
}
整体进行项目开发之中整合的处理步骤还是简单,但是千万要注意,由于是第一次整合处理,所以将生产者与消费者的配置 类分开了,实际上这两个类的作用是完全一样的。
2.3、SpringBoot 整合 Kafka
Kafka 是现在最好的开源消息组件,其仿照 AMQP 协议操作,而且处理的性能也是最高的。本次使用已经配置好的 Kafka 服 务器,而且这台服务器上使用了 kerberos 认证,所以应该首先准备好一个 jass 配置文件:
1、 定义“kafka_client_jaas.conf”配置文件:
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="bob"
password="bob-pwd";
};
2、 为了方便进行项目的观察, 本次依然准备出了两个项目:生产者( microboot-kafka-producer )、 消 费 者 (microboot-kafka-consumer),随后为这两个项目添加 kafka 配置支持:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
3、 【micorboot-kafka-consumer】修改 application.yml 配置文件,进行 kafka 配置项编写:
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
kafka:
bootstrap-servers:
- kafka-single:9095
template:
default-topic: mldn-microboot
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
group-id: group-1
properties:
sasl.mechanism: PLAIN
security.protocol: SASL_PLAINTEXT
4、 【micorboot-kafka-consumer】建立一个 Kafka 的消息的消费程序类:
package cn.study.microboot.consumer; import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service; @Service
public class MessageConsumerService {
@KafkaListener(topics = {"study-microboot"})
public void receiveMessage(ConsumerRecord<String, String> record) { // 进行消息接收处理
System.err.println("【*** 接收消息 ***】key = " + record.key() + "、value = "
+ record.value());
}
}
5、 【micorboot-kafka-consumer】随后还需要修改 SpringBoot 的启动程序类,追加 kerberos 配置:
package cn.study.microboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
static {
System.setProperty("java.security.auth.login.config",
"d:/kafka_client_jaas.conf"); // 表示系统环境属性
}
public static void main(String[] args) throws Exception {
SpringApplication.run(StartSpringBootMain.class, args);
}
}
6、 【microboot-kafka-producer】修改 application.yml 配置文件:
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
kafka:
bootstrap-servers:
- kafka-single:9095
template:
default-topic: mldn-microboot
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
properties:
sasl.mechanism: PLAIN
security.protocol: SASL_PLAINTEXT
7、 【microboot-kafka-producer】定义消息发送的服务接口:
package cn.study.microboot.producer; public interface IMessageProducerService {
public void sendMessage(String msg) ;
}
package cn.study.microboot.service.impl; import javax.annotation.Resource; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service; import cn.study.microboot.service.IMessageProducerService; @Service
public class MessageProducerServiceImpl implements IMessageProducerService {
@Resource
private KafkaTemplate<String, String> kafkaTemplate;
@Override
public void send(String msg) {
this.kafkaTemplate.sendDefault("study-key", msg);
} }
8、 【microboot-kafka-producer】修改程序启动类:
package cn.mldn.microboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
static {
System.setProperty("java.security.auth.login.config",
"d:/kafka_client_jaas.conf"); // 表示系统环境属性
}
public static void main(String[] args) throws Exception {
SpringApplication.run(StartSpringBootMain.class, args);
}
}
9、 【microboot-kafka-producer】编写消息发送的程序类:
package cn.study.microboot; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import cn.study.microboot.service.IMessageProducerService;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageService {
@Resource
private IMessageProducerService messageService;
@Test
public void testStart() throws Exception {
for (int x = 0; x < 100; x++) {
this.messageService.send("study - " + x);
}
}
}
在使用 Kafka 进行数据处理的时候一定要记住,它速度快的主要原因是采用的协议、处理的模式、零拷贝。
3、总结
实际开发之中 90%环境下常用的三个消息组件:ActiveMQ、RabbitMQ、Kafka 的全部定义都在此处,以后你们所从事的开发 里面一定会有消息组件的身影。消息组件带来的最直观好处:数据缓冲,可以保证消息不丢失。
面试题:请解释一下 ActiveMQ 与 RabbitMQ 区别?
· ActiveMQ 使用的是 JMS 协议处理,所以性能比较差,在 ActiveMQ 里面其组成比较简单就是进行主题或者是队列消息的 处理;
·RabbitMQ 使用的是 AMQP 处理,该处理属于一种协议处理,所以处理的性能会比较高,在 RabbitMQ 里面提供有 exchange、 queue、bind 的概念,所有的用户提交的消息发送给 exchange,而后由 bind 绑定 exchange 与 queue,最后根据 routingkey 进行消息 的发送处理,利用这一概念可以实现 fanout(广播)、topic(主题)、direct(直连)的操作处理。同时在 Rabbitmq 之中还通过有虚 拟主机的概念,也就是说不同的虚拟主机可以有自己独立的用户管理、空间管理。
面试题:请解释一下 RabbitMQ 与 Kafka 关系?
· 使用最为广泛性能也比较好的就是 RabbitMQ 组件,Rabbitmq 中的消息消费完就删除,RabbitMQ 本身支持的集群功能有 限,必须结合 HAProxy、Keepalived 才能够实现负载均衡与 HA 技术;
· Kafka 采用零拷贝、批量读取技术可以实现高效的消息交互,Kafka 中的消息会保存两天,同时提供有一个 offset 可以实现 历史消息的读取,Kafka 直接支持有 HA 与负载均衡的支持,在 Kafka 里面支持有数据的副本操作,可以保证数据更加安全。
SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)的更多相关文章
- 新鲜出炉,这是全网讲的最详细的springboot整合消息服务了吧,建议收藏!
springboot整合activeMq ActiveMq是Apache提供的开源消息系统采用java实现, 很好地支持JMS(Java Message Service,即Java消息服务) 规范 A ...
- (十七)SpringBoot之使用异步消息服务jms之ActiveMQ
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- springboot系列八、springboot整合kafka
背景: 当业务在同一时间出现高并发的时候,这个时候我们不想无限的增加服务器,但是又想提高吞吐量.这时可以考虑使用消息异步处理,进行消峰填谷:同时还可以降低耦合度.常见的消息中间件有kafka,rabb ...
- springboot系列九,springboot整合邮件服务、整合定时任务调度
一.整合邮件服务 如果要进行邮件的整合处理,那么你一定需要有一个邮件服务器,实际上 java 本身提供有一套 JavaMail 组件以实现邮件服务器的搭建,但是这个搭建的服务器意义不大,因为你现在搭建 ...
- SpringBoot系列(八)分分钟学会Springboot多种解决跨域方式
SpringBoot系列(八) 分分钟学会SpringBoot多种跨域解决方式 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 s ...
- SpringBoot系列之学习教程汇总
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件 >> source down ...
- SpringBoot系列之从入门到精通系列教程
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...
- SpringBoot系列(九)单,多文件上传的正确姿势
SpringBoot系列(九)分分钟解决文件上传 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...
- SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口
前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...
随机推荐
- 解密OpenTSDB的表存储优化【转】
https://yq.aliyun.com/articles/54785 摘要: 本篇文章会详细讲解OpenTSDB的表结构设计,在理解它的表结构设计的同时,分析其采取该设计的深层次原因以及优缺点.它 ...
- 【内核】linux内核启动流程详细分析
Linux内核启动流程 arch/arm/kernel/head-armv.S 该文件是内核最先执行的一个文件,包括内核入口ENTRY(stext)到start_kernel间的初始化代码, 主要作用 ...
- 【教程】ubuntu中配置tftp
1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端)以及 xinetd (超级服务器) 1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端) ...
- vue2.0如何自定义全局变量的方法
方法一:http://www.jianshu.com/p/04dffe7a6b74 //在mian.js中写入函数 Vue.prototype.changeData = function (){ al ...
- golang初始化结构体数组
最近组里新项目要求用go来写,没办法只能边看文档边写代码,今天遇到郁闷的问题,查了好久最终发现居然是一个标点符号的导致的,遂纪录之 刚刚给一个接口写单元测试时想初始化一个结构体数组,然后遍历该数组并建 ...
- $.ajax使用总结(一):Form提交与Payload提交
http://blog.csdn.net/yiifaa/article/details/73468001 *********************************************** ...
- django 部署,gunicorn、virtualenv、nginx
声明: 1.本篇文章是我边写命令边写的,请尊重我的劳动成果,转载请加上链接. 2.我既然公开写出来,是希望大家遇到问题的时候有个参考,所以,大家可以免费转载,使用该文章 3.但是,如果你要用这篇文章来 ...
- 【WPF/C#】图层筛选/拾取——Color Picker
文章标题实在不懂怎么表述才清楚. 描述问题:多个图片(图层)重叠时,如何通过鼠标点击来拾取到相应的图层.因为图层中会有很多空白的地方(比如图片四周),要求是获取鼠标点击位置的像素颜色值,如果为空白时或 ...
- Java 原码 反码 补码
本篇文章讲解了计算机的原码, 反码和补码. 并且进行了深入探求了为何要使用反码和补码, 以及更进一步的论证了为何可以用反码, 补码的加法计算原码的减法. 论证部分如有不对的地方请各位牛人帮忙指正! 希 ...
- Linux文本编辑器(九)
[教程主题]:Linux文本编辑器 [1]vi vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任 何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令. 由于对U ...