您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringBoot怎么集成Redisson實(shí)現(xiàn)延遲隊(duì)列的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
1、下單成功,30分鐘未支付。支付超時(shí),自動(dòng)取消訂單
2、訂單簽收,簽收后7天未進(jìn)行評(píng)價(jià)。訂單超時(shí)未評(píng)價(jià),系統(tǒng)默認(rèn)好評(píng)
3、下單成功,商家5分鐘未接單,訂單取消
4、配送超時(shí),推送短信提醒
……
對(duì)于延時(shí)比較長(zhǎng)的場(chǎng)景、實(shí)時(shí)性不高的場(chǎng)景,我們可以采用任務(wù)調(diào)度的方式定時(shí)輪詢(xún)處理。如:xxl-job
今天我們采用一種比較簡(jiǎn)單、輕量級(jí)的方式,使用 Redis 的延遲隊(duì)列來(lái)進(jìn)行處理。當(dāng)然有更好的解決方案,可根據(jù)公司的技術(shù)選型和業(yè)務(wù)體系選擇最優(yōu)方案。如:使用消息中間件Kafka、RabbitMQ 的延遲隊(duì)列
先不討論其實(shí)現(xiàn)原理,直接實(shí)戰(zhàn)上代碼先實(shí)現(xiàn)基于 Redis 的延遲隊(duì)列
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.10.5</version> </dependency>
spring: redis: host: 127.0.0.1 port: 6379 password: 123456 database: 12 timeout: 3000
/** * Created by LPB on 2020/04/20. */ @Configuration public class RedissonConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.password}") private String password; @Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer() .setAddress("redis://" + host + ":" + port) .setDatabase(database) .setPassword(password); return Redisson.create(config); } }
/** * redis延遲隊(duì)列工具 * Created by LPB on 2021/04/20. */ @Slf4j @Component public class RedisDelayQueueUtil { @Autowired private RedissonClient redissonClient; /** * 添加延遲隊(duì)列 * @param value 隊(duì)列值 * @param delay 延遲時(shí)間 * @param timeUnit 時(shí)間單位 * @param queueCode 隊(duì)列鍵 * @param <T> */ public <T> void addDelayQueue(T value, long delay, TimeUnit timeUnit, String queueCode){ try { RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(queueCode); RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque); delayedQueue.offer(value, delay, timeUnit); log.info("(添加延時(shí)隊(duì)列成功) 隊(duì)列鍵:{},隊(duì)列值:{},延遲時(shí)間:{}", queueCode, value, timeUnit.toSeconds(delay) + "秒"); } catch (Exception e) { log.error("(添加延時(shí)隊(duì)列失敗) {}", e.getMessage()); throw new RuntimeException("(添加延時(shí)隊(duì)列失敗)"); } } /** * 獲取延遲隊(duì)列 * @param queueCode * @param <T> * @return * @throws InterruptedException */ public <T> T getDelayQueue(String queueCode) throws InterruptedException { RBlockingDeque<Map> blockingDeque = redissonClient.getBlockingDeque(queueCode); T value = (T) blockingDeque.take(); return value; } }
/** * 延遲隊(duì)列業(yè)務(wù)枚舉 * Created by LPB on 2021/04/20. */ @Getter @NoArgsConstructor @AllArgsConstructor public enum RedisDelayQueueEnum { ORDER_PAYMENT_TIMEOUT("ORDER_PAYMENT_TIMEOUT","訂單支付超時(shí),自動(dòng)取消訂單", "orderPaymentTimeout"), ORDER_TIMEOUT_NOT_EVALUATED("ORDER_TIMEOUT_NOT_EVALUATED", "訂單超時(shí)未評(píng)價(jià),系統(tǒng)默認(rèn)好評(píng)", "orderTimeoutNotEvaluated"); /** * 延遲隊(duì)列 Redis Key */ private String code; /** * 中文描述 */ private String name; /** * 延遲隊(duì)列具體業(yè)務(wù)實(shí)現(xiàn)的 Bean * 可通過(guò) Spring 的上下文獲取 */ private String beanId; }
/** * 延遲隊(duì)列執(zhí)行器 * Created by LPB on 2021/04/20. */ public interface RedisDelayQueueHandle<T> { void execute(T t); }
OrderPaymentTimeout:訂單支付超時(shí)延遲隊(duì)列處理類(lèi)
/** * 訂單支付超時(shí)處理類(lèi) * Created by LPB on 2021/04/20. */ @Component @Slf4j public class OrderPaymentTimeout implements RedisDelayQueueHandle<Map> { @Override public void execute(Map map) { log.info("(收到訂單支付超時(shí)延遲消息) {}", map); // TODO 訂單支付超時(shí),自動(dòng)取消訂單處理業(yè)務(wù)... } }
OrderTimeoutNotEvaluated:訂單超時(shí)未評(píng)價(jià)延遲隊(duì)列處理類(lèi)
/** * 訂單超時(shí)未評(píng)價(jià)處理類(lèi) * Created by LPB on 2021/04/20. */ @Component @Slf4j public class OrderTimeoutNotEvaluated implements RedisDelayQueueHandle<Map> { @Override public void execute(Map map) { log.info("(收到訂單超時(shí)未評(píng)價(jià)延遲消息) {}", map); // TODO 訂單超時(shí)未評(píng)價(jià),系統(tǒng)默認(rèn)好評(píng)處理業(yè)務(wù)... } }
/** * 啟動(dòng)延遲隊(duì)列 * Created by LPB on 2021/04/20. */ @Slf4j @Component public class RedisDelayQueueRunner implements CommandLineRunner { @Autowired private RedisDelayQueueUtil redisDelayQueueUtil; @Override public void run(String... args) { new Thread(() -> { while (true){ try { RedisDelayQueueEnum[] queueEnums = RedisDelayQueueEnum.values(); for (RedisDelayQueueEnum queueEnum : queueEnums) { Object value = redisDelayQueueUtil.getDelayQueue(queueEnum.getCode()); if (value != null) { RedisDelayQueueHandle redisDelayQueueHandle = SpringUtil.getBean(queueEnum.getBeanId()); redisDelayQueueHandle.execute(value); } } } catch (InterruptedException e) { log.error("(Redis延遲隊(duì)列異常中斷) {}", e.getMessage()); } } }).start(); log.info("(Redis延遲隊(duì)列啟動(dòng)成功)"); } }
以上步驟,Redis 延遲隊(duì)列核心代碼已經(jīng)完成,下面我們寫(xiě)一個(gè)測(cè)試接口,用 PostMan 模擬測(cè)試一下
/** * 延遲隊(duì)列測(cè)試 * Created by LPB on 2020/04/20. */ @RestController public class RedisDelayQueueController { @Autowired private RedisDelayQueueUtil redisDelayQueueUtil; @PostMapping("/addQueue") public void addQueue() { Map<String, String> map1 = new HashMap<>(); map1.put("orderId", "100"); map1.put("remark", "訂單支付超時(shí),自動(dòng)取消訂單"); Map<String, String> map2 = new HashMap<>(); map2.put("orderId", "200"); map2.put("remark", "訂單超時(shí)未評(píng)價(jià),系統(tǒng)默認(rèn)好評(píng)"); // 添加訂單支付超時(shí),自動(dòng)取消訂單延遲隊(duì)列。為了測(cè)試效果,延遲10秒鐘 redisDelayQueueUtil.addDelayQueue(map1, 10, TimeUnit.SECONDS, RedisDelayQueueEnum.ORDER_PAYMENT_TIMEOUT.getCode()); // 訂單超時(shí)未評(píng)價(jià),系統(tǒng)默認(rèn)好評(píng)。為了測(cè)試效果,延遲20秒鐘 redisDelayQueueUtil.addDelayQueue(map2, 20, TimeUnit.SECONDS, RedisDelayQueueEnum.ORDER_TIMEOUT_NOT_EVALUATED.getCode()); } }
通過(guò) Redis 客戶(hù)端可看到兩個(gè)延遲隊(duì)列已添加成功
查看 IDEA 控制臺(tái)日志可看到延遲隊(duì)列已消費(fèi)成功
以上就是“SpringBoot怎么集成Redisson實(shí)現(xiàn)延遲隊(duì)列”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。