java Jedis TLS连接到Redis集群

olmpazwi  于 2023-05-15  发布在  Java
关注(0)|答案(1)|浏览(359)

我们使用Redis企业版5分片:主机:6379 - 6383端点:redis-endpoint.example.com 6378使用Jedis 4.3.x,当我使用JedisPooled时,它工作正常

GenericObjectPoolConfig<Connection> genericPoolConfig = new GenericObjectPoolConfig<>();
genericPoolConfig.setMaxIdle(4);
genericPoolConfig.setMaxTotal(5);
genericPoolConfig.setMinIdle(2);

JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
      .sslSocketFactory(sslContext.getSocketFactory()) // key/trust stores here
      .ssl(true)
      .user("user_here")
      .password(redisPassword)
      .hostnameVerifier(hostnameVerifier)
      .build();
JedisPooled jedis = new JedisPooled(genericPoolConfig, new HostAndPort("redis-endpoint.example.com", 6378), clientConfig);
jedis.set("key_1", "value_1"); // this cmd works

但是当我尝试使用JedisCluster时,它抛出Exception

Set<HostAndPort> jedisClusterNodes = new HashSet<>();
jedisClusterNodes.add(new HostAndPort("redis-endpoint.example.com", 6378));
JedisCluster cluster = new JedisCluster(jedisClusterNodes, clientConfig, 3, genericPoolConfig); // <== ERROR here
cluster.set("key_2", "value_2");

我得到了错误:

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR command is not allowed
    at redis.clients.jedis.Protocol.processError(Protocol.java:96)
    at redis.clients.jedis.Protocol.process(Protocol.java:137)
    at redis.clients.jedis.Protocol.read(Protocol.java:192)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:316)
    at redis.clients.jedis.Connection.getUnflushedObjectMultiBulkReply(Connection.java:282)
    at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:287)
    at redis.clients.jedis.JedisClusterInfoCache.executeClusterSlots(JedisClusterInfoCache.java:320)
    at redis.clients.jedis.JedisClusterInfoCache.discoverClusterNodesAndSlots(JedisClusterInfoCache.java:60)
    at redis.clients.jedis.providers.ClusterConnectionProvider.initializeSlotsCache(ClusterConnectionProvider.java:42)
    at redis.clients.jedis.providers.ClusterConnectionProvider.<init>(ClusterConnectionProvider.java:33)
    at redis.clients.jedis.UnifiedJedis.<init>(UnifiedJedis.java:122)
    at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:173)
    at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:167)

问题:
1.我应该把所有5个分片都放在Set jedisClusterNodes中,还是只需要放置集群端点。
1.我在使用new JedisCluster(jedisClusterNodes, clientConfig, 3, genericPoolConfig);时做错了什么?

  1. JedisPooled是否适用于集群模式?
mzillmmw

mzillmmw1#

默认情况下,您应该将Redis Enterprise(RE)视为客户端的单个节点。因此,您可以使用JedisPooled与RE通信。
注1:群集特定命令实际上在默认模式下被禁用。这就是您在错误消息ERR command is not allowed中看到的内容。
注意2:Redis Enterprise有一个OSS Cluster模式,允许使用集群特定的命令。只有在这种RE模式下,您才应该使用JedisCluster而不是JedisPooled进行通信。

相关问题