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. 【转】ssh 远程执行命令

    原文:https://blog.csdn.net/liuxiao723846/article/details/82667482 SSH 是 Linux 下进行远程连接的基本工具,不光可以登录,也可以远 ...

  2. vue 和 jquery混合使用

    有时候只要想到要用的 vue.js 的时候就会惯性的想起用vue-cli手脚架搭建一个项目,但是有时候的业务场景并不适合用vue-cli手脚架,这个时候使用vue+jquery混合使用,把他们的优点结 ...

  3. [原]greenplum安装详细过程

    今天又帮其他项目装了一遍GP,加上之前的两次,这是第三次了,虽然每次都有记录,但这次安装还是发现漏写了一些步骤,在此详细记录一下,需要的童鞋可以借鉴. 1.准备 这里准备了4台服务器,1台做maste ...

  4. 解决 Anaconda 3.7更新出现CondaHTTPError与SSLError

    1.问题描述: An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, ...

  5. Keras的TimeDistributed层

    Keras的TimeDistributed层主要用途是在时间维度上进行全连接. 比如Faster RCNN,1张图生成了16个ROI,需要对每一个ROI进行分类和回归,ROI的维度是7×7×512,长 ...

  6. 学习笔记(15)- 保险行业的问答语料 insuranceqa_data

    数据概览 ''' pool data are translated Chinese data with Google API from original English data ''' POOL_T ...

  7. 调用天气预报webservice接口

    1.将 服务端的wsdl文档保存至 本地 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL 2.将里面的 <s:element re ...

  8. SELinux永久关闭

    目录 SELinux永久关闭 参考 SELinux三种模式 永久关闭方法 SELinux永久关闭

  9. JS中,跨域调用(本地)另一个项目的方法

    IP地址,因为是本地的项目,所以我一开始写的是127.0.0.1...,但不对.应该写本机的IP地址才对!

  10. 用Navicat连接阿里云ECS服务器上的MySQL数据库,连接不上,并且报10060错误

    设置远程访问(使用root密码): grant all privileges on . to 'root' @'%' identified by '123456'; flush privileges; ...