SpringBoot应用操作Rabbitmq(topic交换器高级操作)
一、topic交换器为主题交换器,可以根据路由key模糊匹配
实现模型图

二、实战
1、引入maven
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
2、修改配置文件
server:
port: 8080
servlet:
context-path: /rabbitmq spring:
application:
#指定应用的名字
name: rabbit-add
#配置rabbitmq
rabbitmq:
#链接主机
host: 127.0.0.1
#端口
port: 5672
#已经授权的用户账号密码
username: user
password: user
#指定的虚拟主机,默认/,
virtual-host: my_vhost # 自定义配置应用于topic交换器
mq:
config:
#自定义交换器名称
exchange: log.topic
queue:
#自定义error、info、all队列名称
errorName: topic.error.log
infoName: topic.info.log
allName: topic.all.log
#自定义error、info、all路由键的名称
routingInfoKey: topic.info.routing.key
routingErrorKey: topic.error.routing.key
3、消费者代码
a、模糊匹配所有的数据队列,注意在配置路由key的时候是*代表阶段的配置,.不在匹配范围内
package com.niu.topic; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/4/28 7:20 PM
* @RabbitListener 自定义监听事件
* @QueueBinding 绑定交换器与队列的关系value 指定队列exchange指定交换器
* value= @Queue 指定配置队列的信息 value队列名称 autoDelete是否是临时队列
* exchange= @Exchange 指定交换器 value指定交换器名称 type交换器类型
* key 指定路由键
*/
@Component
@Slf4j
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(
value = "${mq.config.queue.allName}", autoDelete = "true"
),
exchange = @Exchange(
value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
key = "*.*.routing.*")
)
public class AllReceiver { /**
* 设置监听方法
*
* @param msg
* @RabbitHandler 声明监听方法是下面的 isDefault属性是默认false接受的完整对象,true接受body体
*/
@RabbitHandler(isDefault = true)
public void process(String msg) {
log.info("接受到消息:all {}", msg);
}
}
b、error的消费端
package com.niu.topic; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/4/28 7:20 PM
* @RabbitListener 自定义监听事件
* @QueueBinding 绑定交换器与队列的关系value 指定队列exchange指定交换器
* value= @Queue 指定配置队列的信息 value队列名称 autoDelete是否是临时队列
* exchange= @Exchange 指定交换器 value指定交换器名称 type交换器类型
* key 指定路由键
*/
@Component
@Slf4j
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(value = "${mq.config.queue.errorName}", autoDelete = "true"),
exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
key = "${mq.config.queue.routingErrorKey}")
)
public class ErrorReceiver { /**
* 设置监听方法
* @RabbitHandler 声明监听方法是下面的 isDefault属性是默认false接受的完整对象,true接受body体
*
* @param msg
*/
@RabbitHandler(isDefault = true)
public void process(String msg) {
log.info("接受到消息:error {}", msg);
}
}
c、info的消费端
package com.niu.topic; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/4/28 7:20 PM
* @RabbitListener 自定义监听事件
* @QueueBinding 绑定交换器与队列的关系value 指定队列exchange指定交换器
* value= @Queue 指定配置队列的信息 value队列名称 autoDelete是否是临时队列
* exchange= @Exchange 指定交换器 value指定交换器名称 type交换器类型
* key 指定路由键
*/
@Component
@Slf4j
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(
value = "${mq.config.queue.infoName}", autoDelete = "true"
),
exchange = @Exchange(
value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
key = "${mq.config.queue.routingInfoKey}")
)
public class InfoReceiver { /**
* 设置监听方法
*
* @param msg
* @RabbitHandler 声明监听方法是下面的 isDefault属性是默认false接受的完整对象,true接受body体
*/
@RabbitHandler(isDefault = true)
public void process(String msg) {
log.info("接受到消息:info {}", msg);
}
}
4、生产者
package com.niu.topic; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/4/29 9:44 AM
*/
@Component
public class Sender {
/**
* spring整合的操作类
* Message 发送的消息对象
* void send(Message var1) throws AmqpException;
* <p>
* var1 路由键 Message 发送的消息对象
* void send(String var1, Message var2) throws AmqpException;
* <p>
* var1 指定交换器名称 var2 路由键 Message 发送的消息对象
* void send(String var1, String var2, Message var3) throws AmqpException;
*
* convertAndSend() 方法不需要指定MessageProperties属性即可发布
*/
@Autowired
private RabbitTemplate rabbitTemplate; @Value("${mq.config.queue.routingInfoKey}")
private String routingInfoKey;
@Value("${mq.config.queue.routingErrorKey}")
private String routingErrorKey;
@Value("${mq.config.exchange}")
private String exchange; public void send(String msg) {
//需要指定交换器和路由键就可以转发
rabbitTemplate.convertAndSend(exchange, routingInfoKey, "info+"+msg);
rabbitTemplate.convertAndSend(exchange, routingErrorKey,"error+"+ msg);
rabbitTemplate.convertAndSend(exchange, "topic.order.routing.key","order+"+ msg);
rabbitTemplate.convertAndSend(exchange, "topic.warn.routing.key", "warn+"+msg);
} }
5、测试截屏

注意:ack确认机制,容易产生数据丢失,和产生内存泄漏,消费者进行死循环,配置这两个属性进行确认。
1、autoDelete属性设置为false
@Queue(value = "${mq.config.queue.orderName}", autoDelete = "false"
2、消费者进行死循环问题

docker安装rabbitmq:rabbitMQ安装docker版 /权限管理命令
简单应用来这里吧: SpringBoot应用操作Rabbitmq
简单应用来这里吧:SpringBoot应用操作Rabbitmq(direct高级操作)
简单应用来这里吧:SpringBoot应用操作Rabbitmq(topic交换器高级操作)
简单应用来这里吧:SpringBoot应用操作Rabbitmq(fanout广播高级操作)
SpringBoot应用操作Rabbitmq(topic交换器高级操作)的更多相关文章
- SpringBoot应用操作Rabbitmq(direct高级操作)
一.首先声明完成任务架构,通过direct订阅/发布的模式进行生产消费. a.消息生产指定交换器和路由key b.消费者绑定交换器,路由key和队列的关系(集群监控收到的消息不重复) 二.实战演练 1 ...
- RabbitMQ topic 交换器
topic交换器:"."将路由键分为几个标识符,"*"匹配一个, "#"可以匹配多个 1:路由键为*或者#的时候 *:只能匹配单个的字符串 ...
- SpringBoot应用操作Rabbitmq(fanout广播高级操作)
一.广播模式fanout.不需要指定路由key. 注:与topic和direct区别是:fanout广播模式会两个队列同时发送相同的消息,并非由交换器转发到某一个队列 二.实战(广播模式) 1.引入m ...
- SpringBoot应用操作Rabbitmq
记录RabbitMQ的简单应用 1.springboot项目中引入maven包,也是springboot官方的插件 <dependency> <groupId>org.spri ...
- Scala学习——函数高级操作
scala函数高级操作 一.字符串高级操作 多行字符串和插值 package top.ruandb.scala.Course06 object StringApp { def main(args: A ...
- Python之路第十二天,高级(4)-Python操作rabbitMQ
rabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(M ...
- EasyNetQ操作RabbitMQ(高级消息队列)
RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).写消息队列的时候用RabbitMQ比较好,但是写的时候需要自己封装下,自己的封装,就需要对RabbitM ...
- SpringBoot操作ES进行各种高级查询
SpringBoot整合ES 创建SpringBoot项目,导入 ES 6.2.1 的 RestClient 依赖和 ES 依赖.在项目中直接引用 es-starter 的话会报容器初始化异常错误,导 ...
- Python菜鸟之路:Python基础-Python操作RabbitMQ
RabbitMQ简介 rabbitmq中文翻译的话,主要还是mq字母上:Message Queue,即消息队列的意思.rabbitmq服务类似于mysql.apache服务,只是提供的功能不一样.ra ...
随机推荐
- vulnhub~MyExpense
最近有点忙,这几天的vulnhub断更了,今天试着做了一下myexpense,当然想要一帆风顺是不可能的,哪怕是有别人的steps 和walkthrough.所以就遇到的坑总结如下: 一般套路就是nm ...
- Appium:We shut down because no new commands came in
在使用Appium自带的Inspector来查找元素定位时,一段时间(60s)不对其进行任何操作appium就会关闭Android应用,并打印出 info: [debug] We shut down ...
- echarts图表x,y轴的设置
https://www.cnblogs.com/cjh-strive/p/11065005.html xAxis属性代表echarts图表的x轴设置代码如下 xAxis : [ { type : 'c ...
- 【docker linux】linux系统镜像转化为docker镜像
概述 使用docker安装linux的同学都知道,你在docker提供的仓库安装linux系统,你就会体验到最精简的.最纯净的linux系统,当然,他会精简到你连ifconfig命令都需要自己配置,恰 ...
- 云计算介绍、TCP/IP协议及配置
云计算介绍.TCP/IP协议及配置 1案例1:配置计算机名及工作组 1.1问题 本例要求为修改计算机名并加入工 ...
- python--内建属性、集合、常见模块、调试
一.常用内建属性 常用专有属性 说明 触发方式 __init__ 构造初始化函数 创建实例后,赋值时使用,在__new__后 __new__ 生成实例所需属性 创建实例时 __class__ 实例所在 ...
- uni-app在线引入阿里字体图标库
第一步 在app.vue中引入阿里字体图标库 第二步 在任意页面使用就可以了 <view class="item" v-for="(value,index) in ...
- 《操作系统》课程笔记(Ch01-导论)
Ch01 - 导论 操作系统的功能 用户视角:在乎使用方便,不在乎资源利用 系统视角:资源分配器.控制程序 计算机系统的运行 启动:利用固件(Firmware)中的引导程序(Bootstrap Pro ...
- 9.2ArrayList 集合 案例,学生管理系统
循环的使用 添加学生:while嵌套for,for设置变量,内嵌if更新变量.if语句判断变量值 修改学生:for循环内嵌if,获取循环中的某个值. package day9_ArrayList.AL ...
- Docker之hello world
Docker Hello World Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world runoob@runoob: ...