redis集群JedisCluster连接关闭问题
JedisCluster连接关闭问题
set方法为例
//伪代码
JedisCluster jedisCluster = new JedisCluster();
jedisCluster.set("testKey", "testValue");
进入到set方法
类JedisCluster中;
初始化一个JedisClusterCommand对象,调用run方法;
需要实现一个execute方法,通过Jedis调用set方法(这里又回到单节点调用set的方式了);
public String set(final String key, final String value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
@Override
public String execute(Jedis connection) {
return connection.set(key, value);
}
}.run(key);
}
进入到run方法
- 类JedisClusterCommand中;
public T run(String key) {
return runWithRetries(JedisClusterCRC16.getSlot(key), this.maxAttempts, false, null);
}
进入到runWithRetries方法
- 类JedisClusterCommand中;
- 只需要关注2个地方即可;
- return execute(connection),这里调用了之前实现的execute方法;
- releaseConnection(connection),在finally中释放了连接;
private T runWithRetries(final int slot, int attempts, boolean tryRandomNode, JedisRedirectionException redirect) {
if (attempts <= 0) {
throw new JedisClusterMaxAttemptsException("No more cluster attempts left.");
}
Jedis connection = null;
try {
//此处为空,走else
if (redirect != null) {
connection = this.connectionHandler.getConnectionFromNode(redirect.getTargetNode());
if (redirect instanceof JedisAskDataException) {
// TODO: Pipeline asking with the original command to make it faster....
connection.asking();
}
} else {
//此处是false,走else
if (tryRandomNode) {
connection = connectionHandler.getConnection();
} else {
//这里会从池中获取一个Jedis对象
connection = connectionHandler.getConnectionFromSlot(slot);
}
}
//这里调用最开始实现的execute方法
return execute(connection);
} catch (JedisNoReachableClusterNodeException jnrcne) {
throw jnrcne;
} catch (JedisConnectionException jce) {
// release current connection before recursion
releaseConnection(connection);
connection = null;
if (attempts <= 1) {
//We need this because if node is not reachable anymore - we need to finally initiate slots
//renewing, or we can stuck with cluster state without one node in opposite case.
//But now if maxAttempts = [1 or 2] we will do it too often.
//TODO make tracking of successful/unsuccessful operations for node - do renewing only
//if there were no successful responses from this node last few seconds
this.connectionHandler.renewSlotCache();
}
return runWithRetries(slot, attempts - 1, tryRandomNode, redirect);
} catch (JedisRedirectionException jre) {
// if MOVED redirection occurred,
if (jre instanceof JedisMovedDataException) {
// it rebuilds cluster's slot cache recommended by Redis cluster specification
this.connectionHandler.renewSlotCache(connection);
}
// release current connection before recursion
releaseConnection(connection);
connection = null;
return runWithRetries(slot, attempts - 1, false, jre);
} finally {
//此处释放了连接
releaseConnection(connection);
}
}
进入到releaseConnection方法
- 类JedisClusterCommand中;
- 实际上是通过Jedis.close()关闭的,和我们用单节点时,是一样的关闭方式;
private void releaseConnection(Jedis connection) {
if (connection != null) {
connection.close();
}
}
总结
- 使用JedisCluster时,不需要手动释放连接;
- 在调用的过程中,会自动释放连接;
- 实际上是JedisCluster中通过JedisPool获取Jedis来执行命令;
redis集群JedisCluster连接关闭问题的更多相关文章
- redis集群+JedisCluster+lua脚本实现分布式锁(转)
https://blog.csdn.net/qq_20597727/article/details/85235602 在这片文章中,使用Jedis clien进行lua脚本的相关操作,同时也使用一部分 ...
- 访问redis集群提示连接超时的问题
上周在服务器通过docker部署了一个单机版redis集群,今天通过StackExchange.Redis访问的时候报了这个错: 提示我把超时时间设置一下,我去服务器上找到redis的配置文件,发现不 ...
- redis集群启动和关闭脚本
创建startall.sh /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7001/redis.conf /usr/ ...
- redis集群的搭建详细教程
1 Redis-cluster架构图 redis-cluster投票:容错 (至少要三个才可以,才能超过半数) 架构细节: (1)所有的redis节点彼此互联(PING-PO ...
- Redis集群模式配置
redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...
- Redis单节点数据同步到Redis集群
一:Redis集群环境准备 1:需要先安装好Redis集群环境并配置好集群 192.168.0.113 7001-7003 192.168.0.162 7004-7006 2:检查redis集群 [r ...
- redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
- spring boot下JedisCluster方式连接Redis集群的配置
最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...
- 通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
随机推荐
- 为什么ISR4K、ASR1K等设备的QoS ACL没有显示计数?
思科的ISR4K和ASR1K设备都是IOS XE的架构,它们和传统的IOS架构是不一样的. 以ISR4K为例,和一般的IOS(例如ISR G2)有所区别,他的转发更依赖硬件完成,针对NAT或QoS应用 ...
- Jmeter_用户定义的变量
1.线程组->添加->配置原件->用户定义的变量 2.自定义变量引用: ${ }
- table 实现向下滑动首行冻结,向右滑动左列冻结
<html> <head> <meta charset="utf-8"> <title>测试</title> <M ...
- 题解【[Ynoi2012]NOIP2015洋溢着希望】
\[ \texttt{Preface} \] 第二道 Ynoi 的题,纪念一下. 这可能是我唯一可以自己做的 Ynoi 题了. \[ \texttt{Description} \] 维护一个长度为 \ ...
- Python 爬取 热词并进行分类数据分析-[拓扑数据]
日期:2020.01.29 博客期:137 星期三 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- 苹果应用商店AppStore审核规则指南
http://www.zesmob.com/blog/40161.html 新应用上架苹果AppStore或重大版本更新时,往往会被拒多次,造成审核不通过的原因,主要是因为对苹果应用商店AppStor ...
- 关于ubuntu挂载ntfs无法进行读写的解决方法
查看挂载信息 df -h 参看要挂载磁盘UUID sudo blkid 编辑/etc/fstab文件: sudo vim /etc/fstab 在最后一行添加如下一行信息: UUID=A248CF46 ...
- 八 Spring的IOC的XML和注解的区别及其整合开发
xml和注解的区别 xml和注解整合开发 注解:取消扫描配置开启注解配置 扫描:<context:component-scan base-package="" /> ...
- TensorFlow基础三(Scope)
用到变量名了,就涉及到了名字域的概念.通过不同的域来区别变量名,毕竟给所有变量都直接取不同名字还是有点辛苦的. 主要是name_scope和variable_scope,name_scope 作用于操 ...
- Atcoder Beginner Contest 140E(多重集,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;multiset<long long&g ...