使用Redis防止重復發(fā)送RabbitMQ消息的方法詳解
問題
今天遇到一個問題,發(fā)送MQ消息的時候需要保證不會重復發(fā)送,注意不是可靠到達(可靠到達可以通過消息確認機制和回調接口保證),這里保證的是不會生產多條一樣的消息。
方法
綜合討論下來決定使用Redis緩存來解決,因為相比于將記錄插入數(shù)據(jù)庫Redis更為高效和便捷。
檢驗是否已經(jīng)發(fā)送
在發(fā)送消息之前根據(jù)相關信息組合成key去Redis中查找,找到后檢測值是否為存在并且是否為設定的值,若存在且與設定的值一樣,則返回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序列化,這種序列化后的內容不僅多而且不易于閱讀,因此將其改為Json序列化。
發(fā)送后添加緩存
在發(fā)送消息的時候會先在Redis中put一個以相關信息組合為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);
}
}
到此這篇關于使用Redis防止重復發(fā)送RabbitMQ消息的方法詳解的文章就介紹到這了,更多相關Redis防止重復發(fā)送RabbitMQ內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Redis哨兵主備切換的數(shù)據(jù)丟失問題及解決
主備切換過程中可能會導致數(shù)據(jù)丟失,異步復制和腦裂是兩種主要原因,異步復制可能導致部分數(shù)據(jù)未復制到slave而master宕機,腦裂則可能導致多個master存在,舊master恢復后數(shù)據(jù)被清空,從而丟失數(shù)據(jù)2024-12-12

