Spring Boot 集成 RabbitMQ 实战
Spring Boot 集成 RabbitMQ 实战
特别说明:
本文主要参考了程序员 DD 的博客文章《Spring Boot中使用RabbitMQ》,在此向原作者表示感谢。
Mac 上 RabbitMQ 的安装

这样子安装的话,RabbitMQ 的脚本是安装到 /usr/local/sbin 这个目录里的,并且不会自动添加到你的 PATH 里,所以要先添加下。
PATH=$PATH:/usr/local/sbin
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
补充说明:sublime .zshrc ,.zshrc 这个文件可以配置环境变量。
启动命令
rabbitmq-server
我们将会看到:

在浏览器中输入 http://localhost:15672/,用户名和密码都是 guest 。
Spring Boot 集成 RabbitMQ 实战
下面我们使用 Spring Boot 集成 RabbitMQ 模块,初步体验一下 MQ。以下的例子只是一个 HelloWorld ,让我们简单认识 RabbitMQ 的发送消息和接收消息。
示例代码:
特别注意:Gradle 构建中一定要使用 spring-boot 的 plugin。
添加 Spring-Boot 插件的方法
在 build.gradle 文件中添加如下配置片段:
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
// NOTE: You should declare only repositories that you need here
mavenLocal()
mavenCentral()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'idea'
apply plugin: 'spring-boot'
补充说明:如果想设置阿里巴巴的 nexus 仓库为中央仓库,可以把如下的代码片段粘贴到 build.gradle 文件的第 1 行:
allprojects {
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
}
1、引入 spring-boot-starter-amqp 模块;
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '1.3.0.M2'
}
还可以配置 application.properties
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
2、编写 RabbitMQ 的配置类
/**
* 特别要说明的是:如果在配置文件中声明了 Queue 对象,就不用在 RabbitMQ 的管理员页面创建队列(Queue)了。
* Created by liwei on 17/3/12.
*/
@Configuration
public class MqConfig {
/**
* 声明接收字符串的队列
*
* @return
*/
@Bean
public Queue stringQueue() {
return new Queue("my-queue");
}
/**
* 声明接收 User 对象的队列
*
* @return
*/
@Bean
public Queue userQueue() {
return new Queue("my-user");
}
}
补充说明:RabbitMQ 管理平台添加队列的方法。

特别要说明的是:如果在配置文件中声明了 Queue 对象,就不用在 RabbitMQ 的管理员页面创建队列(Queue)了。
3、配置 RabbitMQ 的监听器(即接收 MQ 消息的配置)
很简单,使用 @RabbitListener(queues = "my-queue") 注解就可以了。
@Component
public class Receiver {
@RabbitListener(queues = "my-queue")
public void receiveMessage(String message){
System.out.println("接收到的字符串消息是 => " + message);
}
@RabbitListener(queues = "my-user")
public void receiveObject(User user){
System.out.println("------ 接收实体对象 ------");
System.out.println("接收到的实体对象是 => " + user);
}
}
4、编写实体类
特别要注意:实体类一定要实现序列化接口,才可以在网络中传输。
/**
* 特别注意:实体类一定要实现序列化接口,才可以在网络中传输
* Created by liwei on 17/4/25.
*/
public class User implements Serializable {
private final static Long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
5、编写启动类
/**
* 用于测试的控制类
* Created by liwei on 17/3/12.
*/
@RestController
public class SendMQController {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* http://localhost:8080/send?message=hello
*
* @param message
* @return
*/
@RequestMapping("/send")
public String sendMQ(String message) {
rabbitTemplate.convertAndSend("my-queue", message);
return "ok";
}
/**
* http://localhost:8080/send/user
*
* @return
*/
@RequestMapping("/send/user")
public String sendUser() {
User user = new User();
user.setId(1);
user.setUsername("liwei");
user.setPassword("123456");
rabbitTemplate.convertAndSend("my-user", user);
return "OK";
}
}
6、测试方法
启动 MyRabbitMqApplication 的 Main 方法,容器启动以后,访问:
http://localhost:8080/send?message=hello
http://localhost:8080/send/user
观察控制台的输出。
源代码在 GitHub 上的下载地址:
https://github.com/weimingge14/03-springboot-rabbitmq
Spring Boot 集成 RabbitMQ 实战的更多相关文章
- Spring boot集成RabbitMQ(山东数漫江湖)
RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...
- 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...
- Spring Boot集成Shiro实战
Spring Boot集成Shiro权限验证框架,可参考: https://shiro.apache.org/spring-boot.html 引入依赖 <dependency> < ...
- Spring Boot 集成 Elasticsearch 实战
最近有读者问我能不能写下如何使用 Spring Boot 开发 Elasticsearch(以下简称 ES) 相关应用,今天就讲解下如何使用 Spring Boot 结合 ES. 可以在 ES 官方文 ...
- RabbitMQ(3) Spring boot集成RabbitMQ
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. 资源代码:练习用的代码. ...
- spring boot集成RabbitMQ
原文:https://www.jianshu.com/p/e1258c004314 RabbitMQ作为AMQP的代表性产品,在项目中大量使用.结合现在主流的spring boot,极大简化了开发过程 ...
- Spring Boot 集成RabbitMQ
在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送.接收消息的例子. 首先需要启动RabbitMQ服务,并且add一 ...
- spring boot 集成 rabbitmq 指南
先决条件 rabbitmq server 安装参考 一个添加了 web 依赖的 spring boot 项目 我的版本是 2.5.2 添加 maven 依赖 <dependency> &l ...
- spring boot 集成 rabbitmq
1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...
随机推荐
- C#后台获取日期:当天、前七天、后七天
DateTime.Now.ToString(); //当前时间DateTime.Now.AddDays(7).ToString(); //当前时间加上7天DateTime.Now.AddDays(-7 ...
- pytony格式化输出-占位符
1. %s s = string 字符串 2. %d d = digit 整数 3. %f f = float 浮点数 #!/usr/bin/env python #_*_coding:utf-8_* ...
- Docker中使用多阶段Dockerfile构建容器镜像image(镜像优化)
使用多阶段构建 预计阅读时间: 6分钟 多阶段构建是守护程序和客户端上需要Docker 17.05或更高版本的新功能.多阶段构建对于那些努力优化Dockerfiles同时使其易于阅读和维护的人来说非常 ...
- [随笔重写] Python3 的深拷贝与浅拷贝
1. Python3 关于深浅拷贝的官方文档 文档地址:Python3.7.2 源码地址:lib/copy.py 2. 先说结论 深拷贝与浅拷贝是对复合对象而言的 深拷贝会构造一个新的复合对象,然后递 ...
- JAVA10以上版本 搜索不到 dt.jar和tools.jar
从jdk-9之后就已经没有tools.jar和dt.jar了,也不需要在classpath里面配置这些jar了,配置可参考:JAVA_HOME=jdk安装路径JRE_HOME=jre安装路径PATH= ...
- C++ Lambda 表达式使用详解
转载自: http://www.codeceo.com/article/cpp-lambda.html C++ 11 对LB的支持,对于喜欢Functional Programming的人来说,无疑 ...
- 数组方法之reduce实践
Array.prototype.reduce let arr = [1, 2, 3, 4], sum = arr.reduce((prev, curr, index, arr) => { ret ...
- <每日一题> Day3:CodeForces-1141B.MaximalContinuousRest(简单题)
题目链接 参考代码: #include <iostream> #include <algorithm> using namespace std; + ; int value[m ...
- git多账号配置,同时使用多个代码托管平台
git多账号配置,同时使用多个代码托管平台:https://blog.csdn.net/pinnuli/article/details/81293071
- Spring Cloud部署+Mybatis整合
一:架构简介 Spring Cloud是微服务思想的体现.每个项目单独部署,我只需要知道你服务的name就能直接调用你,而不关心你的ip和端口的变化.当接口服务不可用的时候,我能感知到你无法用了,就不 ...