redis未配置密码时springboot集成redisson启动报错Unable to connect to Redis server。

application.yml配置:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password:

方式一:
maven依赖:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.9.1</version>
</dependency>

重新实现实现RedissonClient:

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port);
        if (StringUtils.isNotEmpty(password)) {
            config.useSingleServer().setPassword(password);
        }
        return Redisson.create(config);
    }
}

方式二:
maven依赖:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.8.2</version>
</dependency>

重新实现实现Redisson :

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public Redisson getClient () {
        Config config = new Config();

        config.useSingleServer().setAddress("redis://" + host + ":" + port);
//                                .setPassword(password);
        if (StringUtils.isNotEmpty(password)) {
            config.useSingleServer().setPassword(password);
        }

        return (Redisson)Redisson.create(config);
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐