新增zookeeper-starter实现zookeeper分布式锁

This commit is contained in:
zlt2000
2021-04-03 23:08:42 +08:00
parent 3258ceced0
commit a6170ed6a6
12 changed files with 223 additions and 6 deletions
+1
View File
@@ -22,5 +22,6 @@
<module>zlt-common-core</module>
<module>zlt-elasticsearch-spring-boot-starter</module>
<module>zlt-oss-spring-boot-starter</module>
<module>zlt-zookeeper-spring-boot-starter</module>
</modules>
</project>
@@ -102,7 +102,7 @@ public interface CommonConstant {
String DEF_USER_PASSWORD = "123456";
String LOCK_KEY_PREFIX = "LOCK_KEY:";
String LOCK_KEY_PREFIX = "LOCK_KEY";
/**
* 租户id参数
@@ -126,4 +126,9 @@ public interface CommonConstant {
* 注册中心元数据 版本号
*/
String METADATA_VERSION = "version";
/**
* 文件分隔符
*/
String PATH_SPLIT = "/";
}
@@ -32,9 +32,9 @@ public class RedissonDistributedLock implements DistributedLock {
private ZLock getLock(String key, boolean isFair) {
RLock lock;
if (isFair) {
lock = redisson.getFairLock(CommonConstant.LOCK_KEY_PREFIX + key);
lock = redisson.getFairLock(CommonConstant.LOCK_KEY_PREFIX + ":" + key);
} else {
lock = redisson.getLock(CommonConstant.LOCK_KEY_PREFIX + key);
lock = redisson.getLock(CommonConstant.LOCK_KEY_PREFIX + ":" + key);
}
return new ZLock(lock, this);
}
@@ -0,0 +1,31 @@
<?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>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>4.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>zlt-zookeeper-spring-boot-starter</artifactId>
<description>zookeeper通用组件</description>
<dependencies>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-common-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,40 @@
package com.central.common.zookeeper;
import com.central.common.zookeeper.properties.ZookeeperProperty;
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.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* redis 配置类
*
* @author zlt
* @version 1.0
* @date 2021/4/3
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@EnableConfigurationProperties(ZookeeperProperty.class)
@ComponentScan
public class ZookeeperAutoConfiguration {
/**
* 初始化连接
*/
@Bean(initMethod = "start", destroyMethod = "close")
@ConditionalOnMissingBean
public CuratorFramework curatorFramework(ZookeeperProperty property) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(property.getBaseSleepTime(), property.getMaxRetries());
return CuratorFrameworkFactory.builder()
.connectString(property.getConnectString())
.connectionTimeoutMs(property.getConnectionTimeout())
.sessionTimeoutMs(property.getSessionTimeout())
.retryPolicy(retryPolicy)
.build();
}
}
@@ -0,0 +1,71 @@
package com.central.common.zookeeper.lock;
import com.central.common.constant.CommonConstant;
import com.central.common.exception.LockException;
import com.central.common.lock.DistributedLock;
import com.central.common.lock.ZLock;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* zookeeper分布式锁实现
*
* @author zlt
* @version 1.0
* @date 2021/4/3
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Component
@ConditionalOnProperty(prefix = "zlt.lock", name = "lockerType", havingValue = "ZK")
public class ZookeeperDistributedLock implements DistributedLock {
@Resource
private CuratorFramework client;
private ZLock getLock(String key) {
InterProcessMutex lock = new InterProcessMutex(client, getPath(key));
return new ZLock(lock, this);
}
@Override
public ZLock lock(String key, long leaseTime, TimeUnit unit, boolean isFair) throws Exception {
ZLock zLock = this.getLock(key);
InterProcessMutex ipm = (InterProcessMutex)zLock.getLock();
ipm.acquire();
return zLock;
}
@Override
public ZLock tryLock(String key, long waitTime, long leaseTime, TimeUnit unit, boolean isFair) throws Exception {
ZLock zLock = this.getLock(key);
InterProcessMutex ipm = (InterProcessMutex)zLock.getLock();
if (ipm.acquire(waitTime, unit)) {
return zLock;
}
return null;
}
@Override
public void unlock(Object lock) throws Exception {
if (lock != null) {
if (lock instanceof InterProcessMutex) {
InterProcessMutex ipm = (InterProcessMutex)lock;
if (ipm.isAcquiredInThisProcess()) {
ipm.release();
}
} else {
throw new LockException("requires InterProcessMutex type");
}
}
}
private String getPath(String key) {
return CommonConstant.PATH_SPLIT + CommonConstant.LOCK_KEY_PREFIX + CommonConstant.PATH_SPLIT + key;
}
}
@@ -0,0 +1,45 @@
package com.central.common.zookeeper.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* zookeeper配置
*
* @author zlt
* @version 1.0
* @date 2021/4/3
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Setter
@Getter
@ConfigurationProperties(prefix = "zlt.zookeeper")
public class ZookeeperProperty {
/**
* zk连接集群,多个用逗号隔开
*/
private String connectString;
/**
* 会话超时时间(毫秒)
*/
private int sessionTimeout = 15000;
/**
* 连接超时时间(毫秒)
*/
private int connectionTimeout = 15000;
/**
* 初始重试等待时间(毫秒)
*/
private int baseSleepTime = 2000;
/**
* 重试最大次数
*/
private int maxRetries = 10;
}
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.zookeeper.ZookeeperAutoConfiguration