Springboot集成RabbitMQ并验证五种消息模型
更新时间:2024年03月08日 09:18:52 作者:道法自然 实事求是
RabbitMQ 提供了5种常用消息模型,本文主要介绍了Springboot集成RabbitMQ并验证五种消息模型,具有一定的参考价值,感兴趣的可以了解一下
一、添加依赖
<!--AMQP依赖,包含RabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- 引入knife4j-spring-ui包 /doc.html--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
二、配置文件中添加RabbitMQ访问配置
spring.rabbitmq.host=192.168.0.104 spring.rabbitmq.port=5672 spring.rabbitmq.virtual-host=/ spring.rabbitmq.username=admin spring.rabbitmq.password=admin123
三、消息生产者代码
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @Api(tags = "生产者服务") @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @ApiOperation(value = "基本消息模型") @PostMapping("/testHello") public String testHello() { rabbitTemplate.convertAndSend("hello","hello world"); return "ok"; } @ApiOperation(value = "work消息模型") @PostMapping("/testWork") public String testWork() { for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend("work","hello work!"); } return "ok"; } @ApiOperation(value = "订阅模型-Fanout 广播模式") @PostMapping("/testFanout") public String testFanout() { rabbitTemplate.convertAndSend("logs","","这是日志广播"); return "ok"; } @ApiOperation(value = "订阅模型-Direct") @PostMapping("/testDirect") public String testDirect() { rabbitTemplate.convertAndSend("directs","error","error 的日志信息"); return "ok"; } @ApiOperation(value = "订阅模型-Topic") @PostMapping("/testTopic") public String testTopic() { rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息"); return "ok"; } }
四、消息消费者代码
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.stereotype.Component; @Component public class SpringRabbitListener { //基本消息模型 @RabbitListener(queuesToDeclare = @Queue("hello")) public void receive(String message) { System.out.println("message = " + message); } //work消息模型 @RabbitListener(queuesToDeclare = @Queue("work")) public void workReceive1(String message) { System.out.println("workReceive1 message = " + message); } //work消息模型 @RabbitListener(queuesToDeclare = @Queue("work")) public void workReceive2(String message) { System.out.println("workReceive2 message = " + message); } //订阅模型-Fanout 广播模式 @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name="logs",type = "fanout"))) public void fanoutReceive(String message) { System.out.println("fanoutReceive message = " + message); } //订阅模型-Direct @RabbitListener(bindings ={@QueueBinding(value = @Queue(),key={"info","error"},exchange = @Exchange(type = "direct",name="directs"))}) public void directReceive(String message) { System.out.println("directReceive message = " + message); } //订阅模型-Topic @RabbitListener(bindings = {@QueueBinding( value = @Queue,key = {"user.*"},exchange = @Exchange(type = "topic",name = "topics"))}) public void topicReceive1(String message) { System.out.println("topicReceive1 message = " + message); } //订阅模型-Topic @RabbitListener(bindings = {@QueueBinding(value = @Queue,key = {"user.#"},exchange = @Exchange(type = "topic",name = "topics"))}) public void topicReceive2(String message) { System.out.println("topicReceive2 message = " + message); } }
五、验证
浏览器输入:ip:8080/doc.html 对接口逐个进行测试
参考资料
到此这篇关于Springboot集成RabbitMQ并验证五种消息模型的文章就介绍到这了,更多相关Springboot RabbitMQ验证消息模型内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
IDEA的默认快捷键设置与Eclipse的常用快捷键的设置方法
这篇文章主要介绍了IDEA的默认快捷键设置与Eclipse的常用快捷键的设置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01全面解析Hibernate关联操作、查询操作、高级特性、并发处理机制
这篇文章主要介绍了全面解析Hibernate关联操作、查询操作、高级特性、并发处理机制的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-06-06spring-boot-autoconfigure模块用法详解
autoconfigure就是自动配置的意思,spring-boot通过spring-boot-autoconfigure体现了"约定优于配置"这一设计原则,而spring-boot-autoconfigure主要用到了spring.factories和几个常用的注解条件来实现自动配置,思路很清晰也很简单,感兴趣的朋友跟随小编一起看看吧2022-11-11
最新评论