-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Zookeeper uses curator to implement reentring distributed locks
- Loading branch information
1 parent
0fb3c17
commit 612f2c1
Showing
11 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
...reentrantlock/src/main/java/cn/nicollcheng/zookeeper/ZookeeperCuratorLockApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...rator-reentrantlock/src/main/java/cn/nicollcheng/zookeeper/controller/LockController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...urator-reentrantlock/src/main/java/cn/nicollcheng/zookeeper/curator/ConfigProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...or-reentrantlock/src/main/java/cn/nicollcheng/zookeeper/curator/CuratorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...er-curator-reentrantlock/src/main/java/cn/nicollcheng/zookeeper/lock/DistributedLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.