Skip to content

876415840/distributed-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zookeeper实现分布式锁

通过zk的临时节点实现分布式锁

  • maven依赖
<dependency>
    <groupId>com.github.876415840</groupId>
    <artifactId>distributed-lock</artifactId>
    <version>1.0.0</version>
</dependency>
  • zookeeper配置
@Configuration
public class ZookeeperConfig {

    @Bean
    public CuratorFramework curatorFramework() {
        //创建重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,5);

        //创建zookeeper客户端
        CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181")
                .sessionTimeoutMs(180000)
                .retryPolicy(retryPolicy)
                .namespace("your_namespace")
                .build();

        client.start();
        return client;
    }
}
  • 锁使用
// 启动类加注解 @ComponentScan(basePackages = {"com.github.lock"}) 扫描指定包路径

@Service
public class TestService {
    @DistributeLock(value = "testLock", key = "#id", block = true)
    public void testLock(String id) {
        // do something
    }
}

About

分布式锁插件

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages