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 ...
随机推荐
- django基于odm,简单的post和get封装
- 怎么用 pytorch 查看 GPU 信息
如果你用的 Keras 或者 TensorFlow, 请移步 怎么查看keras 或者 tensorflow 正在使用的GPU In [1]: import torch In [2]: torch.c ...
- Vue2.0---将页面中表格数据导出excel
这不是教程,是随笔. 项目中将后台返回的数据v-for到表格中,然后需要将这个表格导出为EXCEL 只说怎么做. 一.需要安装三个依赖: npm install -S file-saver xlsx ...
- web 前端2 CSS
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...
- python字典-基础
一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...
- C# 判断文件夹与文件是否存在
//在上传文件时经常要判断文件夹是否存在,如果存在就上传文件,否则新建文件夹再上传文件 判断语句为 if (System.IO.Directory.Exists(Server.MapPath(&quo ...
- Azkaban 2.5.0的详细安装过程
准备下载Azkaban2.5.0:https://azkaban.github.io/downloads.htm 准备插件: 一.MySQL安装与配置 启动数据库并查看状态:sudo service ...
- 第二讲shiro异常及执行流程
在认证过程中,有一个父异常为:AuthenticationException 该异常有几个子类,分别对应不同的异常情况: (1)DisabledAccountException:账户失效异常 (2)E ...
- ulimit 管理系统资源
具体的 options 含义以及简单示例可以参考以下表格. 选项 含义 例子 -H 设置硬资源限制,一旦设置不能增加. ulimit – Hs 64:限制硬资源,线程栈大小为 64K. -S 设置软资 ...
- Lamabda Where Select Find First等区别
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...