Spring Boot整合Rabbitmq
Spring Boot应用中整合RabbitMQ,并实现一个简单的发送、接收消息的例子来对RabbitMQ有一个直观的感受和理解。
在Spring Boot中整合RabbitMQ是一件非常容易的事,因为之前我们已经介绍过Starter POMs,其中的AMQP模块就可以很好的支持RabbitMQ,下面我们就来详细说说整合过程:
- 新建一个Spring Boot工程,命名为:“rabbitmq-hello”。
- 在
pom.xml
中引入如下依赖内容,其中spring-boot-starter-amqp
用于支持RabbitMQ。
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.dxz</groupId>
- <artifactId>rabbitmq-hello</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>rabbitmq-hello</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.7.RELEASE</version>
- <relativePath /> <!-- lookup parent from repository -->
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
- 在
application.properties
中配置关于RabbitMQ的连接和用户信息,用户可以回到上面的安装内容,在管理页面中创建用户。
- spring.application.name=rabbitmq-hello
- spring.rabbitmq.host=localhost
- spring.rabbitmq.port=5672
- spring.rabbitmq.username=guest
- spring.rabbitmq.password=guest
- 创建消息生产者
Sender
。通过注入AmqpTemplate
接口的实例来实现消息的发送,AmqpTemplate
接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello
的队列中。
- package com.dxz.rabbitmq_hello;
- import java.util.Date;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- @Component
- public class Sender {
- @Autowired
- private AmqpTemplate rabbitTemplate;
- public void send() {
- String context = "hello " + new Date();
- System.out.println("---------------Sender : " + context);
- this.rabbitTemplate.convertAndSend("hello", context);
- }
- }
- 创建消息消费者
Receiver
。通过@RabbitListener
注解定义该类对hello
队列的监听,并用@RabbitHandler
注解来指定对消息的处理方法。所以,该消费者实现了对hello
队列的消费,消费操作为输出消息的字符串内容。
- package com.dxz.rabbitmq_hello;
- import org.springframework.amqp.rabbit.annotation.RabbitHandler;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
- @Component
- @RabbitListener(queues = "hello")
- public class Receiver {
- @RabbitHandler
- public void process(String hello) {
- System.out.println("Receiver : " + hello);
- }
- }
- 创建RabbitMQ的配置类
RabbitConfig
,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。
- package com.dxz.rabbitmq_hello;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class RabbitConfig {
- @Bean
- public Queue helloQueue() {
- return new Queue("hello");
- }
- }
- 创建应用主类:
- package com.dxz.rabbitmq_hello;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class HelloApplication {
- public static void main(String[] args) {
- SpringApplication.run(HelloApplication.class, args);
- }
- }
- 创建单元测试类,用来调用消息生产:
- package com.dxz.rabbitmq_hello;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.SpringApplicationConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = HelloApplication.class)
- public class HelloApplicationTests {
- @Autowired
- private Sender sender;
- @Test
- public void hello() throws Exception {
- sender.send();
- sender.send();
- sender.send();
- }
- }
启动应用主类,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672
中springcloud
的连接。完成程序编写之后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,然后进行下面的操作:
1
|
o.s.a.r.c.CachingConnectionFactory : Created new connection: SimpleConnection@29836d32 [delegate=amqp://springcloud@127.0.0.1:5672/]
|
同时,我们通过RabbitMQ的控制面板,可以看到Connection和Channels中包含当前连接的条目。
- 运行单元测试类,我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Server的
hello
队列中。
- 切换到应用主类的控制台,我们可以看到类似如下输出,消费者对
hello
队列的监听程序执行了,并输出了接受到的消息信息。
通过上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp
模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,我们还有很多内容没有演示,这里不做更多的讲解,读者可以自行查阅RabbitMQ的官方教程,有更全面的了解。
Spring Boot整合Rabbitmq的更多相关文章
- Spring Boot (十三): Spring Boot 整合 RabbitMQ
1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...
- Spring Boot 整合 rabbitmq
一.消息中间件的应用场景 异步处理 场景:用户注册,信息写入数据库后,需要给用户发送注册成功的邮件,再发送注册成功的邮件. 1.同步调用:注册成功后,顺序执行发送邮件方法,发送短信方法,最后响应用户 ...
- spring boot整合RabbitMQ(Direct模式)
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. Direct Excha ...
- spring boot 2.x 系列 —— spring boot 整合 RabbitMQ
文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(rabbitmq-common) 四.服务消费者(rabbitmq-consumer) 4.1 消息消费者配置 4.2 使用注解@Rabbit ...
- spring boot 整合 RabbitMQ 错误
1.错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spr ...
- Spring Boot整合RabbitMQ详细教程
原文:https://blog.csdn.net/qq_38455201/article/details/80308771 1.首先我们简单了解一下消息中间件的应用场景 异步处理 场景说明:用户注册后 ...
- spring boot 整合 RabbitMq (注解)
1.增加rabbitmq的依赖包 <!-- ampq 依赖包 --> <dependency> <groupId>org.springframework.boot& ...
- spring boot整合RabbitMQ(Fanout模式)
1.Fanout Exchange介绍Fanout Exchange 消息广播的模式,不管路由键或者是路由模式,会把消息发给绑定给它的全部队列,如果配置了routing_key会被忽略. 如上图所示, ...
- spring boot整合RabbitMQ(Topic模式)
1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...
随机推荐
- linux下安装编译网卡驱动的方法
安装linux操作系统后发现没有网卡驱动,表现为 system → Administration → Network下Hardware列表为空. 以下为安装编译网卡驱动的过程,本人是菜鸟,以下是我从网 ...
- LeetCode——max-points-on-a-line
Question Given n points on a 2D plane, find the maximum number of points that lie on the same straig ...
- BASE64Encoded() 方法报错说方法未定义
代码: String enParams = new BASE64Encoder().encode(strParams.getBytes()); 出错,显示方法未定义 解决方法:项目右键——>pr ...
- HTTP通信模拟表单提交数据
前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...
- 免配置环境变量使用Tomcat+设置项目主页路径为http://localhost:8080+修改tomcat端口号
一.免配置jdk JAVA_HOME和tomcat CATALINA_HOME环境变量使用tomcat 众说周知,使用tomcat需要有java环境,一般情况下需要配置jdk和tomcat的路径到w ...
- 关于接口自动化的那些事 - 基于 Python
网络请求模拟小技巧 在学习了一段时间的Python语言后,咱也大概对Python的语法和逻辑有了一定的积累,接下来,可以为接口自动化测试开始尝试做一些比较简单的准备工作啦~跟着我一起来来来~ 扩展库r ...
- IOS开发--解析复杂json数据
json的自我介绍:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅 ...
- c# 判断一个ip通不通 能不能ping通
方法一: 已经证实能用的. using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...
- XSS 跨站脚本攻击实例1
14.44-16.22 编码,跨站脚本攻击1 16.22-16.53 整理cnblog 这篇文章适合知道有XSS脚本攻击,但是一头雾水,从未操作过,也不知道脚本攻击会给客户端用户带来什么不便之处 ...
- javascript 跨域问题解决办法总结
跨域的意思就是不同域名之间的页面默认是无法通信的.因为浏览器默认是禁止跨域的: 图所示:chrome浏览器尝试获取mainFrame失败,提示DomException 1).假如你有个网站 a.com ...