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连接关闭问题的更多相关文章

  1. redis集群+JedisCluster+lua脚本实现分布式锁(转)

    https://blog.csdn.net/qq_20597727/article/details/85235602 在这片文章中,使用Jedis clien进行lua脚本的相关操作,同时也使用一部分 ...

  2. 访问redis集群提示连接超时的问题

    上周在服务器通过docker部署了一个单机版redis集群,今天通过StackExchange.Redis访问的时候报了这个错: 提示我把超时时间设置一下,我去服务器上找到redis的配置文件,发现不 ...

  3. redis集群启动和关闭脚本

    创建startall.sh /usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7001/redis.conf /usr/ ...

  4. redis集群的搭建详细教程

    1 Redis-cluster架构图             redis-cluster投票:容错  (至少要三个才可以,才能超过半数) 架构细节: (1)所有的redis节点彼此互联(PING-PO ...

  5. Redis集群模式配置

    redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...

  6. Redis单节点数据同步到Redis集群

    一:Redis集群环境准备 1:需要先安装好Redis集群环境并配置好集群 192.168.0.113 7001-7003 192.168.0.162 7004-7006 2:检查redis集群 [r ...

  7. redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...

  8. spring boot下JedisCluster方式连接Redis集群的配置

    最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...

  9. 通过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 但是使 ...

随机推荐

  1. 有未经处理的异常: 0xC00000FD: Stack overflow

    将“项目属性.链接器.系统.堆栈保留大小”设大一点比如16000000 这是我在网上找到的答案,很好的解决了我的问题,忘了是哪位大神了,总之,向大神致敬

  2. WLC RTU license

    目前思科的某些WLC不是一定要license文件去安装,例如这里提到的RTU license. RTU:Right To Use Right to Use (RTU) licensing is a m ...

  3. 一、FreeMarker实现对js和css压缩

    1.代码压缩理解:实际上就是将原有的文本中无用的注释.空行.空格去掉来压缩文件的大小.进行js和css压缩会带来如下好处:1)减小了文件的体积,减少文件占用的内存;2)减小了网络传输量和带宽占用; 3 ...

  4. uniGUI之UniSyntaxEdit(24)

    UniSyntaxEdit1语法高亮显示控件,主要属性Language,它是  多行 1]Language 语言 2]执行 FDquery1.Open(UniSyntaxEdit1.Lines.Tex ...

  5. Coursera 国内无法登陆问题

    参考: 1.Coursera登不上去 | 知乎 2.Win10如何修改hosts | 百度经验 浏览器在访问https://www.coursera.org时,出现正在等待 d3njjcbhbojbo ...

  6. 使用git commit命令时会提示"Please tell me who you are"

    在命令行中输入 git config --global user.email "邮箱地址" git config --global user.name "用户名" ...

  7. selenium webdriver 执行Javascript

    @Test public void testElementByID() { //通过JS获取页面元素 driver.get(url); driver.manage().window().maximiz ...

  8. MessageBox函数

    <Windows程序设计>(第五版)(美Charles Petzold著) https://docs.microsoft.com/zh-cn/windows/desktop/apiinde ...

  9. windows下svn post-commit的实现

    前言: 好的!在结束了上一博客教程的Subversion安装之后.我们开始了下一项工作,windows版本下 svn post-commit的实现.说实话,这方面的知识网上的知识并不是很多~~~~~~ ...

  10. 阿里云服务器ubantu创建新用户登录显示问题

    在root用户下输入:vi /etc/passwd,找到添加的用户,在后面加上/bin/bash 重新登录即回复正常