Redis单机及集群实现key值过期监听机制
先上代码,redis 集群实现key值过期监听策略
如下配置⽂档,参考;
修改f 的记录,该值默认"",然后重启redis即可(也可以config动态修改,但是重启后,就没⽤了..),所以⼀步到位,改配置⽂件.
执⾏验证:
该值已经证明,key空间时间,可以监听了....xE表⽰的含义,偷⼀张图可见...哈哈
PSUBSCRIBE __keyevent@*__:expired
启动监听...然后在同⼀节点上,起不同的连接进⾏监听
测试监听没问题,通过....
单机版:
配置⽂件:
spring:
redis:
host: 192.168.241.239
password: 123456
port: 6379
配置环境类:
dis;
实验作文import t.annotation.Bean;
import t.annotation.Configuration;
import org.tion.RedisConnectionFactory;
import org.dis.listener.RedisMessageListenerContainer;
/**
* @program: springdemo
* @description
* @author: Young
* @create: 2020-07-10 16:47
**/
@Configuration
public class RedisListenerConfig {
@Bean隐患险于明火
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.tConnectionFactory(connectionFactory);
return container;
}
}
具体监听实现服务类:
dis;
slf4j.Slf4j;
import org.tion.Message;
import org.dis.listener.KeyExpirationEventMessageListener; import org.sprin
dis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
* @program: springdemo
* @description
* @author: Young
* @create: 2020-07-10 16:49
**/
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
测试真心朋友
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
// ⽤户做⾃⼰的业务处理即可,注意String()可以获取失效的key
String expiredKey = String();
log.info("key[{}] 已过期", expiredKey);
//todo 业务
}
}
集群版:
下⾯是代码实现为:
配置⽂件
(⼀些其他性能的配置项就没有贴,使⽤默认值..nodes也可以⽤- - - - 来进⾏区分..)
spring:
redis:
password: 123456
cluster:
nodes: 192.168.241.239:7000,192.168.241.239:7001,192.168.241.239:7002,192.168.241.239:7003,192.168.241.239:7004,192.168.241.239:7005 configuration配置类:
dis;
积极分子谈话记录
/**
* @program: springdemo
* @description
* @author: Young
* @create: 2020-07-11 15:32
**/
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.dition.ConditionalOnClass;
import org.springframework.dition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.tion.MessageListener;
import org.tion.RedisConnectionFactory;
import org.tion.jedis.JedisConnection;前出塞杜甫
import org.RedisOperations;
import org.dis.listener.PatternTopic;
import org.dis.listener.RedisMessageListenerContainer;
import redis.clients.jedis.Jedis;
@Configuration
@ConditionalOnClass({ JedisConnection.class, RedisOperations.class, Jedis.class, MessageListener.class })adb驱动安装
@AutoConfigureAfter({ JacksonAutoConfiguration.class,RedisAutoConfiguration.class })
public class RedisAutoConfiguration {
@Configuration
@ConditionalOnExpression("!'${dis.host:}'.isEmpty()")
public static class RedisStandAloneAutoConfiguration {
@Bean
public RedisMessageListenerContainer customizeRedisListenerContainer(
RedisConnectionFactory redisConnectionFactory,MessageListener messageListener) {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.tConnectionFactory(redisConnectionFactory);
redisMessageListenerContainer.addMessageListener(messageListener,new PatternTopic("__keyevent@0__:expired")); return redisMessageListenerContainer;
}
}
@Configuration
@ConditionalOnExpression("'${dis.host:}'.isEmpty()")
public static class RedisClusterAutoConfiguration {
@Bean
public RedisMessageListenerFactory redisMessageListenerFactory(BeanFactory beanFactory,
RedisConnectionFactory redisConnectionFactory) {
RedisMessageListenerFactory beans = new RedisMessageListenerFactory();
beans.tBeanFactory(beanFactory);
beans.tRedisConnectionFactory(redisConnectionFactory);
return beans;笑傲人生
}
}
}
监听配置类:
dis;
/**
* @program: springdemo
* @program: springdemo
* @description
* @author: Young
* @create: 2020-07-11 15:41
**/
slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import t.ApplicationListener;
import t.event.ContextRefreshedEvent;
import org.tion.MessageListener;
import org.tion.RedisClusterConnection;
import org.tion.RedisClusterNode;
import org.tion.RedisConnectionFactory;
import org.tion.jedis.JedisConnectionFactory;
import org.dis.listener.PatternTopic;
import org.dis.listener.RedisMessageListenerContainer;
import redis.clients.jedis.JedisShardInfo;
@Slf4j
public class RedisMessageListenerFactory implements BeanFactoryAware, ApplicationListener<ContextRefreshedEvent> {
@Value("${dis.password}")
private String password;
private DefaultListableBeanFactory beanFactory;
private RedisConnectionFactory redisConnectionFactory;
伏波将军
@Autowired
private MessageListener messageListener;
public void tBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (DefaultListableBeanFactory) beanFactory;
}
public void tRedisConnectionFactory(RedisConnectionFactory redisConnectionFactory) {
}
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
RedisClusterConnection redisClusterConnection = ClusterConnection();
if (redisClusterConnection != null) {
Iterable<RedisClusterNode> nodes = redisClusterConnection.clusterGetNodes();
for (RedisClusterNode node : nodes) {
if (node.isMaster()) {
log.info("获取到redis的master节点为[{}]",String());
String containerBeanName = "messageContainer" + node.hashCode();
if (ainsBean(containerBeanName)) {
return;
}
JedisShardInfo jedisShardInfo = new Host(), Port());
jedisShardInfo.tPassword(password);
JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo);
BeanDefinitionBuilder containerBeanDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(RedisMessageListenerContainer.class);
containerBeanDefinitionBuilder.addPropertyValue("connectionFactory", factory);
containerBeanDefinitionBuilder.tScope(BeanDefinition.SCOPE_SINGLETON);