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. ping不通,配置dns

    vim /etc/resolv.conf nameserver 119.29.29.29 nameserver 182.254.116.116 nameserver 8.8.8.8

  2. [No0000C6]Visual Studio 2017 函数头显示引用个数

    Visual Studio 2017  函数头显示引用个数

  3. Filter and servlet

    filter与servlet的区别与联系呢? 1. Filter    实现javax.servlet.Filter接口,在web.xml中配置与标签指定使用哪个Filter实现类过滤哪些URL链接. ...

  4. ES6 字符串

    拓展的方法 子串的识别 ES6 之前判断字符串是否包含子串,用 indexOf 方法,ES6 新增了子串的识别方法. includes():返回布尔值,判断是否找到参数字符串. startsWith( ...

  5. java框架之SpringCloud(4)-Ribbon&Feign负载均衡

    在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...

  6. MySQL Backup mysqldump备份流程学习

    我们都知道MySQL逻辑备份工具mysqldump可以保证备份数据的一致性,但是它是怎么保持一致性的? 本文不讨论mysqldump具体的选项和用法,一直对mysqldump的工作机制梳理的不太清楚, ...

  7. Java代码质量改进之:同步对象的选择

    在Java中,让线程同步的一种方式是使用synchronized关键字,它可以被用来修饰一段代码块,如下: synchronized(被锁的同步对象) { // 代码块:业务代码 } 当synchro ...

  8. hdu5115 Dire Wolf

    题目链接 区间DP $dp_{i,j}$为杀掉$i~j$内的狼的最小代价 枚举$i~j$中最后杀掉的狼,$dp_{i,j}=min\{ { {k\in{[i,j]}} | dp_{i,k-1}+dp_ ...

  9. CentOS6.5系统,mysql数据库的安装

    1.查看数据库中已安装的版本: [mdata@bogon ~]$ yum list installed|grep mysqlmysql-libs.x86_64 5.1.71-1.el6 @anacon ...

  10. oracle数据库调整字段顺序

    oracle数据库调整字段顺序 https://blog.csdn.net/xiaobaixie/article/details/77892034