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. 吴裕雄 python 神经网络——TensorFlow训练神经网络:不使用激活函数

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

  2. 静态成员、final成员、面向对象接口

    静态成员 静态属性定义时在访问控制关键字后面加static 在类定义中使用静态成员的时候,用self关键字后面跟着::操作符,在访问静态成员的时候::后面需要跟$符号 在类定义外部访问静态属性,用类名 ...

  3. idea使用小技巧

    1.按住alt,鼠标往下拉一条直线,可以选中一列或多列,或者不选中任何文字,可以让光标定位到这几行的相同的列的位置,然后输入文本,发现在被选中的所有行同时输入了这些文本(类似notepad++): 2 ...

  4. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

  5. 关于 UIDatePicker 在iOS9 系统上的一个坑

    在使用 UIDatePicker时,在iOS9系统上上遇到一个很奇怪的问题,在其他系统版本中没发现,设置年月日格式显示的视图,在iOS9设备上出现中间月份无法显示的问题: 检查代码没问题,这个视图是使 ...

  6. Java 基础--移位运算符

    移位运算符就是在二进制的基础上对数字进行平移.按照平移的方向和填充数字的规则分为三种: <<(左移).>>(带符号右移)和>>>(无符号右移). 1.左移 按 ...

  7. wc、grep 、 cut、paste 和 tr 命令的用法

    1 wc 命令 wc 命令是一个统计的工具,主要用来显示文件所包含的行.字和字节数. wc 命令是 word count 的缩写. (1)命令格式 wc [选项] [文件] (2)常用参数 参数 描述 ...

  8. Windows 安装python虚拟环境

    windows 安装pytho虚拟环境 方法一:virtualenv (1)使用pip安装virtualenv工具 pip install virtualenv (2)使用virtualenv创建虚拟 ...

  9. selenium webdriver 相关网站

    ITeye:http://shijincheng0223.iteye.com/blog/1481446 http://ztreeapi.iteye.com/blog/1750554 http://sm ...

  10. Android开发之显示分辨率及单位

    Android 各种屏幕分辨率: VGA:         Video Graphics Array,即:显示绘图矩阵,相当于640×480 像素: HVGA:       Half-size VGA ...