業(yè)務(wù)中有類似等待一定時(shí)間之后執(zhí)行某種行為的需求 , 比如 30 分鐘之后關(guān)閉訂單 . 網(wǎng)上有很多使用 Redis 過期監(jiān)聽的 Demo
redis配置
把notify-keyspace-events Ex 這一行的注釋打開
項(xiàng)目結(jié)構(gòu)如下圖
maven依賴
?xml version="1.0" encoding="UTF-8"?> project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> parent> artifactId>kim-redis/artifactId> groupId>com.kim/groupId> version>1.0.0/version> /parent> modelVersion>4.0.0/modelVersion> artifactId>kim-redis-expiration-notice/artifactId> dependencies> dependency> groupId>org.springframework.boot/groupId> artifactId>spring-boot-starter-web/artifactId> /dependency> dependency> groupId>org.springframework.boot/groupId> artifactId>spring-boot-starter-data-redis/artifactId> /dependency> dependency> groupId>org.projectlombok/groupId> artifactId>lombok/artifactId> /dependency> dependency> groupId>org.apache.commons/groupId> artifactId>commons-pool2/artifactId> /dependency> /dependencies> /project>
配置文件
server: port: 20103 spring: redis: #數(shù)據(jù)庫(kù)索引 database: 0 host: 127.0.0.1 port: 6379 password: 123456 lettuce: pool: #最大連接數(shù) max-active: 8 #最大阻塞等待時(shí)間(負(fù)數(shù)表示沒限制) max-wait: -1 #最大空閑 max-idle: 8 #最小空閑 min-idle: 0 #連接超時(shí)時(shí)間 timeout: 10000
啟動(dòng)類
/** * @Project: kim-redis * @PackageName: com.kim.redis.expiration.notice * @FileName: NoticeApplication.java * @Description: The NoticeApplication is... * @Author: kimwu * @Time: 2020-12-19 14:01:56 */ @SpringBootApplication public class NoticeApplication { public static void main(String[] args) { SpringApplication.run(NoticeApplication.class, args); } }
配置類
@Configuration public class RedisTimeoutConfiguration { @Autowired private RedisConnectionFactory redisConnectionFactory; @Bean public RedisMessageListenerContainer redisMessageListenerContainer() { RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory); return redisMessageListenerContainer; } @Bean public KeyExpiredListener keyExpiredListener() { return new KeyExpiredListener(this.redisMessageListenerContainer()); } }
監(jiān)聽類
@Slf4j public class KeyExpiredListener extends KeyExpirationEventMessageListener { public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String channel = new String(message.getChannel(), StandardCharsets.UTF_8); //過期的key String key = new String(message.getBody(), StandardCharsets.UTF_8); log.info("redis key 過期:pattern={},channel={},key={}", new String(pattern), channel, key); } }
當(dāng)key過期時(shí),項(xiàng)目宕機(jī)了
①寫入redis的key
②手動(dòng)關(guān)停服務(wù),等待redis的key過期
③確認(rèn)redis的key過期后,重啟服務(wù)。服務(wù)不會(huì)收到通知
當(dāng)key過期時(shí),redis服務(wù)宕機(jī)了
①寫入redis的key
②關(guān)停redis服務(wù),等待redis的key過期
③啟動(dòng)redis服務(wù),發(fā)現(xiàn)redis的過期key已經(jīng)不存在了,服務(wù)沒有收到通知
redis的鍵過期本身不可靠,并不像rabbitmq一樣保證了可靠性。
當(dāng)服務(wù)本身宕機(jī)或者redis宕機(jī)時(shí),將無法保證過期的key能夠被消費(fèi)。
當(dāng)使用場(chǎng)景對(duì)數(shù)據(jù)完整性不那么精確時(shí),可以使用redis的鍵過期策略。否則不太建議使用redis的鍵過期策略。
到此這篇關(guān)于使用redis實(shí)現(xiàn)延遲通知功能(Redis過期鍵通知)的文章就介紹到這了,更多相關(guān)Redis過期鍵通知內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:楊凌 朝陽(yáng) 臺(tái)州 吉安 北京 江蘇 大慶 果洛
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用redis實(shí)現(xiàn)延遲通知功能(Redis過期鍵通知)》,本文關(guān)鍵詞 使用,redis,實(shí)現(xiàn),延遲,通知,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。