探索jedis连接池预热优化高并发
更新时间:2023年10月25日 09:55:06 作者:codecraft
这篇文章主要为大家介绍了jedis连接池预热优化高并发深入探索示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
序
本文主要研究一下jedis连接池的预热
预热
@Component
@Slf4j
public class JedisWarmUp implements CommandLineRunner {
@Autowired
JedisConnectionFactory jedisConnectionFactory;
@Override
public void run(String... args) throws Exception {
GenericObjectPoolConfig poolConfig = jedisConnectionFactory.getPoolConfig();
if (poolConfig == null) {
return ;
}
try {
DirectFieldAccessor accessor = new DirectFieldAccessor(jedisConnectionFactory);
JedisPool jedisPool = (JedisPool) accessor.getPropertyValue("pool");
if (jedisPool == null) {
log.info("jedisConnectionFactory.pool is null");
return;
}
int warmUpSize = poolConfig.getMaxIdle();
warmUp(jedisPool, warmUpSize);
} catch (Exception e) {
log.error("warmup jedisPool error:{}", e.getMessage());
}
}
private void warmUp(JedisPool jedisPool, int warmUpSize) {
List<Jedis> warmUpList = new ArrayList<>(warmUpSize);
for (int i=0; i< warmUpSize; i++) {
Jedis jedis = jedisPool.getResource();
jedis.ping();
warmUpList.add(jedis);
}
warmUpList.stream().forEach(jedis -> jedis.close());
}
}这里取了maxIdle配置来进行预热,通过DirectFieldAccessor来获取jedisConnectionFactory对象的pool
小结
在高并发场景,通常需要对资源进行预热,比如数据库连接池、redis连接池、线程池等等,可以避免首次请求慢的问题。这里通过先从连接池借一批连接,然后归还一批连接来达到预热的目的。
以上就是探索jedis连接池预热优化高并发的详细内容,更多关于jedis连接池预热的资料请关注脚本之家其它相关文章!
相关文章
Java的线程池ThreadPoolExecutor及多种线程池实现详解
这篇文章主要介绍了Java的线程池ThreadPoolExecutor及多种线程池实现详解,ThreadPoolExecutor 使用 int 的高 3 位来表示线程池状态,低 29 位表示线程数量,之所以将信息存储在一个变量中,是为了保证原子性,需要的朋友可以参考下2024-01-01
Sa-Token不同模式实现单地登录 多地登录 同端互斥登录
这篇文章主要为大家介绍了Sa-Token不同模式实现单地登录 多地登录 同端互斥登录,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07
如何在mapper文件中使用in("str1","str2")
这篇文章主要介绍了如何在mapper文件中使用in("str1","str2"),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01


最新评论