Spring AMQP实现RabbitMQ的5种消息模式

2024-01-09 06:48

本文主要是介绍Spring AMQP实现RabbitMQ的5种消息模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、简单模式

 

简单模式是最简单的消息模式,它包含一个生产者、一个消费者和一个队列。生产者向队列里发送消息,消费者从队列中获取消息并消费。

 

1. 创建队列simple.hello2

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SimpleRabbitConfig {@Beanpublic Queue simpleHello(){return new Queue("simple.hello");}}

2. 创建生产者

@Component
public class SimpleHelloSender {@Autowiredprivate AmqpTemplate amqpTemplate;public void sendMessage(){//发送消息hello simpleamqpTemplate.convertAndSend("simple.hello", "hello simple");}
}

3. 创建消费者

@Component
@RabbitListener(queues = "simple.hello")
public class SimpleHelloReceiver {@RabbitHandlerpublic void handle(String in){System.out.println("我收到了消息:" + in);}
}

4. 测试类

@RestController
@RequestMapping("/rabbit")
public class RabbitTestController {@Autowiredprivate SimpleHelloSender simpleHelloSender;@RequestMapping("/simple")public String simpleSend(){simpleHelloSender.sendMessage();return "消息发送成功";}}

5. 测试结果

二、工作模式(为了方便,和simple方法写在了一起)

       工作模式是指向多个互相竞争的消费者发送消息的模式,它包含一个生产者、两个消费者和一个队列。两个消费者同时绑定到一个队列上去,当消费者获取消息处理耗时任务时,空闲的消费者从队列中获取并消费消息。

 

1. 创建队列

@Bean
public Queue workQueue(){return new Queue("work.queue");
}

2. 创建生产者

public void sendWorkMessage(){amqpTemplate.convertAndSend("work.queue", "hello work queue");
}

3. 创建消费者

@Component
public class RabbitReceiver {// 3个方法同时监听同一个队列@RabbitListener(queues = "work.queue")public void processOne(String in) {System.out.println("work.queue1" + in);}@RabbitListener(queues = "work.queue")public void processTwo(String in) {System.out.println("work.queue2" + in);}@RabbitListener(queues = "work.queue")public void processThree(String in) {System.out.println("work.queue3" + in);}}

4. 测试类

@RequestMapping("/work")
public String workSend(){simpleHelloSender.sendWorkMessage();return "消息发送成功";
}

5. 测试结果,发现是轮询消费,空闲的消费者轮询消费信息,也就是谁有空那就是谁去做事。

三、发布/订阅者模式(Publish/Subscribe)

       发布/订阅模式是指同时向多个消费者发送消息的模式(类似广播的形式),它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列绑定到交换机上去,生产者通过发送消息到交换机,所有消费者接收并消费消息。

 

1. 创建队列

@Configuration
public class FanoutRabbitConfig {// 创建队列@Beanpublic Queue publishOne(){return new Queue("queue.publish.one");}@Beanpublic Queue publishTwo(){return new Queue("queue.publish.two");}@Beanpublic Queue publishThree(){return new Queue("queue.publish.three");}// 创建交换机@Beanpublic FanoutExchange publishExchange(){return new FanoutExchange("publishExchange");}//绑定队列(不用指定routing key),参数名字要和bean名字一致@BeanBinding bingingPublishOne(Queue publishOne, FanoutExchange publishExchange){return BindingBuilder.bind(publishOne).to(publishExchange);}@BeanBinding bindingPublishTwo(Queue publishTwo, FanoutExchange publishExchange){return BindingBuilder.bind(publishTwo).to(publishExchange);}@BeanBinding bindingPublishThree(Queue publishThree, FanoutExchange publishExchange){return BindingBuilder.bind(publishThree).to(publishExchange);}
}

2. 创建生产者

public void sendPublishMessage(){amqpTemplate.convertAndSend("publishExchange","","发布消息");
}

3. 创建消费者

@RabbitListener(queues = "queue.publish.one")
public void publishOne(String in) {System.out.println("queue.publish.one:" + in);
}@RabbitListener(queues = "queue.publish.two")
public void publishTwo(String in) {System.out.println("queue.publish.two:" + in);
}@RabbitListener(queues = "queue.publish.three")
public void publishThree(String in) {System.out.println("queue.publish.three:" + in);
}

4. 测试类

@RequestMapping("/pulish")
public String pulishSend(){simpleHelloSender.sendPublishMessage();return "消息发送成功";
}

5. 测试结果(所有订阅者都能收到消息)

四、路由模式

       路由模式是可以根据路由键选择性给多个消费者发送消息的模式,它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列通过路由键绑定到交换机上去,生产者发送消息到交换机,交换机通过路由键转发到不同队列,队列绑定的消费者接收并消费消息。

1. 创建队列

@Configuration
public class RoutingRabbitConfig {// 创建队列@Beanpublic Queue routingOne(){return new Queue("queue.routing.one");}@Beanpublic Queue routingTwo(){return new Queue("queue.routing.two");}@Beanpublic Queue routingThree(){return new Queue("queue.routing.three");}// 创建交换机@Beanpublic DirectExchange directExchange(){return new DirectExchange("routingExchange");}//绑定队列(@BeanBinding bingingRoutingOne(Queue routingOne, DirectExchange directExchange){return BindingBuilder.bind(routingOne).to(directExchange).with("1");}@BeanBinding bingingRoutingTwo(Queue routingTwo, DirectExchange directExchange){return BindingBuilder.bind(routingTwo).to(directExchange).with("2");}@BeanBinding bingingRoutingThree(Queue routingThree, DirectExchange directExchange){return BindingBuilder.bind(routingThree).to(directExchange).with("3");}
}

2. 创建生产者

public void sendRoutingMessage(String type){amqpTemplate.convertAndSend("routingExchange",type,"发布Routing消息" + type);
}

3. 创建消费者

@RabbitListener(queues = "queue.routing.one")
public void routingOne(String in) {System.out.println("queue.routing.one:" + in);
}@RabbitListener(queues = "queue.routing.two")
public void routingTwo(String in) {System.out.println("queue.routing.two:" + in);
}@RabbitListener(queues = "queue.routing.three")
public void routingThree(String in) {System.out.println("queue.routing.three:" + in);
}

4. 测试类

@RequestMapping("/routing/{type}")
public String routingSend(@PathVariable String type){simpleHelloSender.sendRoutingMessage(type);return "发送成功";
}

5. 测试结果(请求参数分别为1,2,3,只有路由键对应上的队列才能消费)

五、主题模式(Topic)

       主题模式是可以根据路由键匹配规则选择性给多个消费者发送消息的模式,它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列通过路由键匹配规则绑定到交换机上去,生产者发送消息到交换机,交换机通过路由键匹配规则转发到不同队列,队列绑定的消费者接收并消费消息。

 

1. 创建队列

@Configuration
public class TopicRabbitConfig {// 创建队列@Beanpublic Queue topicOne(){return new Queue("queue.topic.one");}@Beanpublic Queue topicTwo(){return new Queue("queue.topic.two");}@Beanpublic Queue topicThree(){return new Queue("queue.topic.three");}// 创建交换机@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}//绑定队列(@BeanBinding bingingTopicOne(Queue topicOne, TopicExchange topicExchange){return BindingBuilder.bind(topicOne).to(topicExchange).with("#.error");}@BeanBinding bingingTopicTwo(Queue topicTwo, TopicExchange topicExchange){return BindingBuilder.bind(topicTwo).to(topicExchange).with("#.log");}@BeanBinding bingingTopicThree(Queue topicThree, TopicExchange topicExchange){return BindingBuilder.bind(topicThree).to(topicExchange).with("test.#.time");}}

2. 创建生产者

public void sendTopicMessage(String topic){amqpTemplate.convertAndSend("topicExchange",topic,"发布Topic消息" + topic);
}

3. 创建消费者

@RabbitListener(queues = "queue.topic.one")
public void topicOne(String in) {System.out.println("queue.topic.one:" + in);
}@RabbitListener(queues = "queue.topic.two")
public void topicTwo(String in) {System.out.println("queue.topic.two:" + in);
}@RabbitListener(queues = "queue.topic.three")
public void topicThree(String in) {System.out.println("queue.topic.three:" + in);
}

4. 测试类

@RequestMapping("/topic/{type}")
public String send(@PathVariable String type){simpleHelloSender.sendTopicMessage(type);return "发送成功";
}

5. 测试结果

这篇关于Spring AMQP实现RabbitMQ的5种消息模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/586249

相关文章

Python实现文件批量重命名器

《Python实现文件批量重命名器》在日常工作和学习中,我们经常需要对大量文件进行重命名操作,本文将介绍一个使用Python开发的文件批量重命名工具,提供了多种重命名模式,有需要的小伙伴可以了解下... 目录前言功能特点模块化设计1.目录路径获取模块2.文件列表获取模块3.重命名模式选择模块4.序列号参数配

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

SpringBoot实现多环境配置文件切换

《SpringBoot实现多环境配置文件切换》这篇文章主要为大家详细介绍了如何使用SpringBoot实现多环境配置文件切换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 示例代码结构2. pom文件3. application文件4. application-dev文

Python FastAPI实现JWT校验的完整指南

《PythonFastAPI实现JWT校验的完整指南》在现代Web开发中,构建安全的API接口是开发者必须面对的核心挑战之一,本文将深入探讨如何基于FastAPI实现JWT(JSONWebToken... 目录一、JWT认证的核心原理二、项目初始化与环境配置三、安全密码处理机制四、JWT令牌的生成与验证五、

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

Python使用Turtle实现精确计时工具

《Python使用Turtle实现精确计时工具》这篇文章主要为大家详细介绍了Python如何使用Turtle实现精确计时工具,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录功能特点使用方法程序架构设计代码详解窗口和画笔创建时间和状态显示更新计时器控制逻辑计时器重置功能事件

Linux给磁盘扩容(LVM方式)的方法实现

《Linux给磁盘扩容(LVM方式)的方法实现》本文主要介绍了Linux给磁盘扩容(LVM方式)的方法实现,涵盖PV/VG/LV概念及操作步骤,具有一定的参考价值,感兴趣的可以了解一下... 目录1 概念2 实战2.1 相关基础命令2.2 开始给LVM扩容2.3 总结最近测试性能,在本地打数据时,发现磁盘空