参考:https://blog.csdn.net/qq_35387940/article/details/100514134
1、说明
Springboot
集成RabbitMQ
实践,使用的Springboot 2.4.5
2、集成过程
2.1 消息发送方(provider
端)
- 引入
mq
集成包pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--··省略部分依赖包·-->
<!--amqp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
-
properties
文件配置
# 端口号
server.port=8000
# 应用名称 --mq生产端
spring.application.name=rabbitmq-provider
# rabbitMq 配置
# host
spring.rabbitmq.host=127.0.0.1
# 中间件端口号
spring.rabbitmq.port=5672
#用户名,默认guest
spring.rabbitmq.username=guest
#密码,默认guest
spring.rabbitmq.password=guest
- 配置队列
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectRabbitConfig {
//队列 起名:TestDirectQueue
@Bean
public Queue TestDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("TestDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue("TestDirectQueue",true);
}
//Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange() {
// return new DirectExchange("TestDirectExchange",true,true);
return new DirectExchange("TestDirectExchange",true,false);
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
DirectExchange lonelyDirectExchange() {
return new DirectExchange("lonelyDirectExchange");
}
- 发送消息
package com.lsy.mqprovider.controller;
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;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping("/api/rabbitmq")
public class SendMessageController {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@GetMapping("/sendDirectMessage")
public String sendDirectMessage() {
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 发送到交换机TestDirectExchange
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
return "ok";
}
}
-
postman测试发送消息
image.png
-
浏览器队列中查看:
localhost:15672
image.png
2.2 消息消费端(Consumer端)
-
pom.xml
、properties
配置同provider
一致 - 增加监听:
@RabbitListener(queues = "TestDirectQueue")
@Component
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
}
}
-启动运行
2021-05-27 14:05:46.374 INFO 51719 --- [ restartedMain] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#190c2e67:0/SimpleConnection@33e1c306 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 61468]
2021-05-27 14:05:46.498 INFO 51719 --- [ restartedMain] com.lsy.rocketmq.RocketMqApplication : Started RocketMqApplication in 2.351 seconds (JVM running for 3.918)
DirectReceiver消费者收到消息 : {createTime=2021-05-27 13:56:47, messageId=355fd887-1617-4fae-8604-a7c637a1afb0, messageData=test message, hello!}
总结:
以上只是简单将RabbitMQ
与Springboot
集成实用起来,感受一下Springboot如何应用MQ
消息中间件的。更多知识需要系统学习RabbitMQ
知识。
网友评论