RabbitMQ的五种模式

一、简单模式

简单模式(Simple):一个生产者,一个消费者
在这里插入图片描述

package com.qiangesoft.rabbitmq.mode.simple;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 简单模式* ps:一个生产者,一个消费者** @author qiangesoft* @date 2024-05-08*/
@Slf4j
@RequestMapping("/simple")
@RestController
public class SimpleMode {private final String QUEUE = "simple.queue";@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 生产者*/@GetMapping("/send")public void send(String message) {rabbitTemplate.convertAndSend(QUEUE, message);}/*** 消费者*/@RabbitListener(queuesToDeclare = @Queue(name = QUEUE))public void receiveMessage(String message) {log.info("Received Message: " + message);}}

二、工作模式

工作队列模式(Work Queue): 多个消费者竞争消息
在这里插入图片描述

package com.qiangesoft.rabbitmq.mode.work;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 工作模式* ps:多个消费者竞争消息** @author qiangesoft* @date 2024-05-08*/
@Slf4j
@RequestMapping("/work")
@RestController
public class WorkMode {private final String QUEUE = "work.queue";@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 生产者*/@GetMapping("/send")public void send(String message) {for (int i = 0; i < 10; i++) {rabbitTemplate.convertAndSend(QUEUE, message + i);}}/*** 消费者1*/@RabbitListener(queuesToDeclare = @Queue(name = QUEUE))public void receiveMessage1(String message) {log.info("Received Message by consumer1: " + message);}/*** 消费者2*/@RabbitListener(queuesToDeclare = @Queue(name = QUEUE))public void receiveMessage2(String message) {log.info("Received Message by consumer2: " + message);}}

三、发布订阅模式

发布/订阅模式(Publish/Subscribe):一个生产者,多个消费者
在这里插入图片描述

package com.qiangesoft.rabbitmq.mode.pubsub;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 发布/订阅模式* ps:一个生产者,多个消费者** @author qiangesoft* @date 2024-05-08*/
@Slf4j
@RequestMapping("/pubsub")
@RestController
public class PubSubMode {private final String EXCHANGE = "fanout.exchange";private final String KEY = "";@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 生产者*/@GetMapping("/send")public void sendM(String message) {rabbitTemplate.convertAndSend(EXCHANGE, KEY, message);}/*** 消费者1*/@RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.FANOUT)))public void receiveMessage1(String message) {log.info("Received Message by consumer1: " + message);}/*** 消费者2*/@RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.FANOUT)))public void receiveMessage2(String message) {log.info("Received Message by consumer2: " + message);}}

四、路由模式

路由模式(Routing):根据路由键将消息转发到对应队列
在这里插入图片描述

package com.qiangesoft.rabbitmq.mode.routing;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 路由模式* ps:根据路由键将消息转发到对应队列** @author qiangesoft* @date 2024-05-08*/
@Slf4j
@RequestMapping("/routing")
@RestController
public class RoutingMode {private final String EXCHANGE = "direct.exchange";private final String KEY1 = "direct1";private final String KEY2 = "direct2";@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 生产者*/@GetMapping("/send")public void sendM(String message) {rabbitTemplate.convertAndSend(EXCHANGE, KEY1, message);}/*** 消费者*/@RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.DIRECT), key = {KEY1, KEY2}))public void receiveMessage(String message) {log.info("Received Message: " + message);}}

五、主题模式

通配符模式(Topics):使用通配符匹配路由键
在这里插入图片描述

package com.qiangesoft.rabbitmq.mode.topic;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 主题模式* ps: 使用通配符匹配路由键** @author qiangesoft* @date 2024-05-08*/
@Slf4j
@RequestMapping("/topic")
@RestController
public class TopicMode {private final String EXCHANGE = "topic.exchange";private final String QUEUE = "topic.queue";@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 生产者*/@GetMapping("/send")public void send(String message) {rabbitTemplate.convertAndSend(EXCHANGE, QUEUE, message);}/*** 消费者*/@RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.TOPIC), key = {"topic.*", "#.topic"}))public void receiveMessage(String message) {log.info("Received Message: " + message);}}

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

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

相关文章

事业单位向媒体投稿发文章上级领导交给了我投稿方法

作为一名事业单位的普通职员,负责信息宣传工作,我见证了从传统投稿方式到智能化转型的全过程,这段旅程既是一次挑战,也是一次宝贵的成长。回想起初涉此领域的日子,那些通过邮箱投稿的时光,至今仍然历历在目,其中的酸甜苦辣,构成了我职业生涯中一段难忘的经历。 邮箱投稿:费时费…

CCF-CSP认证考试 202403-1 词频统计 100分题解

更多 CSP 认证考试题目题解可以前往&#xff1a;CSP-CCF 认证考试真题题解 原题链接&#xff1a; 202403-1 词频统计 时间限制&#xff1a; 1.0 秒 空间限制&#xff1a; 512 MiB 题目描述 在学习了文本处理后&#xff0c;小 P 对英语书中的 n n n 篇文章进行了初步整理。 …

Java Array 数组

文章目录 Java Array 数组一&#xff0c;数组的介绍1. 数组的理解(Array)2. 数组相关的概念3. 数组的特点:4. 变量按照数据类型的分类5. 数组的分类6. 一维数组的使用(6个基本点)7. 数组元素的默认初始化值的情况 Java Array 数组 一&#xff0c;数组的介绍 1. 数组的理解(Ar…

C++从入门到精通---模版

文章目录 泛型编程函数模版模版参数的匹配原则类模版类模版的定义格式类模版的实例化 总结 泛型编程 泛型编程是一种编程范式&#xff0c;旨在实现通用性和灵活性。它允许在编写代码时使用参数化类型&#xff0c;而不是具体的类型&#xff0c;从而使代码更加灵活和可重用。 在…

EventFilter函数,屏蔽Up、Down等键盘事件

首先需要有EventFilter函数&#xff1a; https://blog.csdn.net/qq_46630245/article/details/135472802 .h protected://void paintEvent( QPaintEvent *painter );bool eventFilter(QObject *obj,QEvent *event);.cpp //事件过滤器 bool form1::eventFilter(QObject *watc…

spring ioc 容器加载过程 refresh() 方法详解

IOC 加载过程 从 new ClassPathXmlApplicationContext开始 ApplicationContext context new ClassPathXmlApplicationContext("classpath:application.xml");ClassPathXmlApplicationContext类构造方法 public ClassPathXmlApplicationContext(String[] configLo…

Redis集群分片

什么是集群 集群是由多个复制集组成的,能提供在多个redis节点间共享数据的程序集 简而言之就是将原来的单master主机拆分为多个master主机,将整个数据集分配到各主机上 集群的作用 集群中可以存在多个master,而每个master可以挂载多个slave自带哨兵的故障转移机制,不需要再去…

Python解释器3.8.2版本安装详细教程

Python解释器提取链接链接&#xff1a; https://pan.baidu.com/s/1eDvwYmUJ4l7kIBXewtN4EA?pwd1111 提取码&#xff1a;1111 演示版本为3.6.8&#xff0c;链接安装包为3.8.2版&#xff0c;包中附加pytharm安装包。 1.双击提取好的python-exe安装文件&#xff0c;会…

Navigation常见场景解决方案

路由跳转场景 页面跳转是路由最常用的能力&#xff0c;Navigation通过NavPathStack提供了诸多方法&#xff0c;下文以pushDestination方法为例&#xff0c;介绍Navigation的路由跳转相关能力。 页面间跳转 NavPathStack提供了路由管理的能力&#xff0c;通过NavPathStack进行…

外企接受大龄程序员吗?

本人知乎账号同公众号&#xff1a;老胡聊Java&#xff0c;欢迎留言并咨询 亲身体会外企经历所见所闻&#xff0c;外企能接受大龄程序员。 1 大概是10年的时候&#xff0c;进一家知名外企&#xff0c;和我一起进的一位manager&#xff0c;后来听下来&#xff0c;年龄35&#xf…

0508_IO3

练习1&#xff1a; 1&#xff1a;使用 dup2 实现错误日志功能 使用 write 和 read 实现文件的拷贝功能&#xff0c;注意&#xff0c;代码中所有函数后面&#xff0c;紧跟perror输出错误信息&#xff0c;要求这些错误信息重定向到错误日志 err.txt 中去 1 #include <stdio.h…

【matlab基础知识代码】(十二)逆矩阵与广义逆矩阵

>> Hhilb(4);H1inv(H),norm(H*H1-eye(4))H1 1.0e03 *0.0160 -0.1200 0.2400 -0.1400-0.1200 1.2000 -2.7000 1.68000.2400 -2.7000 6.4800 -4.2000-0.1400 1.6800 -4.2000 2.8000ans 2.8455e-13 矩阵维数较大&#xff0c;警告: 矩阵接近奇…

svg画扇形进度动画

有人问下面这种图好怎么画&#xff1f;svg 想了下&#xff0c;确实用svg可以&#xff0c;可以这么设计 外层是一个容器放置内容&#xff0c;并且设置overflow:hidden&#xff0c; 内层放一个半径大于容器宽高一半的svg&#xff0c;并定位居中&#xff0c;然后svg画扇形&#x…

线程的组成、执行特点、创建的两种方式

线程的组成&#xff1a; cpu时间片 运行内存&#xff1a;栈、堆 线程的逻辑代码 线程执行的特点&#xff1a; 抢占式执行&#xff0c;结果随机&#xff0c;效率高&#xff0c;可以防止单一线程长时间独占CPU 在单核cpu中&#xff0c;宏观上同时执行&#xff0c;微观上顺序…

ABC猜想:数论中的未解之谜

ABC猜想&#xff1a;数论中的未解之谜 引言 ABC猜想是数论领域中一个著名的未解问题&#xff0c;它由法国数学家约瑟夫奥斯特莱&#xff08;Joseph Oesterl&#xff09;和大卫马瑟&#xff08;David Masser&#xff09;在1985年提出。ABC猜想涉及整数加法和乘法之间的深刻联系…

C++青少年简明教程之一:基础知识

C青少年简明教程之一&#xff1a;基础知识 电脑程序设计&#xff08;Computer programming&#xff09;&#xff0c;或称程序设计&#xff08;programming&#xff09;&#xff0c;是给出解决特定问题程序的过程&#xff0c;程序设计往往以某种程序设计语言为工具&#xff0c;给…

【算法】逃离大迷宫

题目信息 在一个 10^6 x 10^6 的网格中&#xff0c;每个网格上方格的坐标为 (x, y) 。 现在从源方格 source [sx, sy] 开始出发&#xff0c;意图赶往目标方格 target [tx, ty] 。数组 blocked 是封锁的方格列表&#xff0c;其中每个 blocked[i] [xi, yi] 表示坐标为 (xi, …

【软件测试】用例篇 -- 详解

一、测试用例的基本要素 测试用例&#xff08;Test Case&#xff09;是为了实施测试而向被测试的系统提供的一组集合&#xff0c;这组集合包含&#xff1a;测试环境、操作步骤、测试数据、预期结果等要素。&#xff08;注意&#xff1a;不需要执行结果&#xff0c;因为执行结果…

IO 5.8日

1&#xff1a;使用 dup2 实现错误日志功能 使用 write 和 read 实现文件的拷贝功能&#xff0c;注意&#xff0c;代码中所有函数后面&#xff0c;紧跟perror输出错误信息&#xff0c;要求这些错误信息重定向到错误日志 err.txt 中去 2&#xff1a;判断一个文件是否拥有用户可写…

python在Django中实现文件上传功能主要涉及几个步骤

在Django中实现文件上传功能主要涉及几个步骤。以下是一个基本的指南,用于在Django应用程序中设置文件上传: 设置模型 首先,你需要在模型中定义一个FileField或ImageField。对于图像,ImageField更为合适,因为它包含对图像大小调整和其他图像相关功能的验证。 python 复…