通过配置Redis服务器,开启其失效通知事件
主要设置方式:在redis.conf中设置
notify-keyspace-events Ex
这样在redis键值对儿失效时,就可以在服务中接收过期通知
springcloud配置:
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、创建监听器类,监听器配置类
package com.icare.bracelet.sso.consume;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.lang.Nullable;
/**
* <pre>
* author : Tianhaibo
* email : tianhaibo@jshuii.com
* time : 2018/9/18 15:34
* desc : redis过期消息监听类
* version : v1.0
* </pre>
*/
@Slf4j
@Configuration
@ConditionalOnProperty(value = "listener.redis.expire")
public class RedisExpiredListener implements MessageListener {
@Override
public void onMessage(Message message, @Nullable byte[] pattern) {