说一说redis分布式锁的几种实现及优缺点 您所在的位置:网站首页 分布式锁的实现方式及优缺点是什么 说一说redis分布式锁的几种实现及优缺点

说一说redis分布式锁的几种实现及优缺点

2024-07-05 08:00| 来源: 网络整理| 查看: 265

基于Jedis setnx、expire实现分布式锁(存在问题,作为错误示范)

先引入相关依赖(jedis 2.3.0后支持redis集群模式,2.4.2后支持jedisCluster多线程处理,2.9.0之后版本较稳定,这里使用jedis 2.9.0版本,)

redis.clients jedis 2.9.0

Jedis单机版连接池:配置jedis连接池获得连接池对象,然后从连接池获得Jedis实例对象(我们一般用集群版的,单机版不过多介绍)

JedisPool jedisPool = null; Jedis jedis = null; //设置连接池的配置对象 JedisPoolConfig config = new JedisPoolConfig(); //设置连接池参数 config.setMaxTotal(30);//最大活动对象数 config.setMaxIdle(10);//最小能够保持idel(空闲)状态的对象数 //获取连接池对象 jedisPool = new JedisPool(config,"127.0.0.1", 6379); //获得jedis实例 jedis=jedisPool.getResource(); //归还连接 jedis.close(); //连接池关闭 jedisPool.close();

Jedis集群版连接池:配置连接池参数

配置文件(yml):

#最大活动对象数 maxTotal: 1000 #最大能够保持idel状态的对象数 maxIdle: 100 #最小能够保持idel状态的对象数 minIdle: 50 #当池内没有返回对象时,最大等待时间 maxWaitMillis: 10000 #当调用borrow Object方法时,是否进行有效性检查 testOnBorrow: true #当调用return Object方法时,是否进行有效性检查 testOnReturn: true #“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1. timeBetweenEvictionRunsMillis: 30000 #向调用者输出“链接”对象时,是否检测它的空闲超时; testWhileIdle: true # 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3. numTestsPerEvictionRun: 50 #redis服务器节点地址(ip:port) nodes: - "*.*.*.*:****"

把JedisCluster作为单例类HTJedisClusterClient的一个属性,经过过单例类HTJedisClusterClient初始化,完成JedisCluster的配置,然后只需要通过调用HTJedisClusterClient的getJedisCluster()方法获得JedisCluster实例对象

public class HTJedisClusterClient { protected final Logger cLogger = Logger.getLogger(getClass()); private static final String LOCK_SUCCESS = "OK"; private static final Long RELEASE_SUCCESS = 1L; private JedisCluster jedisCluster; private Set nodes = new HashSet(); private static volatile HTJedisClusterClient instance; private static String cPath="jedis-config.yml"; private HTJedisClusterClient() { //初始化 this.init(); } private void init() { try { //读取配置文件 Yaml yaml = new Yaml(); // String filePath= SysInfo.cHome+cPath; String filePath = "classpath:" + cPath; File file = ResourceUtils.getFile(filePath); InputStream in=new FileInputStream(file); Map map = yaml.load(in); //配置连接池 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle((int)map.get("maxIdle")); config.setMaxTotal((int)map.get("maxTotal")); config.setMinIdle((int)map.get("minIdle")); config.setTestOnBorrow((boolean)map.get("testOnBorrow")); config.setTimeBetweenEvictionRunsMillis((long)map.get("timeBetweenEvictionRunsMillis")); //节点信息配置 List hostanportList=(List) map.get("nodes"); for(String hostandport:hostanportList){ HostAndPort hostAndPort=HostAndPort.parseString(hostandport); this.nodes.add(hostAndPort); } //获得jedisCluster对象 this.jedisCluster = new JedisCluster(this.nodes, config); cLogger.info("jedis cluster init pub/sub pool finish nodes=" + this.nodes); }catch (Exception e){ e.printStackTrace(); cLogger.info("初始化Jedis连接池异常:"+e.getMessage()); } } public static HTJedisClusterClient getInstance() { if (instance == null) { synchronized(HTJedisClusterClient.class) { if (instance == null) { instance = new HTJedisClusterClient(); } } } return instance; } public JedisCluster getJe


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有