Spring Cloud Stream RocketMQ 延时消息发送:5秒延时失效问题及解决方案
在Spring Cloud Stream框架中使用RocketMQ发送延时消息时,需要注意以下几点:
-
确保RocketMQ的版本在4.3.0及以上,因为在这个版本之前,RocketMQ不支持延时消息。
-
在应用程序的配置文件中,需要设置RocketMQ的延时级别。可以通过以下配置项来设置延时级别:
spring.cloud.stream.rocketmq.binder.delayed-levels='1s,5s,10s,30s,1m,2m,3m,4m,5m,6m,7m,8m,9m,10m,20m,30m,1h,2h'这里设置了延时级别为1s、5s、10s、30s、1m等。
-
在发送消息时,通过设置消息的延时级别来实现延时发送。可以在发送消息的方法上添加
@Headers注解,设置消息头的RocketMQ_DELAY_TIME_LEVEL属性,例如:@StreamListener('output') public void handleMessage(String message, @Headers RocketMQHeaders rocketMQHeaders) { // 设置延时级别为5s rocketMQHeaders.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL, '2'); // 发送消息 rocketMQTemplate.send('topic', message, rocketMQHeaders); }这里设置了延时级别为2,对应的延时时间为5s。
注意:延时消息的具体延时时间由RocketMQ决定,可能会有一定的误差。另外,确保RocketMQ的Broker端也进行了相关配置,以支持延时消息的发送和消费。
原文地址: https://www.cveoy.top/t/topic/phlk 著作权归作者所有。请勿转载和采集!