溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用redis實現(xiàn)延遲通知功能

發(fā)布時間:2021-09-03 21:27:18 來源:億速云 閱讀:101 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用redis實現(xiàn)延遲通知功能”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Redis 過期監(jiān)聽場景

業(yè)務(wù)中有類似等待一定時間之后執(zhí)行某種行為的需求 , 比如 30 分鐘之后關(guān)閉訂單 . 網(wǎng)上有很多使用 Redis 過期監(jiān)聽的 Demo

redis配置

 把notify-keyspace-events Ex 這一行的注釋打開

怎么用redis實現(xiàn)延遲通知功能

項目demo工程

項目結(jié)構(gòu)如下圖

怎么用redis實現(xiàn)延遲通知功能

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
        #最大阻塞等待時間(負(fù)數(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);
    }
}

異常情況測試

當(dāng)key過期時,項目宕機(jī)了
①寫入redis的key
②手動關(guān)停服務(wù),等待redis的key過期
③確認(rèn)redis的key過期后,重啟服務(wù)。服務(wù)不會收到通知

當(dāng)key過期時,redis服務(wù)宕機(jī)了
①寫入redis的key
②關(guān)停redis服務(wù),等待redis的key過期
③啟動redis服務(wù),發(fā)現(xiàn)redis的過期key已經(jīng)不存在了,服務(wù)沒有收到通知

結(jié)論

redis的鍵過期本身不可靠,并不像rabbitmq一樣保證了可靠性。
當(dāng)服務(wù)本身宕機(jī)或者redis宕機(jī)時,將無法保證過期的key能夠被消費。

當(dāng)使用場景對數(shù)據(jù)完整性不那么精確時,可以使用redis的鍵過期策略。否則不太建議使用redis的鍵過期策略。

“怎么用redis實現(xiàn)延遲通知功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI