当前位置:   article > 正文

SpringBoot整合 ActiveMQ、SpringBoot整合RabbitMQ、SpringBoot整合Kafka_spring boot activemq和rabbitmq同时连接

spring boot activemq和rabbitmq同时连接
  1. 1、概念:SpringBoot 整合消息服务
  2. 2、具体内容
  3. 对于异步消息组件在实际的应用之中会有两类:
  4. · JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的;
  5. · AMQP:直接利用协议实现的消息组件,其大众代表作:RabbitMQ,高性能代表作:Kafka。
  6. 2.1、SpringBoot 整合 ActiveMQ
  7. 1、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-activemq</artifactId>
  11. </dependency>
  12. 2、 修改 application.properties 配置文件进行 activemq 的配置;
  13. spring.jms.pub-sub-domain=false
  14. spring.activemq.user=admin
  15. spring.activemq.password=1234
  16. spring.activemq.broker-url=tcp://59.115.158.145:61616
  17. 3、 随后定义一个消息的消费者,消费者主要是进行一个监听控制,在 SpringBoot 里面可以直接利用
  18. 注解@JmsListener进行监听:
  19. package com.microboot.consumer;
  20. import org.springframework.jms.annotation.JmsListener;
  21. import org.springframework.stereotype.Service;
  22. @Service
  23. public class MessageConsumerService {
  24. @JmsListener(destination="study.msg.queue")
  25. public void receiveMessage(String text) { // 进行消息接收处理
  26. System.err.println("【*** 接收消息 ***】" + text);
  27. }
  28. }
  1. 4、 随后建立消息的发送者服务,一般而言如果进行消息的发送往往会准备出一个业务接口来:
  2. package com.microboot.producer;
  3. public interface IMessageProducerService {
  4. public void sendMessage(String msg) ;
  5. }
  1. 5、 随后建立一个配置程序类,定义 ActiveMQ 的消息发送模版处理类:
  2. package com.microboot.config;
  3. import javax.jms.Queue;
  4. import org.apache.activemq.command.ActiveMQQueue;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.jms.annotation.EnableJms;
  8. @Configuration
  9. @EnableJms
  10. public class ActiveMQConfig {
  11. @Bean
  12. public Queue queue() {
  13. return new ActiveMQQueue("study.msg.queue");
  14. }
  15. }
  1. 6、 创建消息发送的子类实现消息发送处理:
  2. package com.microboot.producer;
  3. import javax.annotation.Resource;
  4. import javax.jms.Queue;
  5. import org.springframework.jms.core.JmsMessagingTemplate;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class MessageProducerServiceImpl implements IMessageProducerService {
  9. @Resource
  10. private JmsMessagingTemplate jmsMessagingTemplate;
  11. @Resource
  12. private Queue queue;
  13. @Override
  14. public void sendMessage(String msg) {
  15. this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
  16. }
  17. }
  1. 7、 编写测试类来观察消息的处理:
  2. package com.microboot.test;
  3. import javax.annotation.Resource;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. import org.springframework.test.context.web.WebAppConfiguration;
  9. import com.microboot.StartSpringBootMain;
  10. import com.microboot.producer.IMessageProducerService;
  11. @SpringBootTest(classes = StartSpringBootMain.class)
  12. @RunWith(SpringJUnit4ClassRunner.class)
  13. @WebAppConfiguration
  14. public class TestActiveMQ {
  15. @Resource
  16. private IMessageProducerService messageProducer;
  17. @Test
  18. public void testSend() throws Exception {
  19. for (int x = 0; x < 10; x++) {
  20. this.messageProducer.sendMessage("study - " + x);
  21. }
  22. }
  23. }
  24. 基于 SpringBoot 配置的 JMS 的组件访问整体的处理十分简单
  1. 2.2、SpringBoot 整合 RabbitMQ
  2. 如果要进行 RabbitMQ 整合的时候一定要注意以下几个概念:交换空间、虚拟主机、队列信息。
  3. 本次为了方便起见将项目分为 两个:RabbitMQ-Consumer、RabbitMQ-Producer。
  4. 1、 【两个项目】将 rabbitmq 的依赖支持包拷贝到项目之中;
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-amqp</artifactId>
  8. </dependency>
  9. 2、 【microboot-rabbitmq-producer、microboot-rabbitmq-consumer】
  10. 修改 application.properties配置文件,追加 rabbitmq 的相关配置项:
  11. spring.rabbitmq.addresses=127.0.0.1
  12. spring.rabbitmq.username=admin
  13. spring.rabbitmq.password=admin
  1. 3、 【microboot-rabbitmq-producer】建立一个消息的发送接口:
  2. package cn.study.microboot.producer;
  3. public interface IMessageProducerService {
  4. public void sendMessage(String msg) ;
  5. }
  1. 4、 【microboot-rabbitmq-producer】为了可以正常使用 RabbitMQ 进行消息处理,你还需要做一个消息
  2. 生产配置类;
  3. package cn.study.microboot.config;
  4. import org.springframework.amqp.core.Binding;
  5. import org.springframework.amqp.core.BindingBuilder;
  6. import org.springframework.amqp.core.DirectExchange;
  7. import org.springframework.amqp.core.Queue;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. @Configuration
  11. public class ProducerConfig {
  12. public static final String EXCHANGE = "study.microboot.exchange"; // 交换空间名称
  13. public static final String ROUTINGKEY = "study.microboot.routingkey"; // 设置路由key
  14. public static final String QUEUE_NAME = "study.microboot.queue"; // 队列名称
  15. @Bean
  16. public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) {
  17. return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ;
  18. }
  19. @Bean
  20. public DirectExchange getDirectExchange() { // 使用直连的模式
  21. return new DirectExchange(EXCHANGE, true, true);
  22. }
  23. @Bean
  24. public Queue queue() { // 要创建的队列信息
  25. return new Queue(QUEUE_NAME);
  26. }
  27. }
  1. 5、 【microboot-rabbitmq-producer】创建消息服务的实现子类:
  2. package cn.study.microboot.producer.impl;
  3. import javax.annotation.Resource;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.stereotype.Service;
  6. import cn.study.microboot.config.ProducerConfig;
  7. import cn.study.microboot.producer.IMessageProducerService;
  8. @Service
  9. public class MessageProducerServiceImpl implements IMessageProducerService {
  10. @Resource
  11. private RabbitTemplate rabbitTemplate;
  12. @Override
  13. public void sendMessage(String msg) {
  14. this.rabbitTemplate.convertAndSend(ProducerConfig.EXCHANGE,
  15. ProducerConfig.ROUTINGKEY, msg);
  16. }
  17. }
  1. 6、 【microboot-rabbitmq-consumer】依然需要做一个消费者的配置程序类,而这个程序类里面主要的目的
  2. 依然是设置交换空间、 路由 KEY 等信息。
  3. package cn.study.microboot.config;
  4. import org.springframework.amqp.core.Binding;
  5. import org.springframework.amqp.core.BindingBuilder;
  6. import org.springframework.amqp.core.DirectExchange;
  7. import org.springframework.amqp.core.Queue;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. @Configuration
  11. public class ConsumerConfig {
  12. public static final String EXCHANGE = "study.microboot.exchange"; // 交换空间名称
  13. public static final String ROUTINGKEY = "study.microboot.routingkey"; // 设置路由key
  14. public static final String QUEUE_NAME = "study.microboot.queue"; // 队列名称
  15. @Bean
  16. public Queue queue() { // 要创建的队列信息
  17. return new Queue(QUEUE_NAME);
  18. }
  19. @Bean
  20. public DirectExchange getDirectExchange() { // 使用直连的模式
  21. return new DirectExchange(EXCHANGE, true, true);
  22. }
  23. @Bean
  24. public Binding bindingExchangeQueue(DirectExchange exchange,Queue queue) {
  25. return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY) ;
  26. }
  27. }
  1. 7、 【microboot-rabbitmq-consumer】实现监听处理类:
  2. package cn.study.microboot.consumer;
  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  4. import org.springframework.stereotype.Service;
  5. @Service
  6. public class MessageConsumerService {
  7. @RabbitListener(queues="study.microboot.queue")
  8. public void receiveMessage(String text) { // 进行消息接收处理
  9. System.err.println("【*** 接收消息 ***】" + text);
  10. }
  11. }
  1. 9、 【microboot-rabbitmq-consumer】编写消息接收测试类,这里面不需要编写代码,只需要做一个休眠
  2. 即可:
  3. package cn.study.microboot.test;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. import org.springframework.test.context.web.WebAppConfiguration;
  9. import cn.study.microboot.StartSpringBootMain;
  10. @SpringBootTest(classes = StartSpringBootMain.class)
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @WebAppConfiguration
  13. public class AppTest {
  14. @Test
  15. public void testStart() throws Exception {
  16. Thread.sleep(Long.MAX_VALUE);
  17. }
  18. }
  19. 整体进行项目开发之中整合的处理步骤还是简单,但是千万要注意,由于是第一次整合处理,所以将生产者与
  20. 消费者的配置 类分开了,实际上这两个类的作用是完全一样的。
  1. 2.3、SpringBoot 整合 Kafka
  2. Kafka 是现在最好的开源消息组件,其仿照 AMQP 协议操作,而且处理的性能也是最高的。本次使用已经配置
  3. 好的 Kafka 服 务器,而且这台服务器上使用了 kerberos 认证,所以应该首先准备好一个 jass 配置文件:
  4. 1、 定义“kafka_client_jaas.conf”配置文件:
  5. KafkaClient {
  6. org.apache.kafka.common.security.plain.PlainLoginModule required
  7. username="bob"
  8. password="bob-pwd";
  9. };
  1. 2、 为了方便进行项目的观察, 本次依然准备出了两个项目:生产者( microboot-kafka-producer )、
  2. 消 费 者 (microboot-kafka-consumer),随后为这两个项目添加 kafka 配置支持:
  3. <dependency>
  4. <groupId>org.springframework.kafka</groupId>
  5. <artifactId>spring-kafka</artifactId>
  6. </dependency>
  1. 3、 【micorboot-kafka-consumer】修改 application.yml 配置文件,进行 kafka 配置项编写:
  2. server:
  3. port: 80
  4. spring:
  5. messages:
  6. basename: i18n/Messages,i18n/Pages
  7. kafka:
  8. bootstrap-servers:
  9. - kafka-single:9095
  10. template:
  11. default-topic: mldn-microboot
  12. consumer:
  13. key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
  14. value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
  15. group-id: group-1
  16. properties:
  17. sasl.mechanism: PLAIN
  18. security.protocol: SASL_PLAINTEXT
  1. 4、 【micorboot-kafka-consumer】建立一个 Kafka 的消息的消费程序类:
  2. package cn.study.microboot.consumer;
  3. import org.apache.kafka.clients.consumer.ConsumerRecord;
  4. import org.springframework.kafka.annotation.KafkaListener;
  5. import org.springframework.stereotype.Service;
  6. @Service
  7. public class MessageConsumerService {
  8. @KafkaListener(topics = {"study-microboot"})
  9. public void receiveMessage(ConsumerRecord<String, String> record) { // 进行消息接收处理
  10. System.err.println("【*** 接收消息 ***】key = " + record.key() + "、value = "
  11. + record.value());
  12. }
  13. }
  1. 5、 【micorboot-kafka-consumer】随后还需要修改 SpringBoot 的启动程序类,追加 kerberos 配置:
  2. package cn.study.microboot;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
  6. public class StartSpringBootMain {
  7. static {
  8. System.setProperty("java.security.auth.login.config",
  9. "d:/kafka_client_jaas.conf"); // 表示系统环境属性
  10. }
  11. public static void main(String[] args) throws Exception {
  12. SpringApplication.run(StartSpringBootMain.class, args);
  13. }
  14. }
  1. 6、 【microboot-kafka-producer】修改 application.yml 配置文件:
  2. server:
  3. port: 80
  4. spring:
  5. messages:
  6. basename: i18n/Messages,i18n/Pages
  7. kafka:
  8. bootstrap-servers:
  9. - kafka-single:9095
  10. template:
  11. default-topic: mldn-microboot
  12. producer:
  13. key-serializer: org.apache.kafka.common.serialization.StringSerializer
  14. value-serializer: org.apache.kafka.common.serialization.StringSerializer
  15. properties:
  16. sasl.mechanism: PLAIN
  17. security.protocol: SASL_PLAINTEXT
  1. 7、 【microboot-kafka-producer】定义消息发送的服务接口:
  2. package cn.study.microboot.producer;
  3. public interface IMessageProducerService {
  4. public void sendMessage(String msg) ;
  5. }
  6. package cn.study.microboot.service.impl;
  7. import javax.annotation.Resource;
  8. import org.springframework.kafka.core.KafkaTemplate;
  9. import org.springframework.stereotype.Service;
  10. import cn.study.microboot.service.IMessageProducerService;
  11. @Service
  12. public class MessageProducerServiceImpl implements IMessageProducerService {
  13. @Resource
  14. private KafkaTemplate<String, String> kafkaTemplate;
  15. @Override
  16. public void send(String msg) {
  17. this.kafkaTemplate.sendDefault("study-key", msg);
  18. }
  19. }
  1. 8、 【microboot-kafka-producer】修改程序启动类:
  2. package cn.mldn.microboot;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
  6. public class StartSpringBootMain {
  7. static {
  8. System.setProperty("java.security.auth.login.config",
  9. "d:/kafka_client_jaas.conf"); // 表示系统环境属性
  10. }
  11. public static void main(String[] args) throws Exception {
  12. SpringApplication.run(StartSpringBootMain.class, args);
  13. }
  14. }
  1. 9、 【microboot-kafka-producer】编写消息发送的程序类:
  2. package cn.study.microboot;
  3. import javax.annotation.Resource;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. import org.springframework.test.context.web.WebAppConfiguration;
  9. import cn.study.microboot.service.IMessageProducerService;
  10. @SpringBootTest(classes = StartSpringBootMain.class)
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @WebAppConfiguration
  13. public class TestMessageService {
  14. @Resource
  15. private IMessageProducerService messageService;
  16. @Test
  17. public void testStart() throws Exception {
  18. for (int x = 0; x < 100; x++) {
  19. this.messageService.send("study - " + x);
  20. }
  21. }
  22. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/688457
推荐阅读
相关标签
  

闽ICP备14008679号