Skip to content

Commit

Permalink
Zookeeper uses curator to implement reentring distributed locks
Browse files Browse the repository at this point in the history
  • Loading branch information
chengernest committed Jul 8, 2021
1 parent 0fb3c17 commit 612f2c1
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<module>btrace</module>
<module>activemq</module>
<module>kafka</module>
<module>zookeeper</module>
</modules>
<packaging>pom</packaging>
<name>java-all-in-one</name>
Expand Down
23 changes: 23 additions & 0 deletions zookeeper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-all-in-one</artifactId>
<groupId>cn.nicollcheng</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zookeeper</artifactId>
<packaging>pom</packaging>
<modules>
<module>zookeeper-curator-reentrantlock</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
91 changes: 91 additions & 0 deletions zookeeper/zookeeper-curator-reentrantlock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zookeeper</artifactId>
<groupId>cn.nicollcheng</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zookeeper-curator-reentrantlock</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<curator.version>5.1.0</curator.version>
</properties>

<dependencies>
<!--lock pom start-->
<!--curator-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--lock pom end-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.nicollcheng.zookeeper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ZookeeperCuratorLockApplication {

public static void main(String[] args) {
SpringApplication.run(ZookeeperCuratorLockApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.nicollcheng.zookeeper.controller;

import cn.nicollcheng.zookeeper.lock.DistributedLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <b><code>LockController</code></b>
* <p/>
* Description
* <p/>
* <b>Creation Time:</b> 2021/7/7 17:43.
*
* @author nicollcheng
* @since zookeeper-curator 2021
*/
@RestController
public class LockController {

@GetMapping("/lock")
@DistributedLock("#name")
public String lock(String name){
return "ok lock";
}

@GetMapping("/save")
public String save(String name){
return "ok save";
}

@GetMapping("/update")
public String update(String name){
return "ok update";
}

@GetMapping("/delete")
public String delete(String name){
return "ok delete";
}

@GetMapping("/select")
public String select(String name){
return "ok select";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cn.nicollcheng.zookeeper.curator;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* <b><code>ConfigProperties</code></b>
* <p/>
* Description Curator属性配置
* <p/>
* <b>Creation Time:</b> 2021/7/7 16:48.
*
* @author nicollcheng
* @since zookeeper-curator 2021
*/
@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class ConfigProperties {
/**
* 重试次数
*/
private int retryCount;
/**
* 重试间隔时间
*/
private int elapsedTimeMs;
/**
* 连接地址
*/
private String connectString;
/**
* Session过期时间
*/
private int sessionTimeoutMs;
/**
* 连接超时时间
*/
private int connectionTimeoutMs;
/**
* 分布式锁根节点
*/
private String rootLockNode = "/mutex_lock/";
/**
* 分布式锁锁超时时间,默认-1,获取锁,若失败则阻塞等待直到成功
*/
private long lockTimeout = -1;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.nicollcheng.zookeeper.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* <b><code>CuratorConfiguration</code></b>
* <p/>
* Description Curator配置类
* <p/>
* <b>Creation Time:</b> 2021/7/7 16:45.
*
* @author nicollcheng
* @since zookeeper-curator 2021
*/
@Configuration
public class CuratorConfiguration {

private final ConfigProperties configProperties;

@Autowired
public CuratorConfiguration(ConfigProperties configProperties) {
this.configProperties = configProperties;
}

@Bean(initMethod = "start")
public CuratorFramework curatorFramework() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(
configProperties.getElapsedTimeMs(),
configProperties.getRetryCount());
return CuratorFrameworkFactory.newClient(
configProperties.getConnectString(),
configProperties.getSessionTimeoutMs(),
configProperties.getConnectionTimeoutMs(),
retryPolicy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.nicollcheng.zookeeper.lock;

/**
* <b><code>DistributedLock</code></b>
* <p/>
* Description
* <p/>
* <b>Creation Time:</b> 2021/7/7 17:17.
*
* @author nicollcheng
* @since zookeeper-curator 2021
*/

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 用于标记对方法进行加锁
* created at 2019-07-05 15:15
* @author lerry
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedLock {
/**
* redis锁的key值,可使用SpEL传方法参数
*/
String value();

/**
* 超时时间,默认3秒
*/
int timeout() default 3;
}
Loading

0 comments on commit 612f2c1

Please sign in to comment.