redisson连接池配置_redis连接池原理

redisson连接池配置_redis连接池原理文章目录PreCode初始化槽计算无需手工调用close方法PreRedis进阶-Redis集群原理剖析及gossip协议初探集群原理部分简单的提了下Jest是如何实现RedisCluster的,这里我们再来梳理一下Codeimportredis.clients.jedis.HostAndPort;importredis.clients.jedis.JedisCl…


Jetbrains全系列IDE稳定放心使用

在这里插入图片描述


Pre

Redis进阶-Redis集群原理剖析及gossip协议初探 集群原理部分 简单的提了下Jest是如何实现Redis Cluster 的 ,这里我们再来梳理一下


Code


import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


public class JedisClusterDemo { 
   
    public static void main(String[] args) throws IOException { 
   

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(10);
        config.setMinIdle(5);

        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.18.131", 8001));
        jedisClusterNode.add(new HostAndPort("192.168.18.131", 8004));
        jedisClusterNode.add(new HostAndPort("192.168.18.132", 8002));
        jedisClusterNode.add(new HostAndPort("192.168.18.132", 8005));
        jedisClusterNode.add(new HostAndPort("192.168.18.133", 8003));
        jedisClusterNode.add(new HostAndPort("192.168.18.133", 8006));

        JedisCluster jedisCluster = null;
        try { 
   
            //connectionTimeout:指的是连接一个url的连接等待时间
            //soTimeout:指的是连接上一个url,获取response的返回等待时间
            jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "artisan", config);
            System.out.println(jedisCluster.set("clusterArtisan", "artisanValue"));
            System.out.println(jedisCluster.get("clusterArtisan"));
        } catch (Exception e) { 
   
            e.printStackTrace();
        } finally { 
   
            if (jedisCluster != null)
                jedisCluster.close();
        }
    }
}

这里是个简单的demo, 生产中用的话,需要确保jedisCluster是单例的,并且无需手工调用close,不然的话 这个连接池就关闭了,你就无法获取到连接了。


初始化

当 Redis Cluster 的客户端来连接集群时,它也会得到一份集群的槽位配置信息并将其缓存在客户端本地。这样当客户端要查找某个 key 时,可以直接定位到目标节点。

我们来看下jedis的实现

 jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "artisan", config);

跟下源码

public JedisClusterConnectionHandler(Set<HostAndPort> nodes,
                                       final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password) { 
   
    this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, password);
    initializeSlotsCache(nodes, poolConfig, password);
  }

重点看下 initializeSlotsCache

  private void initializeSlotsCache(Set<HostAndPort> startNodes, GenericObjectPoolConfig poolConfig, String password) { 
   
    for (HostAndPort hostAndPort : startNodes) { 
   
      Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort());
      if (password != null) { 
   
        jedis.auth(password);
      }
      try { 
   
        cache.discoverClusterNodesAndSlots(jedis);
        break;
      } catch (JedisConnectionException e) { 
   
        // try next nodes
      } finally { 
   
        if (jedis != null) { 
   
          jedis.close();
        }
      }
    }
  }

继续 cache.discoverClusterNodesAndSlots(jedis); cache为 JedisClusterInfoCache 对象。

  public void discoverClusterNodesAndSlots(Jedis jedis) { 
   
    w.lock();

    try { 
   
      reset();
      List<Object> slots = jedis.clusterSlots();

      for (Object slotInfoObj : slots) { 
   
        List<Object> slotInfo = (List<Object>) slotInfoObj;

        if (slotInfo.size() <= MASTER_NODE_INDEX) { 
   
          continue;
        }

        List<Integer> slotNums = getAssignedSlotArray(slotInfo);

        // hostInfos
        int size = slotInfo.size();
        for (int i = MASTER_NODE_INDEX; i < size; i++) { 
   
          List<Object> hostInfos = (List<Object>) slotInfo.get(i);
          if (hostInfos.size() <= 0) { 
   
            continue;
          }

          HostAndPort targetNode = generateHostAndPort(hostInfos);
          setupNodeIfNotExist(targetNode);
          if (i == MASTER_NODE_INDEX) { 
   
            assignSlotsToNode(slotNums, targetNode);
          }
        }
      }
    } finally { 
   
      w.unlock();
    }
  }

槽计算

set --------> run  ----> runWithRetries ----> connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key))

CRC16算法,计算key对应的slot connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key))

  public static int getSlot(byte[] key) { 
   
    int s = -1;
    int e = -1;
    boolean sFound = false;
    for (int i = 0; i < key.length; i++) { 
   
      if (key[i] == '{' && !sFound) { 
   
        s = i;
        sFound = true;
      }
      if (key[i] == '}' && sFound) { 
   
        e = i;
        break;
      }
    }
    if (s > -1 && e > -1 && e != s + 1) { 
   
      return getCRC16(key, s + 1, e) & (16384 - 1);
    }
    return getCRC16(key) & (16384 - 1);
  }

无需手工调用close方法

进入到set方法中看下源码

jedisCluster.set("clusterArtisan", "artisanValue")

如下:

  @Override
  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方法

  public T run(String key) { 
   
    if (key == null) { 
   
      throw new JedisClusterException("No way to dispatch this command to Redis Cluster.");
    }

    return runWithRetries(SafeEncoder.encode(key), this.maxAttempts, false, false);
  }

继续 runWithRetries , 截取核心逻辑

private T runWithRetries(byte[] key, int attempts, boolean tryRandomNode, boolean asking) { 
   

    Jedis connection = null;
    try { 
   

      if (asking) { 
   
       ......
      } else { 
   
        if (tryRandomNode) { 
   
          connection = connectionHandler.getConnection();
        } else { 
   
          connection = connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
        }
      }

      return execute(connection);
    } finally { 
   
      releaseConnection(connection);
    }
  }

关注点

  • CRC16算法,计算key对应的slot connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key))
  public static int getSlot(byte[] key) { 
   
    int s = -1;
    int e = -1;
    boolean sFound = false;
    for (int i = 0; i < key.length; i++) { 
   
      if (key[i] == '{' && !sFound) { 
   
        s = i;
        sFound = true;
      }
      if (key[i] == '}' && sFound) { 
   
        e = i;
        break;
      }
    }
    if (s > -1 && e > -1 && e != s + 1) { 
   
      return getCRC16(key, s + 1, e) & (16384 - 1);
    }
    return getCRC16(key) & (16384 - 1);
  }

  • getConnectionFromSlot 通过 JedisPool 获取连接

关注下 JedisCluster是如何获取连接的 getConnectionFromSlot 方法

  @Override
  public Jedis getConnectionFromSlot(int slot) { 
   
    JedisPool connectionPool = cache.getSlotPool(slot);
    if (connectionPool != null) { 
   
      // It can't guaranteed to get valid connection because of node
      // assignment
      return connectionPool.getResource();
    } else { 
   
      renewSlotCache(); //It's abnormal situation for cluster mode, that we have just nothing for slot, try to rediscover state
      connectionPool = cache.getSlotPool(slot);
      if (connectionPool != null) { 
   
        return connectionPool.getResource();
      } else { 
   
        //no choice, fallback to new connection to random node
        return getConnection();
      }
    }
  }

本质上还是通过 JedisPool 来获取一个getResource ,跟我们使用Sentinel 啊 单节点获取方法是一样的


  • finally 语句中的 releaseConnection(connection); ,自动释放连接

看下该方法

  private void releaseConnection(Jedis connection) { 
   
    if (connection != null) { 
   
      connection.close();
    }
  }

说白了,JedisCluster set后会自动释放连接,调用的是jedis 的close方法,所以我们无需手工关闭,否则你这个jedis的连接池就挂逼了…

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182232.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • scrapy安装步骤_linux下安装scrapy

    scrapy安装步骤_linux下安装scrapy1、Scrapy是什么Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。2、scrapy安装安装过程中出错:如果安装出现一下错误building‘twisted.test.raiser’extensionerror:MicrosoftVisualC++14.0isrequired.Getitwith“MicrosoftVisualC++BuildTools”:http://l

    2022年9月2日
  • RSA 加密算法原理简述

    RSA 加密算法原理简述概述本文旨在说明RSA加密算法的原理及实现,而其相关的数学部分的证明则不是本文内容。版权说明著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。作者:Q-WHai发表日期:2016年2月29日本文链接:http://blog.csdn.net/lemon_tree12138/article/details/50696926来源:CSDN…

    2022年6月12日
  • c语言程序设计谭浩强第五版第六章答案_谭浩强c语言答案第五版pdf

    c语言程序设计谭浩强第五版第六章答案_谭浩强c语言答案第五版pdfC语言程序设计第五版谭浩强著第七章答案1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。如有问题欢迎下方留言评论#include<stdio.h>voidmain(void){intmaxgy(intx,inty);intmingb(intx,i…

    2022年10月23日
  • Android adb install 命令安装apk

    Android adb install 命令安装apk如果有人发送一个apk安装包给你安装,普通人都是先把apk文件拷到手机存储内,然后在手机存储找到该安装包,点击安装。但是这样太麻烦了,而且对我们程序猿来说不够逼格,其实我们可以通过adb命令,一行搞定adbinstallapk安装包路径C:\Users\Him>adbinstalld:/app-debug.apkd:/app-debug.apk:1filepu…

    2022年6月5日
  • struts中的action_type object has no attribute

    struts中的action_type object has no attribute在Strust2中,有一个内置对象叫ActionContext,通过该对象可以获得之前Servlet中的对象,比如:requst对象,response对象…那么为什么可以通过ActionContext获得那些对象呢?那是因为在ActionContext内容引用了那些对象,也就是在ActionContext内部记录了那些对象的地址,看下图上图就是简单理解为什么通过Action

    2022年9月10日
  • html转word java_word转excel排版不变

    html转word java_word转excel排版不变页面:无标题页页面后置文件:usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.

    2022年8月31日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号