项目背景
公司的客户快速响应系统每天都会接到十几万的客户反馈信息,而这些信息根据紧急情况如果规定的时间内没有处理,需要给相应的负责人发送短信进行提醒。目前使用的方式是利用定时任务对数据库进行扫描,捞取未完成的订单判断是否超时(这里有一个坑,有点经验的程序员可能会问下产品一个订单大概周期多久然后然后捞数据时规定时间。那些菜鸟很可能就直接全表扫描了),然后发送短信。
这样做有个很明显的缺点,1 这样做订单主表数据量数据量非常大频繁扫描明显会增加数据库压力 2 时间控制不够精准
于是我决定利用MQ来处理这个定时任务问题
方法一:rabbitMq延时队列(以下只是样例并非实际工作代码)
1 添加配置
# 延迟消息配置
spring.cloud.stream.bindings.delayed-consumer.destination=delayed-topic
spring.cloud.stream.bindings.delayed-producer.destination=delayed-topic
spring.cloud.stream.rabbit.bindings.delayed-producer.producer.delayed-exchange=true
2 声明队列
public interface DlqTopic {
String INPUT = "dlq-consumer";
String OUTPUT = "dlq-producer";
@Input(INPUT)
SubscribableChannel input();
@Output(OUTPUT)
MessageChannel output();
}
3 配置生产者
// 延迟消息
@PostMapping("sendDM")
public void sendDelayedMessage(
@RequestParam(value = "body") String body,
@RequestParam(value = "seconds") Integer seconds) {
MessageBean msg = new MessageBean();
msg.setPayload(body);
log.info("ready to send delayed message {}", body);
delayedTopicProducer.output().send(
MessageBuilder.withPayload(msg)
.setHeader("x-delay", seconds * 1000)
.build());
}
4 配置消费者
@EnableBinding(value = {
DelayedTopic.class,
}
)
public class StreamConsumer {
private AtomicInteger count = new AtomicInteger(1);
// 延迟消息示例
@StreamListener(DelayedTopic.INPUT)
public void consumeDelayedMessage(MessageBean bean) {
log.info("Delayed message consumed successfully, payload={}", bean.getPayload());
}
}
方法二:利用死信队列
注死信队列有关介绍
1 添加队列配置
@Slf4j
@Configuration
public class DelayQueueConfig {
/**
* 延迟队列
*/
public static final String DELAY_EXCHANGE = "delay.queue.business.exchange";
public static final String DELAY_QUEUE = "delay.queue.business.queue";
public static final String DELAY_QUEUE_ROUTING_KEY = "delay.queue.business.queue.routingKey";
/**
* 死信队列
*/
public static final String DEAD_LETTER_EXCHANGE = "delay.queue.deadLetter.exchange";
public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "delay.queue.deadLetter.delay_10s.routingKey";
public static final String DEAD_LETTER_QUEUE = "delay.queue.deadLetter.queue";
/**
* 声明 死信交换机
* @return deadLetterExchange
*/
@Bean
public DirectExchange deadLetterExchange() {
return new DirectExchange(DEAD_LETTER_EXCHANGE);
}
/**
* 声明 死信队列 用于接收死信消息
* @return deadLetterQueueA
*/
@Bean
public Queue deadLetterQueueA() {
return new Queue(DEAD_LETTER_QUEUE);
}
/**
* 将 死信队列 绑定到死信交换机上
* @return deadLetterBindingA
*/
@Bean
public Binding deadLetterBindingA() {
return BindingBuilder
.bind(deadLetterQueueA())
.to(deadLetterExchange())
.with(DEAD_LETTER_QUEUE_ROUTING_KEY);
}
/**
* 声明 延时交换机
* @return delayExchange
*/
@Bean
public DirectExchange directExchange() {
return new DirectExchange(DELAY_EXCHANGE);
}
/**
* 将 延时队列 绑定参数
* @return Queue
*/
@Bean
public Queue delayQueueA() {
Map<String, Object> maps = Maps.newHashMapWithExpectedSize(3);
// 队列绑定DLX参数(关键一步)
maps.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
// 队列绑定 死信RoutingKey参数
maps.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
// 消息过期采用第一种设置队列的 ttl 时间,消息过期时间全部相同。 单位:毫秒,这里设置为8秒
maps.put("x-message-ttl", 8000);
return QueueBuilder.durable(DELAY_QUEUE).withArguments(maps).build();
}
/**
* 将 延时队列 绑定到延时交换机上面
* @return delayBindingA
*/
@Bean
public Binding delayBindingA() {
return BindingBuilder
.bind(delayQueueA())
.to(directExchange())
.with(DELAY_QUEUE_ROUTING_KEY);
}
}
2 配置生产者
public interface RabbitMqService {
/**
* 统一发送mq
*
* @param exchange 交换机
* @param routingKey 路由key
* @param msg 消息
* @param ttl 过期时间
*/
void send(String exchange, String routingKey, String msg, Integer ttl);
}
@Service
@Slf4j
public class RabbitMqServiceImpl implements RabbitMqService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void send(String exchange, String routingKey, String msg, Integer ttl) {
MessageProperties messageProperties = new MessageProperties();
// 第二种方式设置消息过期时间
messageProperties.setExpiration(ttl.toString());
// 构建一个消息对象
Message message = new Message(msg.getBytes(), messageProperties);
// 发送RabbitMq消息
rabbitTemplate.convertAndSend(exchange, routingKey, message);
}
}
3 配置消费者
@Component
@Slf4j
public class DelayMsgConsumer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(DelayQueueConfig.DEAD_LETTER_QUEUE),
exchange = @Exchange(DelayQueueConfig.DEAD_LETTER_EXCHANGE)))
public void queueAConsumer(Message message) {
Msg msg = JSONObject.parseObject(new String(message.getBody()), Msg.class);
LocalDateTime now = LocalDateTime.now();
Duration duration = Duration.between(msg.getTime(), now);
log.info("DelayMsgConsumer死信队列消费---->Msg:{}, 发送时间:{}, 当前时间:{}, 相差时间:{}秒,消息设置的ttl:{}",
JSONObject.toJSONString(msg),
localDateTimeToString(msg.getTime()),
localDateTimeToString(now),
duration.getSeconds(),
msg.getTtl());
}
@Data
public static class Msg {
private String ttl;
private String msg;
private LocalDateTime time;
}
private String localDateTimeToString(LocalDateTime localDateTime){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return dateTimeFormatter.format(localDateTime);
}
}```