使用Redis防止重復(fù)發(fā)送RabbitMQ消息的方法詳解
問題
今天遇到一個問題,發(fā)送MQ消息的時候需要保證不會重復(fù)發(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è)務(wù)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;
}
}
}
以上就是檢驗消息是否重復(fù)的方法,需要注意的是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ù)發(fā)送RabbitMQ消息的方法詳解的文章就介紹到這了,更多相關(guān)Redis防止重復(fù)發(fā)送RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis?bitmap數(shù)據(jù)結(jié)構(gòu)之java對等操作詳解
bitmap是以其高性能出名。其基本原理是一位存儲一個標識,其他衍生知道咱就不說了,而redis就是以這種原生格式存儲的,這篇文章主要介紹了redis?bitmap數(shù)據(jù)結(jié)構(gòu)之java對等操作,需要的朋友可以參考下2022-10-10
批量導(dǎo)入txt數(shù)據(jù)到的redis過程
用戶通過將Redis命令逐行寫入txt文件,利用管道模式運行客戶端,成功執(zhí)行批量刪除以"Product*"匹配的Key操作,提高了數(shù)據(jù)清理效率2025-08-08
在Ubuntu?14.04系統(tǒng)上備份和恢復(fù)Redis數(shù)據(jù)詳細步驟
這篇文章主要給大家介紹了關(guān)于在Ubuntu?14.04系統(tǒng)上備份和恢復(fù)Redis數(shù)據(jù)的詳細步驟,文中通過代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Redis具有一定的參考借鑒價值,需要的朋友可以參考下2024-04-04
Redis?異常?read?error?on?connection?的解決方案
這篇文章主要介紹了Redis異常read?error?on?connection的解決方案,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08
Redis 數(shù)據(jù)庫忘記密碼找回或重置的解決方法
對于 Redis 數(shù)據(jù)庫,如果忘記了密碼,可以通過密碼重置來找回密碼,今天通過本文給大家分享Redis 數(shù)據(jù)庫忘記密碼找回或重置的解決方法,感興趣的朋友一起看看吧2024-01-01
redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實現(xiàn)詳解
這篇文章主要為大家介紹了redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Redis的數(shù)據(jù)過期策略和數(shù)據(jù)淘汰策略
本文主要介紹了Redis的數(shù)據(jù)過期策略和數(shù)據(jù)淘汰策略,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

