赞
踩
在现有项目中初次使用了 Redission 工具类,做分布式锁。但是在启动的时候遇到了一个问题
pom.xml
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.9.1</version>
</dependency>
报错如下:
nested exception is org.redisson.client.RedisConnectionException: Unable to connect to Redis server: localhost/127.0.0.1:6379
根据我多次遇到问题的经验判定,这是redis连接失败的问题,
原因分析为 没有配置 redis 连接属性,导致使用默认的值
查看了我的 yml 文件配置,有配置的:
redisconfig:
hostName: ${redis.host}
port: ${redis.port}
password: ${redis.password}
database: ${redis.dbindex}
timeout: ${redis.timeout}
因为没有配置时,使用的是自动转配,现在设置的这个属性 redisconfig.* 不符合 redis 的自动注入属性格则
第一种解决方法是,使用正确的自动装配规则定义属性值:
spring:
redis:
database: 0
host: ${redis_host}
port: ${redis_port}
password: ${redis_password}
第二种解决方法是,从配置文件中读取出来特定的属性,然后通过定义一个 @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);
}
地址前面必须要加上 “redis://” 或者 “rediss://”,才能使用…
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。