JedisConnectionException-读取超时错误

时间:2019-04-30 04:01:21

标签: java amazon-web-services jedis spring-data-redis redis-cluster

我正在尝试使用Spring Data Redis + Jedis组合连接到AWS ElastiCache Redis。 [启用了Redis群集,因此它具有群集配置端点,带有3个分片-每个分片具有1个主节点+ 2个副本节点]

我收到读取超时错误。

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out

AWS Redis Server版本:5.0.3 / 集群模式:启用/ SSL:已启用/ 身份验证:已启用(通过密码)

图书馆- Spring-data-redis:2.1.6。发布/ jedis:2.9.0

Telnet在6379端口上适用于AWS Redis所有节点和集群配置终端节点。

我自己尝试过Redisson,它可以连接到AWS Redis,没有任何问题。

因此,Redis本身没有问题,Spring Data Redis与Jedis一起存在问题。

我的代码如下所示-

        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        redisClusterConfiguration.setClusterNodes(listOfRedisNode);
        redisClusterConfiguration.setPassword(passwordString);


        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));
        jedisClientConfiguration.useSsl();
        jedisClientConfiguration.usePooling();


        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisClientConfiguration.build() );
        jedisConnectionFactory.afterPropertiesSet();

        final RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.afterPropertiesSet();

        System.out.println(redisTemplate.getClientList().size());

        StringRedisConnection stringRedisConnectionlettuce = new DefaultStringRedisConnection(redisTemplate.getConnectionFactory().getConnection());
        final String message2 = stringRedisConnectionlettuce.echo("Hello");
        System.out.println("Hello".equals(message2));

读取超时错误-

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
    at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202)
    at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40)
    at redis.clients.jedis.Protocol.process(Protocol.java:151)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:96)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
    at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:564)
    at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2138)
    at redis.clients.jedis.JedisClusterConnectionHandler.initializeSlotsCache(JedisClusterConnectionHandler.java:36)
    at redis.clients.jedis.JedisClusterConnectionHandler.<init>(JedisClusterConnectionHandler.java:17)
    at redis.clients.jedis.JedisSlotBasedConnectionHandler.<init>(JedisSlotBasedConnectionHandler.java:24)
    at redis.clients.jedis.BinaryJedisCluster.<init>(BinaryJedisCluster.java:54)
    at redis.clients.jedis.JedisCluster.<init>(JedisCluster.java:93)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createCluster(JedisConnectionFactory.java:418)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createCluster(JedisConnectionFactory.java:388)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.afterPropertiesSet(JedisConnectionFactory.java:345)
    at io.github.deepshiv126.springdataredis.example.MySpringBootApplication.main(MySpringBootApplication.java:306)
    ... 8 more
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.net.SocketInputStream.read(SocketInputStream.java:127)
    at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196)
    ... 27 more

我研究了Spring源代码和Jedis源代码-我的假设是不使用SSL连接;

JedisConnectionFactory-afterPropertiesSet()-试图创建集群–在它试图初始化InitialSlotsCache时,它使用密码向Redis Server发出了AUTH命令–这就是发生“读取超时”的地方;

我了解本地redis-您可以进入并运行auth命令以进行身份​​验证。但是我想AWS Redis可能无法做到这一点,它甚至需要在运行AUTH命令之前就拥有SSL连接-为什么Jedis不使用SSL Connection?

这是另一个线程"Cannot get Jedis connection" when using SSL with Redis and Spring Data Redis 说,使用类似JedisPool的东西-但spring-data-redis的JedisConnectionFactory不接受JedisPool。还有其他方法吗?

JedisPool jedisPool = new JedisPool("rediss://" + clusterConfigEndPoint + ":6379");

另一个问题-其他库将redis ssl连接用作rediss://-如何让Jedis Client使用SSL连接,

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经面对这个问题有一段时间了,但是通过以下配置,它可以完美地与 AWS ElasticCache - Redis 配合使用 SSL(启用传输中加密和静态加密)。

以下 Maven 依赖项用作 springboot 应用程序的一部分

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- Latest jedis with SSL support -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.6.0</version>
        </dependency>

@Component
public class RedisConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("PrimaryEndpoint of AWS Elastic Cache Cluster");
        redisStandaloneConfiguration.setPort(6379);
        redisStandaloneConfiguration.setUsername("userName");
        
        redisStandaloneConfiguration.setPassword(new String("password").toCharArray());

        JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));// 60s connection timeout
        jedisClientConfiguration.useSsl();
        jedisClientConfiguration.usePooling();

        JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(redisStandaloneConfiguration,
                jedisClientConfiguration.build());
        jedisConFactory.afterPropertiesSet();

        return jedisConFactory;
    }

    @Bean(value = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}