redis配置文件修改
打开redis.window.conf配置文件,找到以下配置段.
############################# EVENT NOTIFICATION ##############################
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
notify-keyspace-events Ex
修改段落最末尾的配置信息 notify-keyspace-events
默认配置是 notify-keyspace-events "" ,将其修改为 notify-keyspace-events Ex.
E和x的解释可在注释信息中找到.
# E Keyevent事件,以__keyevent @ <db> __前缀发布
# x 过期事件(每次密钥过期时生成的事件)
修改好后,启动redis时,使用redis.windows.conf配置文件启动即可.
导入依赖jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Java端针对redis监听的配置
@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
编写监听器,监听redis过期并进行处理
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
@Autowired
private RedisService redisService;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
/**
* Creates new {@link Message Listener} for {@code __keyevent@*__:expired} messages.
*
* @param listenerContainer must not be {@literal null}.
*/
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
// message.toString()可以获取失效的key
String expiredKey = message.toString();
System.out.println("expiredKey:"+expiredKey);
if(expiredKey.contains("hadToPay")){
String examNo = expiredKey.split("_")[1];
String examItemNo = expiredKey.split("_")[2];
String scheduledId = expiredKey.split("_")[3];
String serviceId = "cn-gdxiash-examcustom";
List<ServiceInstance> serviceInstances
= discoveryClient.getInstances(serviceId);
if(serviceInstances==null || serviceInstances.isEmpty()){
//如若未找到服务则当前键延时2分钟
redisService.set(expiredKey,"scheduledId_"+scheduledId,120);
return;
}
//使用服务的第一个实例测试
ServiceInstance serviceInstance = serviceInstances.get(0);
String restUrl = "http://"+ serviceInstance.getHost() +
":"+ serviceInstance.getPort()
+"/exam/pay/"+examNo+"/"+examItemNo+"/"+scheduledId;
//交给examcustom服务处理即可
System.out.println("The restUrl value is "+restUrl);
String result = restTemplate.getForObject(restUrl,String.class);
System.out.println("申请单【"+examNo+"】缴费查询结果:"+result);
}
}
}
实际监听效果展示