在Redis和Spring Data Redis中使用SSL时出现“无法获取Jedis连接”

bxpogfeg  于 2022-10-31  发布在  Redis
关注(0)|答案(4)|浏览(302)

{"header":{"type":"auto_translation","ret_code":"error","time_cost":1268.0,"request_id":"7135dc86592a11edb16dad0d39d360b4"},"message":"Translation error (20001), please retry later. Detail: RuntimeException - The length of source sentence is too long!!! - {\n "header": {\n "time_cost": 0.000441,\n "type": "auto_translation",\n "ret_code": "The length of source sentence is too long!!!"\n }\n}"}

hujrc8aj

hujrc8aj1#

不要使用JedisShardInfo,因为它会导致配置的歧义。接受JedisShardInfo的构造函数在Spring Data Redis 2.0中被弃用,请使用基于属性的配置:

JedisConnectionFactory conn =  new JedisConnectionFactory();
conn.setHostName(redisHost);
conn.setPort(redisPort);
conn.setUseSsl(true);
anauzrmj

anauzrmj2#

我遇到了一个类似的问题,但是,它是与JedisPool,而不是与SpringJedisShardInfo。我相信,虽然,这是本质上相同的问题。当连接到一个AWS Elasticache redis集群与SSL启用,我会得到一个

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset

解决我的问题的方法是使用jedis 2.9.0并启动我的JedisPool,如下所示:

String host = "someHost"; // The primary endpoint of the cluster
JedisPool jedisPool = new JedisPool("rediss://" + host + ":6379");

这里使用String构造函数很重要,因为URI构造函数不支持SSL。

1sbrub3j

1sbrub3j3#

你只提到了AWS Elastic Redis,但你没有说明它是基于集群的Redis还是单节点Redis?
Spring Data Redis 2.2将支持带有SSL的集群。https://jira.spring.io/browse/DATAREDIS-974

bweufnob

bweufnob4#

对于redis client 3.2.0及以上版本,我们可以如下设置ssl属性:
使用JedisShardInfo:

JedisShardInfo shardInfo = new JedisShardInfo(redisServer, redisServerPort, redisTimeout, redisSsl);
shardInfo.setPassword(redisPassword);
Jedis jedis = new Jedis(shardInfo);

使用JedisPool:

jedisPool = new JedisPool(poolConfig, redisServer, redisServerPort, redisTimeout, redisPassword, redisSsl);
jedis = jedisPool.getResource();

其中,在poolConfig中,您可以指定各种属性,例如,setMaxIdlesetMaxWaitMillissetMaxTotal等。

相关问题