SpringBoot distributed lock-Distributed-Lock

Distributed lock: Distributed-Lock

1. Introduction

​Distributed -Lock was born to solve resource contention in distributed systems. In the current era of distributed and microservice architecture, multiple services may snatch the first resource. If not controlled, it may lead to unimaginable consequences, or some operations cannot be performed multiple times at the same time.

​ 1. The underlying storage currently supports only three types: Redis, Zookeeper, and Mysql . Redis is recommended.

​ 2. It supports the automatic release of the lock over time to prevent deadlock, and is also a reentrant lock.

3. Simple and convenient to use, less intrusive, the code only needs one annotation @Lock .

4. Both the resource ID of the lock and the request ID support Spel expressions

2. Use

1. Add dependencies

github address: https://github.com/wens1121/distributed-lock

After downloading the source code, package it: mvn install

import dependencies

<dependency>
  <groupId>com.distributed-lock</groupId>
  <artifactId>lock-spring-boot-starter</artifactId>
  <version>1.0.1</version>
</dependency>

2. Configure storage dependencies

​ Specify storage dependencies

spring:
	dispers-lock:
		storage: redis/zookeeper/mysql

The default is not to configure, the three methods have priority, and will be automatically assembled according to the dependent environment.

Priority order:
redis > zookeeper > mysql redis> zookeeper> mysqlredis>zookeeper>mysql

3. Instructions for use environment

Redis

依赖RedisTemplate 如果springboot中没有配置RedisTemplate,则不可使用

Zookeeper

​ Depends on pom

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.11.0</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.11.0</version>
    <scope>provided</scope>
</dependency>

​ Configuration

spring:
  dispers-lock:
    storage: zookeeper
    zookeeper-config:
      ipPorts: 127.0.0.1:2181

Mysql

依赖DataSource,如果没有dataSource则不会加载,多数据源情况下通过配置可以指定数据源。

For detailed configuration, see the LockConfig class

3. Code

It is very convenient to use, just pay attention to the @Lock annotation

@Target(value = {
    
    ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lock {
    
    
    /** 锁的key */
    String key() ;

    /** 锁的请求ID 值为-1,则使用UUID*/
    String id() default "'0'";

    /** 超时时间 */
    long timeout() default 1;

    /** 时间单位 */
    TimeUnit unit() default TimeUnit.HOURS;

    /** 锁类型,默认方法结束后释放锁 */
    LockTypeEnum lockType() default LockTypeEnum.AUTO_LOCK;
}
/**
 * @Author 温少
 * @Description  说明: 只加锁,方法执行前加锁
 * @Date 2020/9/11 3:57 下午
 * @Param  * @param
 * @return void
 **/
@Lock(key = "'test'",id = "#user.name + '_aaaaa'",lockType = LockTypeEnum.LOCK)
public void test(User user){
    
    
    System.out.println("------------>>>>>>>>"+user);
}


/**
 * @Author 温少
 * @Description  说明: 只解锁,方法执行结束进行解锁
 * @Date 2020/9/11 3:57 下午
 * @Param  * @param
 * @return void
 **/
@Lock(key = "test",lockType = LockTypeEnum.UNLOCK)
public void test2(){
    
    
    System.out.println("------------>>>>>>>>");
}


/**
 * @Author 温少
 * @Description  说明: 自动加解锁,方法执行前加锁,执行完解锁
 * @Date 2020/9/11 3:57 下午
 * @Param  * @param
 * @return void
 **/
@Lock(key = "'data'",id = "#id" ,lockType = LockTypeEnum.AUTO_LOCK)
public void test3(String id){
    
    
    System.out.println("-----------------"+id);
}

3. Problems

1. The parameter list name of the method obtained in AOP is empty, resulting in a null pointer exception

在springboot 2.0之前,动态代理默认使用的是jdk动态代理,之后默认使用的是cglib动态代理。使用jdk动态代理是无法获取到方法的参数列表的,所以要修改配置,改为强制使用cglib代理

spring:
  aop:
      proxy-target-class: true

Guess you like

Origin blog.csdn.net/weixin_33613947/article/details/109056823