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

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优