分布式锁

简介: 快速实战

目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题就存在问题,解决方案分布式锁。

下面介绍两种首先分布式锁的方案:

  1. 基于Spring Integration实现分布式锁
  2. 基于redisson实现分布式锁

优缺点:第一种引入简单,使用方便,但只支持重入锁。第二种较第一种麻烦一点点,但支持重入锁、公平锁、读锁、写锁等多种类型。

第一种方案:提供的全局锁目前为以下存储提供了实现
Gemfire
JDBC
Redis
Zookeeper
因为第二种方案基于redis存储,为了方便该方案讲解选择redis作为存储。同时项目都采用springboot方式。

  1. 首先创建springboot项目,添加基本依赖及redis、integration-redis依赖。如果选择其他存储方式,添加对应的integration依赖即可,无需更改业务,非常方便。
  <!--integration-->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-redis</artifactId>
        </dependency>
        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.9.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  1. 添加yml配置
spring:
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true
  server:
    port: 8080
  mvc:
    static-path-pattern: /**
    resources:
      static-locations: classpath:/static/
  redis:
    database: 1
    host: xx.xx.xx.xx
    port: 6379
    password: xxx
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
    redisson:
      address: "xxxx"
      password: xxx
  1. 创建config配置,RedisLockRegistry的第三个参数设置为上锁以后xxx秒自动解锁的时间,默认60s。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;

/**
 * 描述:spring-integration-redis锁配置
 *
 * @Auther: gc.x
 * @Date: 2019/10/28
 */
@Configuration
public class RedisLockConfiguration {

    @Bean
    public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory){
        return new RedisLockRegistry(redisConnectionFactory,"spring-integration-redis",60000L);
    }

}
  1. 选择基于注解的形式加锁,先自定义锁注解,然后创建对应的切面

@interface


/**
 * 描述: 基于spring-integration-redis实现分布式锁
 * 只支持重入锁ReentrantLock
 * @Auther: gc.x
 * @Ddate:2019/10/28
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LockDistributed {

    /**
     * 锁的名称
     * @return
     */
    String name() default "";

    /**
     * 尝试加锁 最多等待时间
     * @return
     */
    int time() default 30;
    /**
     * 自定义业务key
     * @return
     */
    String [] keys() default {};
    /**
     * 上锁以后xxx秒自动解锁
     *
     */
    /**
     *上锁以后xxx秒自动解锁
     *  long leaseTime();
     *  在RedisLockConfiguration里面配置,如果要使用该参数,需要在切面里面new RedisLockRegistry
     * @return
     */
}

Aspect
主要就是拦截自定义的注解,获取数据生成lockname,及竞争锁时间,

redisLockRegistry.obtain方法获取锁,
lock.tryLock方法竞争锁
由于篇幅省略了部分私有方法,不用纠结,结尾会提供完整源码
---getKeyName是获取自定义的作用于参数上的一个注解,可以将传入的参数作为           lockname的一部分,增强锁的力度。
---getName获取完整路径的方法名。
/**
 * 描述:添加了 LzxLockDistributed 注解 的Aop
 *
 * @Auther: gc
 * @Date: 2019/6/18 10:56
 */
@Component
@Aspect
@Slf4j
public class LockAspect {
    /**
     *  锁前缀名
     */
    public static final String LOCK_NAME_PREFIX = "lock";
    /**
     *  分隔符
     */
    public static final String LOCK_NAME_SEPARATOR = ".";

    /**
     * 表达式解析器
     */
    private ExpressionParser parser = new SpelExpressionParser();

    /**
     * 参数解析
     */
    private ParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();

    @Autowired
    private RedisLockRegistry redisLockRegistry;
  /*  @Autowired
    private RedisConnectionFactory redisConnectionFactory;*/

    @Around(value = "@annotation(lockDistributed)")
    public Object aroundApi(ProceedingJoinPoint point, LockDistributed lockDistributed) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();

        String businessKeyName = getKeyName(point,lockDistributed);
        String lockName = LOCK_NAME_PREFIX+LOCK_NAME_SEPARATOR + getName(lockDistributed.name(), signature) + businessKeyName;
        log.info("lockName===>"+lockName);

       // RedisLockRegistry redisLockRegistry=new RedisLockRegistry(redisConnectionFactory,"spring-integration-redis",lockDistributed.leaseTime());

        Lock lock = redisLockRegistry.obtain(lockName);
        boolean currentThreadLock = false;
        Object proceed = null;
        try{
            currentThreadLock = lock.tryLock(lockDistributed.time(), TimeUnit.SECONDS);
            log.info("获取锁====="+currentThreadLock);
            if (!currentThreadLock) {
                throw new TimeoutException("获取锁资源等待超时");
            }
            proceed = point.proceed();
        }catch (Exception e){
            throw e;
        }finally {
            if(currentThreadLock){
                lock.unlock();
            }

        }


        return proceed;

    }
}
  1. 测试锁

@RestController
public class DistributedLock {

    @LockDistributed
    @RequestMapping("/redisLockTest")
    public void redisLockTest() {
        System.out.println("获取锁,执行");
        try {
            Thread.sleep(20*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束-------------------->>>>>>>>>>>>>>");
    }
}

第二种方案:提供多种锁实现

  1. 同理导入依赖
  2. 配置yml
  3. 创建config

@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {

    @Autowired
    private RedissonProperties redssionProperties;

    /**
     * 单体的
     * @return
     */
    @Bean
    @ConditionalOnProperty(value = "redisson.address")
    public RedissonClient redissonSingle(){
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer().setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setDatabase(redssionProperties.getDatabase())
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
        if(StringUtils.isNotEmpty(redssionProperties.getPassword())){
            serverConfig.setPassword(redssionProperties.getPassword());
        }
        return Redisson.create(config);
    }

    /**
     * 集群的
     * @return
     */
    @Bean
    @ConditionalOnProperty(value = "redisson.masterAddresses")
    public RedissonClient redissonSentinel(){
        Config config = new Config();
        ClusterServersConfig serverConfig = config.useClusterServers().addNodeAddress(redssionProperties.getSentinelAddresses())
                .setTimeout(redssionProperties.getTimeout())
                //设置集群扫描时间
                .setScanInterval(redssionProperties.getScanInterval())
                //主节点线程池数量
                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
                //从节点线程池数量
                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());

        if(StringUtils.isNotEmpty(redssionProperties.getPassword())){
            serverConfig.setPassword(redssionProperties.getPassword());
        }
        return Redisson.create(config);
    }
  1. 自定义注解,属性多了锁类型

/**
 * 描述: 基于redisson实现分布式锁
 * 支持多种锁类型
 * @Auther: gc.x
 * @Ddate:2019/10/28
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LockDistributed2 {

    /**
     * 锁的名称
     * @return
     */
    String name() default "";
    /**
     * 锁类型,默认可重入锁
     * @return
     */
    LockType lockType() default LockType.Reentrant;
    /**
     * 尝试加锁,最多等待时间
     * @return
     */
    long waitTime() default 60;
    /**
     *上锁以后xxx秒自动解锁
     * @return
     */
    long leaseTime() default 60*5;

    /**
     * 自定义业务key
     * @return
     */
    String [] keys() default {};
}
  1. 切面,和方案一类似,主要根据注解上的锁类型获取对应的redisson提供的锁。

该实现内容较多不贴出来了,结构如下
在这里插入图片描述

/**
 * @Auther: gc.x
 * @Date: 2019/10/28
 * @Description:
 */
@Aspect
@Component
@Slf4j
public class LockAspect2 {
    /**
     *  锁前缀名
     */
    public static final String LOCK_NAME_PREFIX = "lock";
    /**
     *  分隔符
     */
    public static final String LOCK_NAME_SEPARATOR = ".";

    /**
     * 表达式解析器
     */
    private ExpressionParser parser = new SpelExpressionParser();

    /**
     * 参数解析
     */
    private ParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();

    @Autowired
    private LockFactory lockFactory;

    @Around(value = "@annotation(lockDistributed2)")
    public Object around(ProceedingJoinPoint joinPoint, LockDistributed2 lockDistributed2) throws Throwable{
        // 获取方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取锁类型
        LockType type= lockDistributed2.lockType();
        String businessKeyName = getKeyName(joinPoint,lockDistributed2);
        String lockName = LOCK_NAME_PREFIX+LOCK_NAME_SEPARATOR + getName(lockDistributed2.name(), signature) + businessKeyName;
        log.info("lockName===>"+lockName);
        long waitTime = lockDistributed2.waitTime();
        long leaseTime =lockDistributed2.leaseTime();
        LockInfo lockInfo = new LockInfo(type,lockName,waitTime,leaseTime);

        ILock ilock = lockFactory.getLock(lockInfo);
        boolean currentThreadLock = false;
        try {
            currentThreadLock = ilock.acquire();
            if (!currentThreadLock) {
                throw new TimeoutException("获取锁资源等待超时");
            }
            return joinPoint.proceed();
        } finally {
            if (currentThreadLock) {
                ilock.release();
            }
        }
    }
  1. 同理测试,结束

附上码云:https://gitee.com/giteeClass/project
欢迎fork

目录
相关文章
|
存储 缓存 Android开发
android 读取WebView缓存及清理WebView缓存
1.缓存的分类: 首先要说的一点是缓存的分类,我们缓存的数据分为:页面缓存和数据缓存 页面缓存:加载一个网页时的html、JS、CSS等页面或者资源数据,这些缓存资源是由于浏览器  的行为而产生,开发者只能通过配置HTTP响应头影响浏览器的行为才能间接地影响到这些缓存数据。
4195 0
|
10月前
|
机器学习/深度学习 存储 自然语言处理
蚂蚁集团 Ling-2.5-1T 开源:万亿参数,重新定义"又快又强"
Ling-2.5-1T是蚂蚁集团inclusionAI推出的开源即时大模型(MIT协议),以“效率×效果”为核心:万亿参数、63B激活,首创混合线性注意力架构,支持百万token上下文;推理吞吐大幅提升,AIME任务仅需1/3 token即达前沿思考模型水平。ModelScope可下载。
1444 4
蚂蚁集团 Ling-2.5-1T 开源:万亿参数,重新定义"又快又强"
|
存储 SQL 关系型数据库
MySQL一张表最多能存多少数据?
MySQL一张表最多能存多少数据?
MySQL一张表最多能存多少数据?
|
8月前
|
存储 人工智能 运维
AI协同天花板!开源知识库搭配智能解析,文档无缝适配AI调用
在数字化转型持续深化的当下,企业知识呈现多源、异构、海量特征,传统知识库常陷入 “建而不用、存而不通” 的困境。作为 AI 驱动的开源知识库系统,以大模型能力为核心,打通文档管理、智能创作、问答检索与第三方集成全链路,搭配百智云智能文档解析能力,为组织提供轻量化、高可用、可扩展的知识管理解决方案,助力知识从 “静态存储” 转向 “动态赋能”。
|
机器人 5G UED
一文详解运营商发布的“5G消息白皮书”
2020年4月8日中国移动、中国电信、中国联通携手华为、小米、vivo、OPPO、中兴等11家终端厂商联合发布了《5G消息白皮书》,用来`推动传统短消息服务升级`。 三大运营商联合发布的《5G消息白皮书》创造了一种怎样的商业模式,我们该如何理解《5G消息白皮书》?
2706 1
|
关系型数据库 MySQL 分布式数据库
Hbase与MySQL对比,区别是什么?
Hbase与MySQL对比,区别是什么?
1324 2
|
开发框架 移动开发 前端开发
除了 HMR 插件,还有哪些技术可以实现热更新?
【10月更文挑战第23天】不同的热更新技术都有其特点和适用场景。开发者需要根据项目的具体需求和技术架构,选择合适的热更新技术来提高开发效率和用户体验。同时,随着技术的不断发展,热更新技术也在不断创新和完善,未来可能会出现更多更先进的热更新技术和方法。
|
JavaScript
vue 全局响应键盘按键/监听键盘事件(含 js 获取键盘keyCode值的方法)
vue 全局响应键盘按键/监听键盘事件(含 js 获取键盘keyCode值的方法)
1402 2
蓝易云 - 如何在Debian中同步系统时间?Debian系统时间配置(NTP服务)
以上就是在Debian系统中同步系统时间的步骤。通过以上步骤,你可以确保你的系统时间始终与NTP服务器保持同步,从而确保系统时间的准确性。
1392 3

热门文章

最新文章