添加RabbitMQ队列的测试
This commit is contained in:
parent
e4481507fa
commit
47f79ff458
@ -21,10 +21,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-amqp</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.coobird</groupId>
|
||||
<artifactId>thumbnailator</artifactId>
|
||||
|
@ -0,0 +1,47 @@
|
||||
package xyz.longicorn.driver.config;
|
||||
|
||||
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 RabbitConfig {
|
||||
// 要订阅消息队列的主题
|
||||
public static final String RABBITMQ_DEFAULT_TOPIC = "default_message_topic";
|
||||
public static final String RABBITMQ_DEFAULT_DIRECT_EXCHANGE = "default_message_exchange";
|
||||
public static final String RABBITMQ_DEFAULT_DIRECT_ROUTING = "default_message_routing";
|
||||
// 队列
|
||||
@Bean
|
||||
public Queue rabbitmqDemoDirectQueue() {
|
||||
/**
|
||||
* 1、name: 队列名称
|
||||
* 2、durable: 是否持久化
|
||||
* 3、exclusive: 是否独享、排外的。如果设置为true,定义为排他队列。则只有创建者可以使用此队列。也就是private私有的。
|
||||
* 4、autoDelete: 是否自动删除。也就是临时队列。当最后一个消费者断开连接后,会自动删除。
|
||||
* */
|
||||
return new Queue(RABBITMQ_DEFAULT_TOPIC, true, false, false);
|
||||
}
|
||||
|
||||
// 交换机
|
||||
@Bean
|
||||
public DirectExchange rabbitmqDemoDirectExchange() {
|
||||
//Direct交换机
|
||||
return new DirectExchange(RABBITMQ_DEFAULT_DIRECT_EXCHANGE, true, false);
|
||||
}
|
||||
|
||||
// 队列与交换机的绑定
|
||||
@Bean
|
||||
public Binding bindDirect() {
|
||||
//链式写法,绑定交换机和队列,并设置匹配键
|
||||
return BindingBuilder
|
||||
//绑定队列
|
||||
.bind(rabbitmqDemoDirectQueue())
|
||||
//到交换机
|
||||
.to(rabbitmqDemoDirectExchange())
|
||||
//并设置匹配键
|
||||
.with(RABBITMQ_DEFAULT_DIRECT_ROUTING);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.longicorn.driver.consumer;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.longicorn.driver.config.RabbitConfig;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@RabbitListener(queues = RabbitConfig.RABBITMQ_DEFAULT_TOPIC) // 设置要监听队列的主题名称
|
||||
@Slf4j
|
||||
public class RabbitMessageConsumer {
|
||||
// 处理消息的方法 参数取决去发送消息的数据类型
|
||||
@RabbitHandler
|
||||
public void processMessage(Map<String, Object> map) {
|
||||
log.info("获取到一条消息:" + map.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.longicorn.driver.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xyz.longicorn.driver.service.RabbitMQService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController
|
||||
public class MessageQueueController {
|
||||
|
||||
@Resource
|
||||
private RabbitMQService rabbitMQService;
|
||||
|
||||
@RequestMapping("/send-msg")
|
||||
public String sendMessage(String message) {
|
||||
rabbitMQService.sendMessage(message);
|
||||
return "send message success";
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package xyz.longicorn.driver.service;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xyz.longicorn.driver.config.RabbitConfig;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class RabbitMQService {
|
||||
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public boolean sendMessage(String message) {
|
||||
try {
|
||||
String msgId = UUID.fastUUID().toString();
|
||||
String sendTime = sdf.format(new Date());
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", msgId);
|
||||
map.put("sendTime", sendTime);
|
||||
map.put("message", message);
|
||||
|
||||
// 发送
|
||||
rabbitTemplate.convertAndSend(
|
||||
RabbitConfig.RABBITMQ_DEFAULT_DIRECT_EXCHANGE, // 接收消息的交换机
|
||||
RabbitConfig.RABBITMQ_DEFAULT_DIRECT_ROUTING, // 发送消息的路由
|
||||
map);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -61,7 +61,11 @@ spring:
|
||||
# redis缓存
|
||||
redis:
|
||||
database: 1
|
||||
|
||||
rabbitmq:
|
||||
host: 127.0.0.1
|
||||
port: 5672
|
||||
username: client
|
||||
password: 123123
|
||||
mvc:
|
||||
pathmatch:
|
||||
# 因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
|
||||
|
Loading…
x
Reference in New Issue
Block a user