如何使用 Spring Data Redis 和 GCP 托管集群来监听键空间事件?

问题描述 投票:0回答:2

借助 Spring Data Redis

@Indexed
注释,我正在 Redis 中使用二级索引。我的条目有 TTL。 这具有在主条目到期后保留索引的副作用。这是预期的,Spring 可以侦听键空间到期事件,以便在主 TTL 完成后删除这些索引。

但是,使用 Spring 启用监听密钥空间过期事件时,我在启动时遇到以下错误:

ERR unknown command 'CONFIG'

这就是我配置监听器的方式:

@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP)

我该怎么做才能让这项工作成功?

spring google-cloud-platform redis spring-data-redis
2个回答
2
投票

此问题与 Redis 集群是托管的这一事实有关,因此远程客户端无法在其上调用 CONFIG。启用 Spring 键空间事件侦听器时,它会尝试通过将

notify-keyspace-events
配置键设置为“Ex”来配置 Redis 以发出键空间到期事件。

此问题的解决方法是:

  1. 在 GCP 上配置您的 MemoryStore,添加
    notify-keyspace-events
    键,并将“Ex”作为值。
  2. 使用
    @EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP, keyspaceNotificationsConfigParameter = "")
    进行客户端配置。 显式为空的字符串会阻止 Spring 尝试覆盖远程配置。

0
投票

同样重要的是要记住,监听器必须监听来自所有主节点的通知。

请参阅:https://github.com/spring-projects/spring-data-redis/issues/1782

添加lettuce属性来连接集群解决了问题:

  data:
    redis:
      password: your-password
      ssl:
        enabled: true
      cluster:
        nodes: node-address
      lettuce:
        cluster:
          refresh:
            dynamic-refresh-sources: false
            period: PT1S
        pool:
          max-active: 15
          max-wait: -1
          max-idle: -1
          min-idle: 5 
© www.soinside.com 2019 - 2024. All rights reserved.