Skip to content

qingyuan1232/distributed-lock-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

分布式锁工具包

使用方法

springboot 项目

1.拉取代码并打包

mvn clean install

2.pom引用

  <dependency>
    <groupId>com.qingyuan1232</groupId>
    <artifactId>redis-lock-spring-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>

3.项目中使用

注解式 支持SpEL表达式

    @PostMapping("/test")
    @SLock(key = {"#user"}, lockFailHandler = "lockFailHandler")
    public Object test(String user) throws InterruptedException {
        Thread.sleep(1000);
        return Thread.currentThread() + "success" + user;
    }

    public Object lockFailHandler(String user) {
        return Thread.currentThread() + "lock fail" + user;
    }

编程式

    @Autowired
    private LockTemplate lockTemplate;

    @PostMapping("/test")
    public Object test(String user) throws InterruptedException {
        try {
            if (lockTemplate.lock(user,1000L,1000L)){
                Thread.sleep(1000);
            }else {
                //加锁失败
            }
        }finally {
            lockTemplate.unLock(user);
        }
        return Thread.currentThread() + "success" + user;
    }

Releases

No releases published

Packages

No packages published

Languages