# **SpringBoot** Detailed explanation of distributed lock @klock

SpringBoot distributed lock @klock detailed explanation

@Klock( name = "testKey", //key默认为lock.包名.类名-方法名;key.com.example.springmvc.klock.service.UserServiceImpl-test 如果配置了name则为lock.name-方法名;key.testKey-test
        lockType= LockType.Fair,//锁为公平锁
        waitTime = 10, //获取锁最长等待时间。10s
        leaseTime = 300,//获得锁后,自动释放锁的时间300s
        lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST, //请求过来后获取锁超时的处理策略 这里配置的直接返回失败
        releaseTimeoutStrategy = ReleaseTimeoutStrategy.FAIL_FAST)//超过300s后超时自动释放,锁处理策略 这里配置的直接返回失败,也可以配置不做处理
public void test(){
    
    
    log.info("缓存");

}

image-20230626111126436

@Klock can mark four parameters, the functions are as follows:

name: The name of the lock, corresponding to the key value of redis. The default is: lock.package name.class name-method name. The name can be specified according to the business. The rule is: lock.name-method name.

lockType: The type of lock, currently supported (reentrant lock, fair lock, read-write lock). Default: fair lock

waitTime: the longest waiting time to acquire the lock. Default: 60s. At the same time, it can also be configured uniformly through spring.klock.waitTime

leaseTime: the time to automatically release the lock after acquiring the lock. Default: 60s. At the same time, it can also be configured uniformly through spring.klock.leaseTime

lockTimeoutStrategy: The processing strategy for lock timeout, which can be configured as no processing, fast failure, or blocking waiting. The default strategy is no processing.

customLockTimeoutStrategy: To customize the lock timeout processing strategy, you need to specify the method name of the custom processing method and keep the input parameters consistent.

releaseTimeoutStrategy: When releasing a lock, the processing strategy for holding a lock that has timed out can be configured as no processing or fast failure. The default strategy is no processing.

customReleaseTimeoutStrategy: When customizing the release lock, you need to specify the method name of the custom processing method and keep the input parameters consistent.

Lock timeout processing strategy ( LockTimeoutStrategy ):

  • NO_OPERATION does not process and continues to execute business logic
  • FAIL_FAST fails quickly and will throw KlockTimeoutException
  • KEEP_ACQUIRE blocks and waits until the lock is obtained. However, after too many attempts, it will stop acquiring the lock and report an error. At this time, a deadlock is likely to occur.
  • For customization (customLockTimeoutStrategy), you need to specify the method name of the custom processing method and keep the input parameters consistent. After specifying the custom processing method, the above three strategies will be overwritten and the operation of the business logic will be intercepted.

Timeout processing strategy when releasing the lock ( ReleaseTimeoutStrategy ):

  • NO_OPERATION does not process and continues to execute business logic
  • FAIL_FAST fails quickly and will throw KlockTimeoutException
  • For customization (customReleaseTimeoutStrategy), you need to specify the method name of the custom processing method and keep the input parameters consistent. After specifying the custom processing method, the above two strategies will be overwritten. When the custom processing method is executed, the business logic has been executed and will Executed before the method returns and throws an exception.

Lock timeout processing process

Enterprise WeChat screenshot_f121de39-f6ee-43d1-ae80-44583e29996f

Custom timeout processing strategy

@Klock(name="test2Key", waitTime=2, customLockTimeoutStrategy = "customLockTimeout")
public void test2(String name, String age) {
    
    
    try {
    
    
        TimeUnit.SECONDS.sleep(3);
        log.info("acquire lock");
    } catch (InterruptedException e) {
    
    
        e.printStackTrace();
    }
}

private String customLockTimeout(String name, String age) {
    
    
    log.info("customLockTimeout name: " + name + " age: " + age);
    return "超时报错";
}

image-20230626112510883

image-20230626112612299

Guess you like

Origin blog.csdn.net/itScholar001/article/details/131394046