就在这一天,做项目时正好用jedis,整合springboot,没使redis data stater,,,在对使用包装过后要进行整合到容器中时,第一次测试注入jedispool成功了。。。就因此种下了bug的隐患,在服务和消费运行之后,奇迹发生了。。。。。。。Could not get a resource from the pool \\\, Failed connecting to host \\\,SocketTimeout我丢,从网上找了好多解答,去阿里社区看文档设计的答案也不符合。。。
什么连接池配置类,
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setBlockWhenExhausted(true);
jedisPoolConfig.setMaxWaitMillis(1000*10);
jedisPoolConfig.setTestOnBorrow(true);
redis服务配置文件bind ,protected 之类的。。。
直接注释掉 bind就可以,网上说后两个要改no
在安全组开启 port对应的端口
# bind xxxxx //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群,直接注释掉了
daemonize yes //redis后台运行
producted-mode yes
更换了pom依赖版本也不顶用,之前使用的
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
虽然去了github 看了对版本的讨论,对一些2.1 -> 2.7x之类的说法。但我这个也不属于 跨度啊
更新成了,,,, 毫无卵用
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
</dependency>
经过一些折腾之后。。最后在看源码的过程中找到了答案。。。。。。天啊真不容易 ,,源码是个好东西,
在对 jedisPool 进行初始化的时候传递的参数是对应的。但你参数的类型 匹配上了,,即使你写错了,也不会报错。。。
我是使用的jedis整合到用@configuration来从池中获得连接对象,
@Configuration
public class RedisConfig {
private static final String VALUE = "disabled";
@Value("${spring.redis.host:disabled}")
private String host;
@Value("${spring.redis.port:0}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database:0}")
private int database;
@Bean
public RedisUtils getRedisUtil() {
if (VALUE.equals(host)) {
return null;
}
RedisUtils redisUtils = new RedisUtils();
redisUtils.initPool(host, port, database, password);
return redisUtils;
}
}
public class RedisUtils {
private JedisPool jedisPool;
public void initPool(String host, int port, int database, String password) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setBlockWhenExhausted(true);
jedisPoolConfig.setMaxWaitMillis(1000*10);
jedisPoolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(jedisPoolConfig, host, port, 1000*20, password, database);
}
public Jedis getJedis() {
Jedis jedis = jedisPool.getResource();
return jedis;
}
}
我这个错误,,我发现在网上搜索之后,是比较罕见的。。。。所以我发了和大家分享,,不用在犯这种低级的错误而到来更多的麻烦和烦恼。。