springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持。

资源代码:练习用的代码。

https://github.com/xiaozhuanfeng?tab=repositories

简单使用

1、配置pom包,主要是添加spring-boot-starter-amqp的支持

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

2、配置文件(生产者,消费者都配置)

配置rabbitmq的安装地址、端口以及账户信息

#RabbitMq
spring.application.name=Spring-boot-rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=eric
spring.rabbitmq.password=eric

3、对列配置(生产者配置)

package com.example.demo.rabbitMq.demo;

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 Queue() {
return new Queue("mesgQue");
}
}

注意:new Queue("String"),源码:

durable  持久化

exclusive 独占,只能被一个Consumer的conn使用

auto_delete 自动删除,在最后一个consumer断开连接后自动删除

  • durable属性和auto-delete属性可以同时生效
  • durable属性和exclusive属性会有性质上的冲突,两者同时设置时,仅exclusive属性生效
  • auto_delete属性和exclusive属性可以同时生效

4、生产者:在工程rabbitMqProj1编写:

package com.example.demo.rabbitMq.demo;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class HelloSender2 {
@Autowired
private AmqpTemplate rabbitTemplate; public void send(int i) {
String context = "hello world....." + i;
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("mesgQue", context);
}
}

5、消费者:在工程rabbitMqProj2编写:

package com.example.demo.rabbitMq.demo;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(queues = "mesgQue")
public class HelloReceiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver1 : " + hello);
}
}
package com.example.demo.rabbitMq.demo;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(queues = "mesgQue")
public class HelloReceiver2 {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver2 : " + hello);
}
}

注意:参数类型一定要与生产者发送的类型一致,否则当生产者消息放入队列时,消费者工程将报异常,No method found for class com.example.demo.dto.User

6、测试:

6.1、生产者

  @Test
public void oneSenderToManyRec() throws Exception {
for (int i=0;i<100;i++){
helloSender2.send(i);
}
}

执行后,在RabbitMQ控制台查看,如图:

此时或许会报错:Socket Closed,需要设置virtual hosts,因为eric现在有登录后台的权限,但是没有管理队列的权限。

解决:

具体参照:https://blog.csdn.net/wabiaozia/article/details/53791366

Virtual Hosts管理:
RabbitMQ中可以虚拟消息服务器VirtualHost,每个VirtualHost相当月一个相对独立的RabbitMQ服务器,每个VirtualHost之间是相互隔离的。exchange、queue、message不能互通。 相当于mysql的db。
所以可以在控制台新增一个virtual host :eric,一个队列,注意用户时eric,注意这里需要重新指定虚拟主机:
spring.rabbitmq.virtualHost=eric

然后调整下代码中的队列名,试下,是可以正常执行的。。。

6.2、启动消费端,发现可以接收到数据,再看控制台,此时队列数据已经消费了。

RabbitMQ(3) Spring boot集成RabbitMQ的更多相关文章

  1. Spring Boot 集成 RabbitMQ 实战

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

  2. Spring boot集成RabbitMQ(山东数漫江湖)

    RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...

  3. 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

    这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...

  4. spring boot集成RabbitMQ

    原文:https://www.jianshu.com/p/e1258c004314 RabbitMQ作为AMQP的代表性产品,在项目中大量使用.结合现在主流的spring boot,极大简化了开发过程 ...

  5. Spring Boot 集成RabbitMQ

    在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送.接收消息的例子. 首先需要启动RabbitMQ服务,并且add一 ...

  6. spring boot 集成 rabbitmq 指南

    先决条件 rabbitmq server 安装参考 一个添加了 web 依赖的 spring boot 项目 我的版本是 2.5.2 添加 maven 依赖 <dependency> &l ...

  7. spring boot 集成 rabbitmq

    1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...

  8. spring boot 集成RabbitMQ的异常

    com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.clos ...

  9. RabbitMQ(三):RabbitMQ与Spring Boot简单整合

    RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.Spring Boot的兴起,极大地简化了Spring的开发,本文将使用Spring Boot与RabbitM ...

随机推荐

  1. 43-2-CAN协议

    1.帧的种类 通信是通过以下 5 种类型的帧进行的. • 数据帧 • 遥控帧 • 错误帧 • 过载帧 • 帧间隔 另外, 数据帧和遥控帧有标准格式和扩展格式两种格式.标准格式有 11 个位的标识符(I ...

  2. 基于UVM的verilog验证

    Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的UVM验证开发有哪些坑,以及怎么避免. Introduction 本例使用环境:ModelSim 10.2c,UV ...

  3. spring--给配置文件.properties加密

    11111111111编写类并继承PropertyPlaceholderConfigurer.java package com.xx.encryptDecrypt; import java.util. ...

  4. ndarray的用法总结

    #发现ndarray的一维,二维都可以用[i][j], 它们都是下标索引的连用, 比如j表示第j个元素;#二维ndarray可以用[m, n]来进行行列的操作,类似matlab中的用法.取某一列是[: ...

  5. 【JVM】-NO.111.JVM.1 -【JDK11 HashMap详解-1-hash()剖析】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  6. outlook2016用Exchange轻松绑定腾讯企业邮箱

    系统版本:Win10 X64 1709 英文版 邮箱:Outlook2016 背景知识: 1.发送邮件均使用SMTP协议(SMTP 全称“Simple Mail Transfer Protocol”, ...

  7. iPhone屏蔽IOS更新、iPhone系统更新的提示(免越狱,有效期更新至2021年)

    iPhone屏蔽IOS更新.iPhone系统更新的提示(免越狱,有效期更新至2021年) 1.在Safari浏览器中粘贴如下链接,按提示打开链接. 输入http://apt.dataage.pub 2 ...

  8. 32位ubuntu16.4编译android4.1.1

    安装所需库 sudo apt-get install build-essential sudo apt-get install make sudo apt-get install gcc sudo a ...

  9. beego 初体验 - 基础模块 - config, httplibs, logs

    beego 的基础模块支持了一些web开发常用的功能. 配置,http操作库,日志 配置模块: 这是我的配置文件 如何读取: httplibs:这是一个利用 httplibs 发起 get 请求的示例 ...

  10. 关于EasyUI查询功能的二级联动

    EasyUI 二级联动 data-options="multiple:true" 属性可实现对于车牌号的多选.