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. 阿里云 centos 服务器无法自动挂载 nas 的问题

    阿里云服务器 centos 7.3 ,开始是通过 fstab 配置的自动挂载: xxx.cn-hangzhou.nas.aliyuncs.com:/ /nas nfs4 auto 0 0 但服务器启动 ...

  2. SQL中什么时候需要使用游标?使用游标的步骤

    https://zhidao.baidu.com/question/568932670.html 例子table1结构如下id intname varchar(50) declare @id intd ...

  3. [转]在centos 6.4下安装opencv 2.3.1

    系统环境介绍: centos 6.4 1.安装依赖包 yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp ...

  4. [ovs] openvswitch 从源码编译安装

    文档:https://docs.openvswitch.org/en/latest/intro/install/general/ 1. yum install autoconf automake li ...

  5. pandas 2

    ============== sdf={'rkey':[1,2,3,2],'name':['rkey1','rkey2','rkey3','rkey4']}sdf2={'lkey':[1,2,3],' ...

  6. spark-sql集合的“条件过滤”,“合并”,“动态类型映射DataFrame”,“存储”

    List<String> basicList = new ArrayList<String>(); basicList.add("{\"name\" ...

  7. 目标检测(六)YOLOv2__YOLO9000: Better, Faster, Stronger

    项目链接 Abstract 在该论文中,作者首先介绍了对YOLOv1检测系统的各种改进措施.改进后得到的模型被称为YOLOv2,它使用了一种新颖的多尺度训练方法,使得模型可以在不同尺寸的输入上运行,并 ...

  8. java框架之Struts2(3)-OGNL&ValueStack

    OGNL 概述 OGNL 是 Object-Graph Navigation Language 的缩写,它是一种第三方的.功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用 ...

  9. PHP 二位数组按照下标排序

    1.排序得内容 array(6) { [0] => array(12) { [0] => string(3) "160" [1] => string(2) &qu ...

  10. JavaScript知识精简

      JS单线程,同步,一次执行某一段代码,等到前一个程序执行完毕再执行.,阻塞,安全. 多线程,异步,不用等到前一个程序执行完毕就执行. 数据类型 JavaScript 是 弱类型 语言,但并不是没有 ...