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. English Time And Date

    What's the Time in English? Explanation There are two common ways of telling the time. Formal but ea ...

  2. MySql查看与修改auto_increment方法(转)

    add by zhj:  在创建表时,如果没有显式的指定AUTO_INCREMENT的值,那它默认是1 原文:https://blog.csdn.net/fdipzone/article/detail ...

  3. 11.1-uC/OS-III就绪列表

    准备好运行的任务被放到就绪列表中, 如图6-1.就绪列表是一个数组( OSRdyList[]),它一共有OS_CFG_PRIO_MAX条记录,记录的数据类型为OS_RDY_LIST(见OS.H).就绪 ...

  4. jenkins使用笔记

    jenkins动态在构建的时候给脚本传递参数 1.任务  >General > 参数化构建过程 >选项参数 2.把变量传递给shell脚本 3.构建的时候给参数赋值 4.shell脚 ...

  5. [js]js设计模式-工厂模式

    // 定义一个人 var p1 = { name: 'wxb', age: 22, writejs: function () { console.log(this.name + ' can sing. ...

  6. Linux下安装Gensim

    依赖软件包:numpy 直接使用pip安装: [root@mycentos ~]#pip install gensim 安装gensim的时候会遇到下面的一系列错误: Cannot uninstall ...

  7. Selenium上机实验

    1.安装SeleniumIDE插件 2.学会使用SeleniumIDE录制脚本和导出脚本 3.访问https://psych.liebes.top/st使用学号登录系统(账户名为学号,密码为学号后6位 ...

  8. 平常比较多实用的SQL

    创建数据库 创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database ...

  9. sitecore 数字化营销-path funnel

    路径分析器是一个应用程序,允许您查看联系人在浏览网站时所采用的各种路径.您可以查看联系人在转换目标并与广告系列互动时所采用的路径,让您深入了解哪些路径为每次转化提供最佳参与价值,以及哪些路径效率较低且 ...

  10. Java当出现未被捕获的异常应该如何处理

    在你学习在程序中处理异常之前,看一看如果你不处理它们会有什么情况发生是很有好处的.下面的小程序包括一个故意导致被零除错误的表达式.class Exc0 {    public static void ...