使用场景,不限于下面
- 用户下订单结束,如果用户未及时支付,后续需要取消订单,可以怎么做?定时任务可以做,但是不能接近实时或者消耗数据库性能太大。
- 【数据库定时任务方案】:定时任务可以做到订单的状态的改变,但是数据库定时任务是定时执行的,而我们订单状态改变的时间确是随时可以发生的,所以定时任务不能做到准实时。
- 【消息中间件方案】:我们可以将消息投递到中间件里面,通常设置一个延迟时间,精确到某个时刻进行消费,那么可以实现准实时。
- 有一个推送,具体是这样的:我们需要在某个时刻发送给某个用户消息,而这个时刻是某个时间过多久之后的时刻,怎么做?
- 利用消息中间件的延迟消息特点,将消息放入后固定时间后消费,可以完美解决。但是注意每个中间件延迟时间是否有限制。
RabbitMQ延迟消息实现方式一【基于rabbitmq_delayed_message_exchange插件方式】
经过多次测试,插件的方式不能使用RabbitMQ的事务消息,所以推荐第二种方式。
软件下载地址
https://www.rabbitmq.com/download.html
安装过程略!网上有很多教程。
插件下载地址
https://www.rabbitmq.com/community-plugins [千万记住,一定选好版本号。如果没有选对版本,在使用延迟消息的时候,会遇到各种各样的奇葩问题,而且网上还找不到解决方案]
插件安装
启用插件 rabbitmq-plugins enable rabbitmq_delayed_message_exchange [启动插件成功后,重启RabbitMQ,让其生效]
SpringBoot集成RabbitMQ
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml配置
#配置rabbitMq 服务器
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxx
spring.rabbitmq.password=xxx
spring.rabbitmq.publisher-confirms=false
spring.rabbitmq.publisher-returns=false
spring.rabbitmq.listener.simple.acknowledge-mode=manual
# 重试机制(默认开启并一直重试),最大尝试五次,每次间隔3秒
spring.rabbitmq.listener.simple.retry.enabled=true
spring.rabbitmq.listener.simple.retry.max-attempts=5
spring.rabbitmq.listener.simple.retry.initial-interval=3000
spring.rabbitmq.virtual=
延迟消息配置
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* 延迟消息
* @author niky
*/
@Configuration
public class DelayMsgConfig {
public static final String DELAY_EXCHANGE = "delay_exchange";
public static final String DELAY_KEY = "delay_key";
public static final String DELAY_QUEUE = "delay_queue";
@Bean
public CustomExchange delayExchange() {
Map<String, Object> args = new HashMap<>(1);
args.put("x-delayed-type", "direct");
return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
}
@Bean
public Queue delayQueue() {
return new Queue(DELAY_QUEUE, true);
}
@Bean
public Binding cfgDelayBinding() {
return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(DELAY_KEY).noargs();
}
/**
* 配置启用rabbitmq事务
*/
@Bean
public RabbitTransactionManager rabbitTransactionManager(CachingConnectionFactory connectionFactory) {
return new RabbitTransactionManager(connectionFactory);
}
}
生产者发送消息
@Override
public void sendDelayMsg(DelayMsgPushStatus msg) {
//这里的消息可以是任意对象,无需额外配置,直接传即可
logger.info("===============延时队列生产消息====================");
logger.info("发送时间:{},发送内容:{}", LocalDateTime.now(), JSON.toJSONString(msg));
this.rabbitTemplate.convertAndSend(
DelayMsgConfig.DELAY_EXCHANGE,
DelayMsgConfig.DELAY_KEY,
msg.getMsg(),
message -> {
//注意这里时间可以使long,而且是设置header
message.getMessageProperties().setHeader("x-delay", msg.getDelayTime());
return message;
}