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 ...
随机推荐
- 06 使用bbed提交delete的数据--01
使用bbed模拟delete提交操作 --session 1 TEST@ orcl )); Table created. TEST@ orcl ,'AAAAA'); row created. TEST ...
- Appium-入门实例1
参考:(https://blog.csdn.net/zh175578809/article/details/76862590) 第一步:启动虚拟设备 在运行App之前,首先需要创建一个Android模 ...
- 监听器 ServletRequestAttributeListener&ServletRequestListener详解
在web开发中,监听器不仅可以对Application监听,同时还可以对seesion和request对象进行监听: 该文章主要演示的是对request对象的创建和request属性的监听. 项目结构 ...
- 小白学Python(18)——pyecharts 关系图 Graph
Graph-基本示例 import json import os from pyecharts import options as opts from pyecharts.charts import ...
- C++对象构造时,构造函数运行时并不知道VT的存在
class A {public: A() { init(); } virtual void init() { printf("A::init\n"); }}; class B : ...
- wxpython程序基本功能源码整理,包括基本文字,输入框,字体设置,按钮绑定事件触发
#coding=utf-8 import wx class MyApp(wx.App): def __init__(self): wx.App.__init__(self) def OnInit(se ...
- 16、前端知识点--Object.defineProperty 的用法+双向数据绑定原理解析
一.Object.defineProperty 的用法 Object.defineProperty 可以用于给对象添加更新属性. <script> // Object.defineProp ...
- vue主动刷新页面及列表数据删除后的刷新方法
在继刷新当前页面,重载页面数据那篇之后 那一篇 深入理解数据驱动 以上算是开发过程中的一个坑,用了一段时间,今天再读代码的时候,感觉被坑的很严重. 1. 获取列表方法 2.重新获取数据 3.这样再次调 ...
- jQuery学习总结01-选择器
下面简单介绍一个常用的jQuery使用方法,其他详细的猛戳这里 一.选择器 1.parent > child 说明:在给定父类的情况下匹配所有的子类 示例: 描述:匹配表单中所有的的子级inpu ...
- windows下使用命令行获取管理员权限
在win下运行npm install安装依赖出现错误: Error: EBUSY, resource busy or locked 搜索错误信息后发现是由于没有管理员权限,在bash中输入以下命令后运 ...