RabbitMQ常见的交换机类型

RabbitMQ安装

pom.xml里导入相关的依赖:

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

application.properties配置文件

spring.rabbitmq.host=192.168.152.155
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
#开启发送端确认 生产者Publisher 到服务器Broker
spring.rabbitmq.publisher-confirms=true
#开启发送端消息抵达队列的确认
spring.rabbitmq.publisher-returns=true
#只要抵达队列,以异步发送优先回调我们这个returnConfirm
spring.rabbitmq.template.mandatory=true

Direct exchange(直通交换机)

        消息中的路由键(routing key)如果和 Binding中的binding key 一致,交换器就将消息发到对应的队列中。路由键与队列名完全匹配,如果一个队列绑定到交换机要求路由键为“dog”,则只转发routing key 标记为“dog”的消息,不会转发 “dog.puppy”,也不会转发“dog.guard” 等等。它是 完全匹配、单播的模式
接着我们先使用下 direct exchange(直连型交换机),创建 DirectRabbitConfig.java
package com.beijing.gulimall.product.rabbitmq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Slf4j
@Configuration
public class DirectRabbitConfig {//创建队列@Beanpublic Queue TestDirectQueue() {//public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete)// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。log.info("Queue[{}]创建成功", "TestDirectQueue");return new Queue("TestDirectQueue", true, false, false);}//创建交换机@BeanDirectExchange TestDirectExchange() {log.info("Exchange[{}]创建成功", "TestDirectExchange");return new DirectExchange("TestDirectExchange", true, false);}//创建绑定关系@BeanBinding TestBindingDirect() {//	public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,//			Map<String, Object> arguments) {log.info("Binding[{}]创建成功", "TestBindingDirect");return new Binding("TestDirectQueue", Binding.DestinationType.QUEUE, "TestDirectExchange", "direct.test", null);}}

然后写个简单的接口进行消息推送 SendMessageController.java

package com.beijing.gulimall.product.rabbitmq;import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;@RestController
public class SendMessageController {@AutowiredRabbitTemplate rabbitTemplate;@RequestMapping("/hello")public void testRabbitMQ() {String messageId = String.valueOf(UUID.randomUUID());String messageData = "test message, hello!";String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));Map<String, Object> map = new HashMap<>();map.put("messageId", messageId);map.put("messageData", messageData);map.put("createTime", createTime);//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchangerabbitTemplate.convertAndSend("TestDirectExchange", "direct.test", JSONObject.toJSONString(map));}
}

Fanout Exchange (扇出交换机)

每个发到 fanout 类型交换器的消息都会分到所有绑定的队列上去。fanout 交换器不处理路由键,只是简单的将队列绑定到交换器上,每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上。很像子网 广播 ,每台子网内的主机都获得了一份复制的消息。fanout 类型转发消息是最快的。
创建FanoutRabbitConfig.class
package com.beijing.gulimall.product.rabbitmq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutRabbitConfig {@Beanpublic FanoutExchange testFanoutExchange(){return new FanoutExchange("TestFanoutExchange",true,false);}@Beanpublic Queue testFanoutQueueOne(){return new Queue("TestFanoutQueueOne",true,false,false);}@Beanpublic Queue testFanoutQueueTwo(){return new Queue("TestFanoutQueueTwo",true,false,false);}@Beanpublic Binding createFanoutBindingOne(){return new Binding("TestFanoutQueueOne", Binding.DestinationType.QUEUE,"TestFanoutExchange","fanout.one231.test",null);}@Beanpublic Binding createFanoutBindingTwo(){return new Binding("TestFanoutQueueTwo", Binding.DestinationType.QUEUE,"TestFanoutExchange","fanout123.one.#",null);}
}

发送消息 

    @RequestMapping("/hello3")public void testRabbitMQ3() {String messageId = String.valueOf(UUID.randomUUID());String messageData = "test message, hello!";String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));Map<String, Object> map = new HashMap<>();map.put("messageId", messageId);map.put("messageData", messageData);map.put("createTime", createTime);//topic.one.test2//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchangerabbitTemplate.convertAndSend("TestFanoutExchange","",JSONObject.toJSONString(map));}

结果:

主题交换机(Topic Exchange)

topic 交换器通过模式匹配分配消息的路由键属性,将路由键和某个模式进行匹配,此时队列需要绑定到一个模式上。它将路由键和绑定键的字符串切分成单词,这些单词之间用点隔开 。它同样也
会识别两个通配符:符号“ #” 和符号 “* ”。 # 匹配 0 个或多个单词, * 匹配一个单词。
创建 TopicRabbitConfig.class
package com.beijing.gulimall.product.rabbitmq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TopicRabbitConfig {@Beanpublic TopicExchange testTopicExchange(){return new TopicExchange("TestTopicExchange");}@Beanpublic Queue testTopicQueueOne(){return new Queue("TestTopicQueueOne",true,false,false);}@Beanpublic Queue testTopicQueueTwo(){return new Queue("TestTopicQueueTwo",true,false,false);}@Beanpublic Binding createBindingOne(){return new Binding("TestTopicQueueOne", Binding.DestinationType.QUEUE,"TestTopicExchange","topic.one.test",null);}@Beanpublic Binding createBindingTwo(){return new Binding("TestTopicQueueTwo", Binding.DestinationType.QUEUE,"TestTopicExchange","topic.one.#",null);}}

发送消息 

    @RequestMapping("/hello2")public void testRabbitMQ2() {String messageId = String.valueOf(UUID.randomUUID());String messageData = "test message, hello!";String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));Map<String, Object> map = new HashMap<>();map.put("messageId", messageId);map.put("messageData", messageData);map.put("createTime", createTime);//topic.one.test2//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchangerabbitTemplate.convertAndSend("TestTopicExchange", "topic.one.test", JSONObject.toJSONString(map));}

结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/104046.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Nginx - 反向代理与负载均衡

目录 一、Nginx 1.1、Nginx 下载 1.2、nginx 基础配置的认识 a&#xff09;第一部分&#xff1a;全局块 b&#xff09;第二部分&#xff1a;events 块 c&#xff09;第三部分&#xff1a;http 块 http 块中 内嵌的 server 块 1.3、一些常用配置 1.3.1、location 匹配级…

java正则表达式 及应用场景爬虫,捕获分组非捕获分组

正则表达式 通常用于校验 比如说qq号 看输入的是否符合规则就可以用这个 public class regex {public static void main(String[] args) {//正则表达式判断qq号是否正确//规则 6位及20位以内 0不能再开头 必须全是数子String qq"1234567890";System.out.println(qq…

【机器学习】sklearn特征选择(feature selection)

文章目录 特征工程过滤法&#xff08;Filter&#xff09;方差过滤相关性过滤卡方过滤F验表互信息法小结 嵌入法&#xff08;Embedded&#xff09;包装法&#xff08;Wrapper&#xff09; 特征工程 特征提取(feature extraction)特征创造(feature creation)特征选择(feature se…

【软件设计师-下午题总结】

目录 下午题之总结于学习记录&#xff1a;题一、数据流图&#xff1a;1、熟悉相关的图形2、实体名称3、数据存储4、补充缺失的数据流和起点终点5、用结构化语言描述6、描述&#xff0c;找加工逻辑的时候7、如何保持数据流平衡 题二&#xff1a;实体联系图&#xff1a;1、常用图…

Django Test

Django--Laboratory drug management and early warning system-CSDN博客 创建项目doinglms django-admin startproject doinglms python manage.py runserver 运行开发服务器(Development Server) 创建一个自定义 App,名称为 lms: python manage.py startapp lms

minio桶命名规则

一、背景 今天做项目需要上传图片到minio&#xff0c;上传失败&#xff0c;查看错误是桶未创建成功。 minio桶的创建具有自己的命名规则&#xff0c;不符合则无法创建。 二、命名规则 1、存储桶名称的长度必须介于 3&#xff08;最小&#xff09;到 63&#xff08;最大&…

【数据结构】二叉树--堆排序

目录 一 降序(建小堆) 二 升序 (建大堆) ​三 优化(以升序为例) 四 TOP-K问题 一 降序(建小堆) void Swap(int* x, int* y) {int tmp *x;*x *y;*y tmp; }//降序 建小堆 void AdjustUp(int* a, int child) {int parent (child - 1) / 2;while (child > 0){if (a[chil…

Ubuntu 22.04.3 LTS单机私有化部署sealos

推荐使用奇数台 Master 节点和若干 Node 节点操作系统 :Ubuntu 22.04 LTS内核版本 :5.4 及以上配置推荐 :CPU 4 核 , 内存 8GB, 存储空间 100GB 以上最小配置 :CPU 2 核 , 内存 4GB, 存储空间 60GB 这里采用的Ubuntu 22.04.3 LTS 版本&#xff0c;Ubuntu 20.04.4 LTS这个版本…

Eclipse插件安装版本不兼容问题解决方案——Papyrus插件为例

项目场景: Eclipse Papyrus安装后,没有新建Papyrus工程选项,也没有新建Papyrus Model的选项。 打开Papyrus Model会报错 问题描述 同样的,安装其他插件也是。可能某个插件之前安装是好用的,结果Eclipse的版本更新了,就再也安装不好用了 原因分析: 根本原因是因为包之…

Xcode 15下,包含个推的项目运行时崩溃的处理办法

升级到Xcode15后&#xff0c;部分包含个推的项目在iOS17以下的系统版本运行时&#xff0c;会出现崩溃&#xff0c;由于崩溃在个推Framework内部&#xff0c;无法定位到具体代码&#xff0c;经过和个推官方沟通&#xff0c;确认问题是项目支持的最低版本问题。 需要将项目的最低…

十七、【渐变工具组】

文章目录 渐变工具油漆桶工具 渐变工具 渐变样式有5种&#xff0c;分别是线性渐变&#xff0c;径向渐变&#xff0c;角度渐变&#xff0c;对称渐变&#xff0c;菱形渐变 另外渐变工具的颜色可以进行编辑&#xff0c;需要先打开渐变编辑工具&#xff1a; 如何使用渐变编辑工…

与HTTP相关的各种概念

网络世界 网络世界中最重要的一个名词就是互联网&#xff08;Internet&#xff09;,它以TCP/IP协议族为基础&#xff0c;构建成了一望无际的信息传输网络。而我们通常所说的“上网”&#xff0c;主要就是访问互联网的一个子集——万维网&#xff08;World Wide Web&#xff09…

如何使用CSS和JavaScript实施暗模式?

近年来&#xff0c;暗模式作为用户界面选项备受追捧。它提供了更暗的背景和更亮的文本&#xff0c;不仅可以减轻眼睛疲劳&#xff0c;还可以节省电池续航时间&#xff0c;尤其是在OLED屏幕上。 不妨了解如何结合使用CSS和JavaScript为网站和Web应用程序添加暗模式选项。 了解暗…

JWT的原理及实际应用

前言&#xff1a; 定义&#xff1a;JSON Web Token&#xff08;缩写 JWT&#xff09;是目前最流行的跨域认证解决方案 JWT官网 由于HTTP协议是无状态的&#xff0c;这意味着如果我们想判定一个接口是否被认证后访问&#xff0c;就需要借助cookie或者session会话机制进行判定&…

保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment

开发环境&#xff1a; 效果图&#xff1a; 《《《代码在底部》》》 1&#xff0c;新建项目 2&#xff0c;新建若干Fragment&#xff0c;内容一样&#xff0c;改一下显示出来的Text&#xff0c;名字分别为test1Fragment,test2Fragment,test3Fragment,默认TextView的Text属性分别…

交通 | python网络爬虫:“多线程并行 + 多线程异步协程

推文作者&#xff1a;Amiee 编者按&#xff1a; 常规爬虫都是爬完一个网页接着爬下一个网页&#xff0c;不适应数据量大的网页&#xff0c;本文介绍了多线程处理同时爬取多个网页的内容&#xff0c;提升爬虫效率。 1.引言​ 一般而言&#xff0c;常规爬虫都是爬完一个网页接着…

代码随想录算法训练营第五十二天 | 123.买卖股票的最佳时机III、188.买卖股票的最佳时机IV

123.买卖股票的最佳时机III 视频讲解&#xff1a;动态规划&#xff0c;股票至多买卖两次&#xff0c;怎么求&#xff1f; | LeetCode&#xff1a;123.买卖股票最佳时机III_哔哩哔哩_bilibili 代码随想录 &#xff08;1&#xff09;代码 188.买卖股票的最佳时机IV 视频讲解&a…

文字雨特效

效果展示 CSS 知识点 简易实现云朵技巧text-shadow 属性的灵活运用filter 属性实现元素自动变色 实现页面布局 <div class"container"><div class"cloud"><h2>Data Clouds Rain</h2></div> </div>实现云朵 实现云…

从基础到卷积神经网络(第12天)

1. PyTorch 神经网络基础 1.1 模型构造 1. 块和层 首先&#xff0c;回顾一下多层感知机 import torch from torch import nn from torch.nn import functional as Fnet nn.Sequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))X torch.rand(2, 20) # 生成随机…

<图像处理> Fast角点检测

Fast角点检测 基本原理是使用圆周长为N个像素的圆来判定其圆心像素P是否为角点&#xff0c;如下图所示为圆周长为16个像素的圆&#xff08;半径为3&#xff09;&#xff1b;OpenCV还提供圆周长为12和8个像素的圆来检测角点。 相对中心像素的位置信息 //圆周长为16 static c…