Spring BOOT 集成 RabbitMq 实战操作(一)

简介:

RabbitMq消息消费者服务 

开发工具Idea和Spring boot来开发的。


消息消费目前只是一个简单的Demo,后续会处理成更智能一些。


首先配置文件类,RabbitMqConfig,里面配置一些用户名和密码嗨哟队列信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package  com.basic.rabbitmq.consumer.config;
 
import  com.basic.rabbitmq.consumer.listener.HandleMessageListenerAdapter;
import  org.springframework.amqp.core.*;
import  org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import  org.springframework.amqp.rabbit.core.RabbitAdmin;
import  org.springframework.amqp.rabbit.core.RabbitTemplate;
import  org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import  org.springframework.beans.factory.annotation.Qualifier;
import  org.springframework.context.annotation.Bean;
import  org.springframework.core.env.Environment;
import  com.rabbitmq.client.ConnectionFactory;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.context.annotation.ComponentScan;
import  org.springframework.context.annotation.Configuration;
import  org.springframework.context.annotation.PropertySource;
 
/**
  * Rabbitmq配置类
  * Created by sdc on 2017/7/4.
  */
@Configuration
@ComponentScan (basePackages = { "com.basic" })
@PropertySource (value = { "classpath:application.properties" })
public  class  RabbitMqConfig {
 
     @Autowired
     private  Environment env;
 
     /**
      * 构建connectionfactory
      * @return
      * @throws Exception
      */
     @Bean
     public  ConnectionFactory connectionFactory()  throws  Exception {
         ConnectionFactory connectionFactory =  new  ConnectionFactory();
         connectionFactory.setHost(env.getProperty( "spring.rabbitmq.host" ));
         connectionFactory.setPort(Integer.valueOf( "5672" .trim()));
         connectionFactory.setVirtualHost( "/" );
         connectionFactory.setUsername(env.getProperty( "spring.rabbitmq.username" ));
         connectionFactory.setPassword(env.getProperty( "spring.rabbitmq.password" ));
         return  connectionFactory;
     }
 
     /**
      * CachingConnectionFactory
      * @return
      * @throws Exception
      */
     @Bean
     public  CachingConnectionFactory cachingConnectionFactory()  throws  Exception {
         return  new  CachingConnectionFactory(connectionFactory());
     }
 
     /**
      * RabbitTemplate,类似于jdbctemplate一样的工具类
      * @return
      * @throws Exception
      */
     @Bean
     public  RabbitTemplate rabbitTemplate()  throws   Exception {
         RabbitTemplate rabbitTemplate =  new  RabbitTemplate(cachingConnectionFactory());
         rabbitTemplate.setChannelTransacted( true );
         return  rabbitTemplate;
     }
 
     @Bean
     public  AmqpAdmin amqpAdmin()  throws   Exception {
         return  new  RabbitAdmin(cachingConnectionFactory());
     }
 
     @Bean
     public  SimpleMessageListenerContainer listenerContainer(
             @Qualifier ( "handleMessageListenerAdapter" ) HandleMessageListenerAdapter handleMessageListenerAdapter)  throws  Exception {
         //队列名字
         String queueName = env.getProperty( "emial.server.queue" ).trim();
 
         //单一的消息监听容器
         SimpleMessageListenerContainer simpleMessageListenerContainer =
                 new  SimpleMessageListenerContainer(cachingConnectionFactory());
         simpleMessageListenerContainer.setQueueNames(queueName);
         simpleMessageListenerContainer.setMessageListener(handleMessageListenerAdapter);
         //手动设置 ACK,就是成功消费信息了,就设置一下这个,rabbitmq就从此队列里删除这条信息了。
         simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
 
         return  simpleMessageListenerContainer;
     }
 
 
}


我这里配置了一个SimpleMessageListenerContainer,这个Bean,用来监听队列里的消息的。


具体的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.basic.rabbitmq.consumer.listener;
 
         import  com.rabbitmq.client.Channel;
         import  org.springframework.amqp.core.Message;
         import  org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
         import  org.springframework.beans.factory.annotation.Autowired;
         import  org.springframework.context.annotation.ComponentScan;
         import  org.springframework.mail.MailMessage;
         import  org.springframework.mail.javamail.JavaMailSender;
         import  org.springframework.stereotype.Component;
 
         import  javax.annotation.Resource;
 
/**
  * 监听消息的处理适配器
  * Created by sdc on 2017/7/10.
  */
@Component ( "handleMessageListenerAdapter" )
public  class  HandleMessageListenerAdapter  extends  MessageListenerAdapter {
 
//    @Resource
//    private JavaMailSender mailSender;
 
     /**
      * 这块和activemq那个监听器差不多,都是监听信息,也都是onMessage方法。
      * @param message
      * @param channel
      * @throws Exception
      */
     @Override
     public  void  onMessage(Message message, Channel channel)  throws  Exception {
         String messageDetail =  new  String(message.getBody());  //消息体
         System.out.println( "消息消费:"  + messageDetail);
 
         // 手动ACK
         channel.basicAck(message.getMessageProperties().getDeliveryTag(),  false );
     }
}


还有一些配制文件,请看

http://10103778.blog.51cto.com/10093778/1945756

这个博客,就可以看到具体的配制了。


启动这个项目,就可以从队列消费消息了。消费者还是比较简单的,对应到相应的队列就可以处理了消息了。



本文转自 豆芽菜橙 51CTO博客,原文链接:http://blog.51cto.com/shangdc/1945974


相关实践学习
快速体验阿里云云消息队列RocketMQ版
本实验将带您快速体验使用云消息队列RocketMQ版Serverless系列实例进行获取接入点、创建Topic、创建订阅组、收发消息、查看消息轨迹和仪表盘。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
12月前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
523 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
11月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1496 2
Spring Boot 3.x 微服务架构实战指南
|
12月前
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
3189 1
|
11月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
12月前
|
消息中间件 Java Kafka
消息队列比较:Spring 微服务中的 Kafka 与 RabbitMQ
本文深入解析了 Kafka 和 RabbitMQ 两大主流消息队列在 Spring 微服务中的应用与对比。内容涵盖消息队列的基本原理、Kafka 与 RabbitMQ 的核心概念、各自优势及典型用例,并结合 Spring 生态的集成方式,帮助开发者根据实际需求选择合适的消息中间件,提升系统解耦、可扩展性与可靠性。
764 1
消息队列比较:Spring 微服务中的 Kafka 与 RabbitMQ
|
12月前
|
消息中间件 存储 Java
RabbitMQ 和 Spring Cloud Stream 实现异步通信
本文介绍了在微服务架构中,如何利用 RabbitMQ 作为消息代理,并结合 Spring Cloud Stream 实现高效的异步通信。内容涵盖异步通信的优势、RabbitMQ 的核心概念与特性、Spring Cloud Stream 的功能及其与 RabbitMQ 的集成方式。通过这种组合,开发者可以构建出具备高可用性、可扩展性和弹性的分布式系统,满足现代应用对快速响应和可靠消息传递的需求。
614 2
RabbitMQ 和 Spring Cloud Stream 实现异步通信
|
12月前
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
951 0