Unable to connect to Redis;nested exception is io.lettuce.core.RedisConnectionException using RedisTempalte

huangapple go评论71阅读模式
英文:

Unable to connect to Redis;nested exception is io.lettuce.core.RedisConnectionException using RedisTempalte

问题

我尝试从我的Spring响应式应用程序连接到Azure Redis缓存时,出现无法连接到Redis;嵌套异常是使用RedisTempalte的io.lettuce.core.RedisConnectionException错误。

我在属性文件中配置了以下内容:

spring.redis.host=主机名
spring.redis.port=6379
spring.redis.password=密码

还尝试了使用LettuceConnectionFactory的基于Java的配置。

英文:

I am getting Unable to connect to Redis;nested exception is io.lettuce.core.RedisConnectionException using RedisTempalte error when i try to connect azure redis cache from my spring reactive application.

I have configured below in properties file

spring.redis.host=hostName         
spring.redis.port=6379  
spring.redis.password=password

also tried java based configuration using LettuceConnectionFactory

答案1

得分: 1

以下是您要翻译的内容:

我在启用 AWS ElastiCache 中的密码身份验证时遇到了相同的问题,因为没有为 Redis 客户端使用 SSL。

下面的 RedisConfig.java 使用 SSL 进行连接,我的错误消失了。顺便说一下,我正在使用 Spring Reactive。

@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Setter
public class RedisConfig {

    private String host;
    private String password;

    @Bean
    @Primary
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisConfiguration defaultRedisConfig) {
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .useSsl().and()
                .commandTimeout(Duration.ofMillis(60000)).build();
        return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
    }

    @Bean
    public RedisConfiguration defaultRedisConfig() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPassword(RedisPassword.of(password));
        return config;
    }
}
英文:

I have encountered the same problem when enabling password authentication in AWS ElastiCache because of not using SSL for the Redis client.

Bellow RedisConfig.java uses SSL to connect and my error disappeared. I am using Spring Reactive by the way.

@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Setter
public class RedisConfig {

    private String host;
    private String password;

    @Bean
    @Primary
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisConfiguration defaultRedisConfig) {
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .useSsl().and()
                .commandTimeout(Duration.ofMillis(60000)).build();
        return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
    }

    @Bean
    public RedisConfiguration defaultRedisConfig() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPassword(RedisPassword.of(password));
        return config;
    }
}

答案2

得分: 0

确保在AWS Redis连接中使用jedisConFactory.setUseSsl(true);。默认情况下,Redis连接的SSL功能是关闭的。

@Bean
JedisConnectionFactory jedisConnectionFactory() {

    final JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(300);
    jedisPoolConfig.setMaxIdle(20);
    jedisPoolConfig.setMaxWaitMillis(2000);
    jedisPoolConfig.setBlockWhenExhausted(true);

    JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(jedisPoolConfig);

    jedisConFactory.setHostName(redisHost);
    jedisConFactory.setPort(redisPort);
    jedisConFactory.setPassword(redisPassword);
    jedisConFactory.setUsePool(true);
    jedisConFactory.setTimeout(redisTimeout);
    jedisConFactory.setUseSsl(redisSsl);

    return jedisConFactory;
}

@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    //template.setEnableTransactionSupport(true);
    template.setExposeConnection(true);
    template.afterPropertiesSet();
    return template;
}
英文:

Make Sure to use jedisConFactory.setUseSsl(true); for aws redis connection. By Default ssl is off for redis connection.

@Bean
JedisConnectionFactory jedisConnectionFactory() {

	final JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	jedisPoolConfig.setMaxTotal(300);
	jedisPoolConfig.setMaxIdle(20);
	jedisPoolConfig.setMaxWaitMillis(2000);
	jedisPoolConfig.setBlockWhenExhausted(true);

	JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(jedisPoolConfig);

	jedisConFactory.setHostName(redisHost);
	jedisConFactory.setPort(redisPort);
	jedisConFactory.setPassword(redisPassword);
	jedisConFactory.setUsePool(true);
	jedisConFactory.setTimeout(redisTimeout);
	jedisConFactory.setUseSsl(redisSsl);

	return jedisConFactory;
}

@Bean(name = &quot;redisTemplate&quot;)
public RedisTemplate&lt;String, Object&gt; redisTemplate() {
	RedisTemplate&lt;String, Object&gt; template = new RedisTemplate&lt;&gt;();
	template.setConnectionFactory(jedisConnectionFactory());
	//template.setEnableTransactionSupport(true);
	template.setExposeConnection(true);
	template.afterPropertiesSet();
	return template;
}

huangapple
  • 本文由 发表于 2020年8月25日 12:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63572259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定