zoukankan      html  css  js  c++  java
  • redis分布式锁

    redis有一版客户端对分布式锁有很好的支持,所以我使用的是该客户端

    首先添加客户端依赖

    <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>3.10.2</version>
    </dependency>

    然后添加redis客户端的信息配置

    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.password=123456
    spring.redis.timeout=3600
    spring.redis.database=1
    spring.redis.jedis.pool.max-active=8
    spring.redis.jedis.pool.max-wait=1
    spring.redis.jedis.pool.max-idle=500
    spring.redis.jedis.pool.min-idle=0

    最后添加工具类即可

    package com.voole.platform.util;
    
    import java.util.concurrent.TimeUnit;
    
    import org.redisson.api.RLock;
    import org.redisson.api.RedissonClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    /**
     * 
     * <P>Description:
     * 使用RFuture 异步获取锁 </P>
     * @ClassName: DistributedLockByRedis
     * @author 冯浩  2019年3月20日 上午9:53:41
     * @see TODO
     */
    @Component
    public class DistributedLockByRedis {
        
        @Autowired
        private RedissonClient client;
        
        public boolean lock(String key) {
            RLock lock = client.getLock(key);
            boolean trylock = false;
            try {
                trylock = lock.tryLock(60, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return trylock;
        }
        
        public void release(String key) {
            RLock lock = client.getLock(key);
            if(lock.isLocked()) {
                lock.unlock();
            }
            
        }
    }
  • 相关阅读:
    【NET CORE微服务一条龙应用】第一章 网关使用与配置
    111
    test
    再来一个测试
    测试博客
    flutter 中的json解析
    关于flutter -app开发过程中的问题及解决方式总结
    使用Mybatis-plus通过自定义Sql查询只有主键为null的问题
    Centos 6中keepalived作为服务启动
    CentOS6 开放、关闭防火墙相关端口命令
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/10563285.html
Copyright © 2011-2022 走看看