Redis 过期监听场景
业务中有类似等待一定时间之后执行某种行为的需求 , 比如 30 分钟之后关闭订单 . 网上有很多使用 Redis 过期监听的 Demo
redis配置
 把notify-keyspace-events Ex 这一行的注释打开

项目demo工程
项目结构如下图

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:
    #数据库索引
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 123456
    lettuce:
      pool:
        #最大连接数
        max-active: 8
        #最大阻塞等待时间(负数表示没限制)
        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());
    }
}
 
监听类
@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服务,发现redis的过期key已经不存在了,服务没有收到通知
结论
redis的键过期本身不可靠,并不像rabbitmq一样保证了可靠性。
当服务本身宕机或者redis宕机时,将无法保证过期的key能够被消费。
当使用场景对数据完整性不那么精确时,可以使用redis的键过期策略。否则不太建议使用redis的键过期策略。
到此这篇关于使用redis实现延迟通知功能(Redis过期键通知)的文章就介绍到这了,更多相关Redis过期键通知内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
                            
                            
                                您可能感兴趣的文章:- redis学习之RDB、AOF与复制时对过期键的处理教程
- 大家都应该知道的Redis过期键与过期策略
- Redis 2.8-4.0过期键优化过程全纪录
- redis键空间通知使用实现
- Redis开启键空间通知实现超时通知的步骤详解