使用Redis防止重復發(fā)送RabbitMQ消息的方法詳解
問題
今天遇到一個問題,發(fā)送MQ
消息的時候需要保證不會重復發(fā)送,注意不是可靠到達(可靠到達可以通過消息確認機制和回調(diào)接口保證),這里保證的是不會生產(chǎn)多條一樣的消息。
方法
綜合討論下來決定使用Redis
緩存來解決,因為相比于將記錄插入數(shù)據(jù)庫Redis
更為高效和便捷。
檢驗是否已經(jīng)發(fā)送
在發(fā)送消息之前根據(jù)相關(guān)信息組合成key
去Redis
中查找,找到后檢測值是否為存在并且是否為設(shè)定的值,若存在且與設(shè)定的值一樣,則返回false
,說明該消息已經(jīng)發(fā)送過了。
public boolean isSend(String messageType, Long bizId, int hashCode) { // 根據(jù)消息類型、業(yè)務id和哈希值組合成key String key = this.genKey(messageType, bizId, hashCode); Long value = super.get(key); if (value != null && value.equals(DEFAULT_VALUE)) { return false; } return true; } /**get方法*/ public V get(K key) { if (key == null) { return null; } else { try { // 在key前添加前綴和名字,并將原來的key進行json序列化 String realKey = this.genRealKey(key); String content = (String)this.redisTemplate.opsForValue().get(realKey); // 若get到的值不為null則進行json反序列化 return content == null ? null : this.valueSerializer.deserialize(content); } catch (Exception e) { CACHE.error("", key.toString(), "", "0", e); return null; } } }
以上就是檢驗消息是否重復的方法,需要注意的是JSON序列化,因為Redis默認使用的是JDK序列化,這種序列化后的內(nèi)容不僅多而且不易于閱讀,因此將其改為Json序列化。
發(fā)送后添加緩存
在發(fā)送消息的時候會先在Redis中put一個以相關(guān)信息組合為key,value為默認值的記錄,過期時間為5min。
public void sendMessage(String messageType, Long bizId, int hashCode) { super.put(genKey(messageType, bizId, hashCode), DEFAULT_VALUE); } /**put方法*/ public void put(K key, V value) { try { if (key != null && null != value) { // 進行json序列化 String content = this.valueSerializer.serialize(value); this.redisTemplate.opsForValue().set(this.genRealKey(key), content, this.expires, this.timeUnit); } } catch (Throwable e) { e.printStackTrace(); } }
發(fā)送消息方法
最后的發(fā)送消息方法大致代碼如下:
public void sendMQMessage(Long bizId, String messageTemplateCode, String msg, int msgHashCode, String exchange, String routingKey) { //加入緩存 boolean send = true; //String messageType = MessageTypeUtil.getMessageType(messageTemplateCode); if (bizId != null) { // 檢測是否已經(jīng)發(fā)送 send = sendMessageCache.isSend(messageTemplateCode, bizId, msgHashCode); } //發(fā)送mq消息 if (send) { if (bizId != null) { // 加入緩存 sendMessageCache.sendMessage(messageTemplateCode, bizId, msgHashCode); } // 發(fā)送消息 messageSender.send(exchange, routingKey, msg); } }
到此這篇關(guān)于使用Redis防止重復發(fā)送RabbitMQ消息的方法詳解的文章就介紹到這了,更多相關(guān)Redis防止重復發(fā)送RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Redis結(jié)合SpringBoot的秒殺案例詳解
這篇文章主要介紹了Redis結(jié)合SpringBoot的秒殺案例,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Jedis操作Redis實現(xiàn)模擬驗證碼發(fā)送功能
Redis是一個著名的key-value存儲系統(tǒng),也是nosql中的最常見的一種,這篇文章主要給大家介紹Jedis操作Redis實現(xiàn)模擬驗證碼發(fā)送功能,感興趣的朋友一起看看吧2021-09-09