当前位置:   article > 正文

Redission 使用填坑之初次使用_nested exception is org.redisson.client.redisconne

nested exception is org.redisson.client.redisconnectionexception: unable to

在现有项目中初次使用了 Redission 工具类,做分布式锁。但是在启动的时候遇到了一个问题
pom.xml

<dependency>
	<groupId>org.redisson</groupId>
	<artifactId>redisson-spring-boot-starter</artifactId>
	<version>3.9.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

报错如下:

nested exception is org.redisson.client.RedisConnectionException: Unable to connect to Redis server: localhost/127.0.0.1:6379
  • 1

根据我多次遇到问题的经验判定,这是redis连接失败的问题,
原因分析为 没有配置 redis 连接属性,导致使用默认的值
查看了我的 yml 文件配置,有配置的:

redisconfig:
  hostName: ${redis.host}
  port: ${redis.port}
  password: ${redis.password}
  database: ${redis.dbindex}
  timeout: ${redis.timeout}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

因为没有配置时,使用的是自动转配,现在设置的这个属性 redisconfig.* 不符合 redis 的自动注入属性格则

第一种解决方法是,使用正确的自动装配规则定义属性值:

spring: 
  redis:
    database: 0
    host: ${redis_host}
    port: ${redis_port}
    password: ${redis_password}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第二种解决方法是,从配置文件中读取出来特定的属性,然后通过定义一个 @Bean ,给它注入

 @Bean(destroyMethod = "shutdown")
    RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + hostName + ":" + port)
                .setPassword(password)
                .setDatabase(Integer.parseInt(database))
                .setConnectTimeout(Integer.parseInt(timeout))
                .setTimeout(Integer.parseInt(timeout));
        return Redisson.create(config);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

地址前面必须要加上 “redis://” 或者 “rediss://”,才能使用…

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/791185
推荐阅读
相关标签
  

闽ICP备14008679号