gpt4 book ai didi

redis - 如何使用jedis客户端在java中监听redis集群的键空间通知?

转载 作者:可可西里 更新时间:2023-11-01 11:14:33 25 4
gpt4 key购买 nike

我正在设置一个服务器,它可以监听和发送有关 redis 数据库中发生的任何事件的消息。我成功地收到了有关 redis 主机和端口的新事件的通知,但无法为 redis 集群这样做。

GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(30);
config.setMaxWaitMillis(2000);

Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7001));

JedisCluster cluster1 = new JedisCluster(jedisClusterNode, config);

String redisProperties = cluster1.getClusterNodes().toString().replaceAll("[{}]", "");
Set<HostAndPort> nodes = new HashSet<>();
String[] mainArray = redisProperties.split(",");
for (int i = 0; i < mainArray.length; i++) {
String[] equalArray = mainArray[i].split("=");
String mainData = equalArray[0];
String[] ipPortPair = mainData.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}

JedisCluster cluster = new JedisCluster(nodes, 10000, 1000, 1, config);

jedis.configSet("notify-keyspace-events", "AKE");//对于所有类型的事件jedis.psubscribe(new KeyListenerCluster(), "keyevent@0:*");

我可以使用 redis-cluster 执行所有其他操作,但不能做一件事。

cluster.configSet("notify-keyspace-events", "AKE");//对于所有类型的事件cluster.psubscribe(new KeyListenerCluster(), "keyevent@0:*");

最佳答案

我面临同样的问题,Keyspace 通知间歇性工作(10 次中有 6 或 7 次工作)。我读到需要订阅所有主节点才能获得通知。如果有帮助,我已将我的配置文件粘贴到下面:

KeySpaceNotificationMessageListener keySpaceNotificationMessageListener;

@Value("${spring.redis.cluster.nodes}")
private String hostsAndPorts;

@Bean
JedisConnectionFactory jedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration)
{
return new JedisConnectionFactory(redisClusterConfiguration);
}

@Bean
MessageListenerAdapter messageListener() {
return new MessageListenerAdapter(keySpaceNotificationMessageListener);
}


@Bean(name = "cacheManager1")
@Primary
public RedisCacheManager redisCacheManager1()
{
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofMinutes(1))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));

redisCacheConfiguration.usePrefix();

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(redisClusterConfiguration()))
.cacheDefaults(redisCacheConfiguration).build();

}

@Bean(name = "cacheManager2")
public RedisCacheManager redisCacheManager2( JedisConnectionFactory jedisConnectionFactory)
{
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofDays(1))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));

redisCacheConfiguration.usePrefix();

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(redisClusterConfiguration()))
.cacheDefaults(redisCacheConfiguration).build();

}

@Bean
public RedisClusterConfiguration redisClusterConfiguration()
{
String [] redisHostAndPorts = hostsAndPorts.split(",");

System.out.println(Arrays.toString(redisHostAndPorts));
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(redisHostAndPorts));
return redisClusterConfiguration;
}

@Bean
RedisMessageListenerContainer redisContainer() {
final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(jedisConnectionFactory(redisClusterConfiguration()));
container.addMessageListener(messageListener(), new PatternTopic("__keyspace@*:*"));
container.setTaskExecutor(Executors.newFixedThreadPool(4));
return container;
}

public void setKeySpaceNotificationMessageListener(KeySpaceNotificationMessageListener keySpaceNotificationMessageListener)
{
this.keySpaceNotificationMessageListener = keySpaceNotificationMessageListener;
}

关于redis - 如何使用jedis客户端在java中监听redis集群的键空间通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com