使用redis實現(xiàn)延遲通知功能(Redis過期鍵通知)
Redis 過期監(jiān)聽場景
業(yè)務中有類似等待一定時間之后執(zhí)行某種行為的需求 , 比如 30 分鐘之后關閉訂單 . 網(wǎng)上有很多使用 Redis 過期監(jiān)聽的 Demo
redis配置
把notify-keyspace-events Ex 這一行的注釋打開
項目demo工程
項目結(jié)構如下圖
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ù)庫索引 database: 0 host: 127.0.0.1 port: 6379 password: 123456 lettuce: pool: #最大連接數(shù) max-active: 8 #最大阻塞等待時間(負數(shù)表示沒限制) max-wait: -1 #最大空閑 max-idle: 8 #最小空閑 min-idle: 0 #連接超時時間 timeout: 10000
啟動類
/** * @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); } }
異常情況測試
當key過期時,項目宕機了
①寫入redis的key
②手動關停服務,等待redis的key過期
③確認redis的key過期后,重啟服務。服務不會收到通知
當key過期時,redis服務宕機了
①寫入redis的key
②關停redis服務,等待redis的key過期
③啟動redis服務,發(fā)現(xiàn)redis的過期key已經(jīng)不存在了,服務沒有收到通知
結(jié)論
redis的鍵過期本身不可靠,并不像rabbitmq一樣保證了可靠性。
當服務本身宕機或者redis宕機時,將無法保證過期的key能夠被消費。
當使用場景對數(shù)據(jù)完整性不那么精確時,可以使用redis的鍵過期策略。否則不太建議使用redis的鍵過期策略。
到此這篇關于使用redis實現(xiàn)延遲通知功能(Redis過期鍵通知)的文章就介紹到這了,更多相關Redis過期鍵通知內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
完美解決Redis在雙擊redis-server.exe出現(xiàn)閃退問題
本文主要介紹了完美解決Redis在雙擊redis-server.exe出現(xiàn)閃退問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01聊聊使用RedisTemplat實現(xiàn)簡單的分布式鎖的問題
這篇文章主要介紹了使用RedisTemplat實現(xiàn)簡單的分布式鎖問題,文中給大家介紹在SpringBootTest中編寫測試模塊的詳細代碼,需要的朋友可以參考下2021-11-11利用redis實現(xiàn)分布式鎖,快速解決高并發(fā)時的線程安全問題
這篇文章主要介紹了利用redis實現(xiàn)分布式鎖,快速解決高并發(fā)時的線程安全問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01