Spring Boot监听Redis键空间事件无效?一次定位不到Keyspace事件的完整排查过程

在使用 Spring Boot + Redis 实现键空间事件监听的过程中,很多人会遇到监听器无法触发的问题。本文记录了我在使用 Spring Boot 2.5.4、Redis 3.2.100(端口 6380,无密码)环境下,监听以 quotedprice: 开头的 Key 的 set 操作,却一直监听不到的全过程排查与解决方案,代码在文章最后面。

配置好监听器并成功启动 Spring Boot 应用后,在 Redis 中执行:

redis-cli -p 6380
select 0
set quotedprice:test123 "hello"

预期:后台应输出类似:

📣 Received Redis key event: __keyspace@0__:quotedprice:test123 -> set

初步排查:

  • 监听代码无误

    • 使用 RedisMessageListenerContainer

    • 监听频道为__keyspace@0__:quotedprice:* (高亮的0代表你需要监听的redis库的位置)

    • onMessage 方法为 public,未拼写错误

    • RedisTemplate 配置正常,StringRedisSerializer + Jackson2JsonRedisSerializer

    • 应用成功启动,控制台打印:

      ✅ RedisMessageListenerContainer STARTED and listening on __keyspace@0__:quotedprice:*

  • Redis 设置了键

    • 使用 redis-cli -p 6380 确认设置成功

  • Redis 的 notify-keyspace-events 设置

    redis-cli -p 6380 config get notify-keyspace-events
    返回

  • 1) "notify-keyspace-events"
    2) "xKE"

    看似配置了 KE,但仍然没有触发监听。

🧠 关键原因:notify-keyspace-events 配置不完整

Redis 中的 notify-keyspace-events 控制的是哪些事件可以被发布出去。

  • "xKE" 表示:过期事件(x)、Keyspace事件(K)、Keyevent事件(E)

  • 然而:set 属于写操作事件,属于 g(generic)类事件

正确做法:设置为 AKE

执行以下命令:

redis-cli -p 6380 config set notify-keyspace-events AKE

解释:

  • A:表示所有事件(包含 g 写、s 字符串、x 过期、e 驱逐、KE 等)

  • K:开启 Keyspace 事件通知

  • E:开启 Keyevent 事件通知

这是保证监听器正常工作的关键配置!


测试验证

再次执行:

redis-cli -p 6380 select 0 set quotedprice:test999 "hi-hello"

控制台立即输出:

📣 Received Redis key event: __keyspace@0__:quotedprice:test999 -> set

监听成功!一切恢复正常!


 总结

遇到 Redis 监听器不触发的第一步,不是怀疑代码,而是检查 notify-keyspace-events 配置是否开启了相关事件类型

notify-keyspace-events AKE

如果使用 redis.conf 文件启动,可以加入:

notify-keyspace-events "AKE"

附:监听代码示例

@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
        RedisConnectionFactory redisConnectionFactory,
        @Qualifier("MessageListenerAdapter1") MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(redisConnectionFactory);
    container.addMessageListener(listenerAdapter, new PatternTopic("__keyspace@0__:quotedprice:*"));
    System.out.println("✅ RedisMessageListenerContainer STARTED and listening on __keyspace@0__:quotedprice:*");
    return container;
}

@Bean("MessageListenerAdapter1")
public MessageListenerAdapter listenerAdapter(RedisKeyEventListener listener) {
    return new MessageListenerAdapter(listener, "onMessage");
}

监听类

@Component
public class RedisKeyEventListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.printf("📣 Received Redis key event: %s -> %s%n", channel, key);
    }
}

附加命令小贴士

查看事件通知配置:
redis-cli -p 6380 config get notify-keyspace-events

修改为 AKE:
redis-cli -p 6380 config set notify-keyspace-events AKE

设置key:
redis-cli -p 6380
select 0
set quotedprice:test "hello"


如果你的redis 设置了密码:
redis-cli -p 6380 -a "youpassword" config get notify-keyspace-events

redis-cli -p 6380 -a "youpassword" config set notify-keyspace-events AKE

注意:此处的密码一定要加单引号或者双引号,&和# 在使用命令 -a 时会被 shell 拆分或转义导致命令执行失败!!!

如有帮助,欢迎收藏并转发这篇文章!希望大家在监听 Redis 键空间事件时,不再踩坑 🎯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值