@Bean
public JedisPool getJedisPool(){
String host = environment.getProperty("lock.redis.host");
String port = environment.getProperty("lock.redis.port");
GenericObjectPoolConfig config = new JedisPoolConfig();
int idle = 25;
config.setMaxTotal(idle); //配置最大jedis实例数
config.setMaxIdle(idle);//配置资源池最大闲置数
config.setMinIdle(idle);
config.setTestWhileIdle(true);
config.setTimeBetweenEvictionRunsMillis(30000);
config.setNumTestsPerEvictionRun(idle); // 每次都验证25个链接是否有效
config.setMaxWaitMillis(200);//等待可用连接的最大时间
config.setTestOnBorrow(false);//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
config.setTestOnCreate(false);
config.setEvictionPolicy(new MyEvictionPolicy());
JedisPool jedisPool = new JedisPool(config, host, Integer.parseInt(port));
return jedisPool;
}
/**
* 永不驱逐,利用test进行驱逐
*/
public static class MyEvictionPolicy implements EvictionPolicy {
@Override
public boolean evict(EvictionConfig config, PooledObject underTest, int idleCount) {
if (((Jedis)underTest.getObject()).ping().equals("PONG")) {
return false;
}
return true;
}
}
Jedis一般配置
最新推荐文章于 2025-04-14 18:53:19 发布