SpringBoot的RabbitMQ消息队列: 一、消息发送接收第一印象

本文主要是介绍SpringBoot的RabbitMQ消息队列: 一、消息发送接收第一印象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.在原来项目的基础上.

修改pom.xml配置,导入jar包

增加配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.

修改系统配置文件application.properties,编制RabbitMQ的链接参数

spring.application.name=rabbitmq-hello
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456


ps:这里特别注意,mq的账号和密码都正常,还提示连接不上,这个时候就需要注意改账号的权限问题.

在打开localhost:15672 使用上述配置的账号密码登陆,进入admin选项卡,查看是否有权限,

Can access virtual hosts
 看这个是否有赋予对应的权限.....没有则需要加上,不然是访问不到的.


编制RabbitMQ配置、发送、接受的代码

1、编写配置文件类

在com.example包中增加类,名称为HelloRabbitConfig,并修改代码为

[java]  view plain copy
  1. package com.example;  
  2.   
  3. import org.springframework.amqp.core.Queue;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. @Configuration  
  8. public class HelloRabbitConfig {  
  9.   
  10.     @Bean  
  11.     public Queue helloQueue() {  
  12.         return new Queue("hello");  
  13.     }  
  14. }  

2、编写发送消息类

在com.example包中增加类,名称为HelloSender,并修改代码为

[java]  view plain copy
  1. package com.example;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7. import org.springframework.amqp.core.AmqpTemplate;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. @Component  
  12. public class HelloSender {  
  13.   
  14.     protected static Logger logger=LoggerFactory.getLogger(HelloSender.class);   
  15.       
  16.     @Autowired  
  17.     private AmqpTemplate rabbitTemplate;  
  18.   
  19.     public String send(String name) {  
  20.         String context = "hello "+name+" --" + new Date();  
  21.         logger.debug("HelloSender: " + context);  
  22.         this.rabbitTemplate.convertAndSend("hello", context);  
  23.         return context;  
  24.     }  
  25. }  

3、编写接受消息类

在com.example包中增加类,名称为HelloReceiver,并修改代码为

[html]  view plain copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.rabbit.annotation.RabbitHandler;  
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. @RabbitListener(queues = "hello")  
  11. public class HelloReceiver {  
  12.     protected static Logger logger = LoggerFactory.getLogger(HelloReceiver.class);  
  13.   
  14.     @RabbitHandler  
  15.     public void process(String hello) {  
  16.         logger.debug("HelloReceiver : " + hello);  
  17.     }  
  18. }  

4、编写RestController,调用发送消息

在com.example包中增加类,名称为HelloController,并修改代码为

[java]  view plain copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @RestController   
  11. public class HelloController {  
  12.     protected static Logger logger=LoggerFactory.getLogger(HelloController.class);   
  13.       
  14.     @Autowired  
  15.         private HelloSender helloSender;  
  16.       
  17.     @RequestMapping("/send/{name}")  
  18.     public String helloworld(@PathVariable String name) {  
  19.         return helloSender.send(name);  
  20.     }  
  21. }  

5、运行测试

A、启动Docker中容器rabbitmq5672。docker start rabbitmq5672   (如果已启动,则忽略)

B、启动工程。在工程所在的文件夹打开命令行,且在命令行中执行 mvn spring-boot:run

C、在浏览器中输入 http://localhost:8080/send/上帝

浏览器中显示

控制台中显示,成功发送、接收消息

三、小结

1、rabbitmq连接

1.1 springboot,在启动时,根据系统配置文件的参数建立一个rabbitmq连接。参看控制台日志:

 (MBeanExporter.java:431)- Registering beans for JMX exposure on startup

(MBeanExporter.java:916)- Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure

 (MBeanExporter.java:678)- Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]

(DefaultLifecycleProcessor.java:343)- Starting beans in phase -2147482648

(DefaultLifecycleProcessor.java:343)- Starting beans in phase 2147483647

(AbstractConnectionFactory.java:347)- Created new connection: SimpleConnection@7075a8f1 [delegate=amqp://test@127.0.0.1:5672/, localPort= 62881]

1.2、在springboot启动之后,查看rabbitmq(http://localhost:15672/)的connections,也可以查看到。当然,你也可以停止springboot去看链接(停止之后,当然就没有链接了).
1.3、把logbak.xml的root的等级修改为debug,你会发现监听在不断的链接reabitmq;它就是一个心跳。通过这个心跳,rabbitmq也知道存在哪些监听器在监听那个队列。

2、配置文件

在配置文件中,仅仅配置了一个名为hello的队列。以后发送、接收都与这个队列相关。

3、发送消息

3.1、发送消息使用模板机制,用springboot自动注入的rabbitTemplate,它已经封装好链接、管道、转换等。

3.2、消息转换和发送

void convertAndSend(String routingKey, Object message) throws AmqpException;

它的注释是:Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.

转换一个Java对象为Amqp消息,然后再用缺省的交换机指定路由键发送消息。

4、接受消息

4.1、监听消息

在类上声明@RabbitListener(queues = "hello"),表示监听hello队列

4.2、消息处理句柄

在接收处理消息的方法上声明@RabbitHandler

Annotation that marks a method to be the target of a Rabbit message  listener within a class that is annotated with {@link RabbitListener}.

5、印象

整个工程比较简单,没有什么特殊的配置,仅仅三个类文件即可实现消息的发送和接受。





这篇关于SpringBoot的RabbitMQ消息队列: 一、消息发送接收第一印象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1095396

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与