From 9f5b5d1c0114cddc501242f2e0d1243b96530638 Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Sat, 3 Apr 2021 23:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ezookeeper-starter=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0zookeeper=E5=88=86=E5=B8=83=E5=BC=8F=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 + pom.xml | 16 +++++ .../central/file/service/impl/S3Service.java | 5 +- zlt-business/user-center/pom.xml | 4 ++ zlt-commons/pom.xml | 1 + .../common/constant/CommonConstant.java | 7 +- .../redis/lock/RedissonDistributedLock.java | 4 +- .../zlt-zookeeper-spring-boot-starter/pom.xml | 31 ++++++++ .../zookeeper/ZookeeperAutoConfiguration.java | 40 +++++++++++ .../lock/ZookeeperDistributedLock.java | 71 +++++++++++++++++++ .../properties/ZookeeperProperty.java | 45 ++++++++++++ .../main/resources/META-INF/spring.factories | 2 + 12 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 zlt-commons/zlt-zookeeper-spring-boot-starter/pom.xml create mode 100644 zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/ZookeeperAutoConfiguration.java create mode 100644 zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/lock/ZookeeperDistributedLock.java create mode 100644 zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/properties/ZookeeperProperty.java create mode 100644 zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/README.md b/README.md index 615a087..fc9df8f 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,9 @@ central-platform -- 父项目,公共依赖 │ │ ├─zlt-ribbon-spring-boot-starter -- 封装Ribbon和Feign的通用操作逻辑 │ │ ├─zlt-sentinel-spring-boot-starter -- 封装Sentinel的通用操作逻辑 │ │ ├─zlt-swagger2-spring-boot-starter -- 封装Swagger通用操作逻辑 +│ │ ├─zlt-elasticsearch-spring-boot-starter -- 封装Elasticsearch通用操作逻辑 +│ │ ├─zlt-oss-spring-boot-starter -- 封装对象存储通用操作逻辑 +│ │ ├─zlt-zookeeper-spring-boot-starter -- 封装Zookeeper通用操作逻辑 │ ├─zlt-config -- 配置中心 │ ├─zlt-doc -- 项目文档 │ ├─zlt-gateway -- api网关一级工程 diff --git a/pom.xml b/pom.xml index c4e7f1c..b1d802f 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ 2.0.5 6.2.0.Final 2.7.8 + 5.1.0 1.2.2 zlt-job/**/*, zlt-register/**/*, zlt-web/**/* openjdk:8-jre-alpine @@ -355,6 +356,21 @@ zlt-oss-spring-boot-starter ${project.version} + + com.zlt + zlt-zookeeper-spring-boot-starter + ${project.version} + + + org.apache.curator + curator-recipes + ${curator.version} + + + org.apache.curator + curator-framework + ${curator.version} + org.springframework.boot diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java index 4067e98..f87679d 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/S3Service.java @@ -1,6 +1,7 @@ package com.central.file.service.impl; import cn.hutool.core.util.StrUtil; +import com.central.common.constant.CommonConstant; import com.central.file.model.FileInfo; import com.central.oss.model.ObjectInfo; import com.central.oss.properties.FileServerProperties; @@ -24,8 +25,6 @@ import java.io.OutputStream; @Service @ConditionalOnProperty(prefix = com.central.oss.properties.FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_S3) public class S3Service extends AbstractIFileService { - private static final String PATH_SPLIT = "/"; - @Resource private S3Template s3Template; @@ -64,7 +63,7 @@ public class S3Service extends AbstractIFileService { private S3Object parsePath(String path) { S3Object s3Object = new S3Object(); if (StrUtil.isNotEmpty(path)) { - int splitIndex = path.lastIndexOf(PATH_SPLIT); + int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT); if (splitIndex != -1) { s3Object.bucketName = path.substring(0, splitIndex); s3Object.objectName = path.substring(splitIndex + 1); diff --git a/zlt-business/user-center/pom.xml b/zlt-business/user-center/pom.xml index a0840c2..bfcb077 100644 --- a/zlt-business/user-center/pom.xml +++ b/zlt-business/user-center/pom.xml @@ -43,6 +43,10 @@ com.zlt search-client + + com.zlt + zlt-zookeeper-spring-boot-starter + org.springframework.boot diff --git a/zlt-commons/pom.xml b/zlt-commons/pom.xml index 4ca65a7..2bf0da7 100644 --- a/zlt-commons/pom.xml +++ b/zlt-commons/pom.xml @@ -22,5 +22,6 @@ zlt-common-core zlt-elasticsearch-spring-boot-starter zlt-oss-spring-boot-starter + zlt-zookeeper-spring-boot-starter \ No newline at end of file diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/CommonConstant.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/CommonConstant.java index 7d8afef..353e86f 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/CommonConstant.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/CommonConstant.java @@ -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 = "/"; } diff --git a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java index 37d96d6..fef1924 100644 --- a/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java +++ b/zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/lock/RedissonDistributedLock.java @@ -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); } diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/pom.xml b/zlt-commons/zlt-zookeeper-spring-boot-starter/pom.xml new file mode 100644 index 0000000..b05cf7b --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/pom.xml @@ -0,0 +1,31 @@ + + + + com.zlt + zlt-commons + 4.3.0 + + 4.0.0 + jar + zlt-zookeeper-spring-boot-starter + zookeeper通用组件 + + + com.zlt + zlt-common-core + + + + org.apache.curator + curator-recipes + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/ZookeeperAutoConfiguration.java b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/ZookeeperAutoConfiguration.java new file mode 100644 index 0000000..1c19004 --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/ZookeeperAutoConfiguration.java @@ -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 + *

+ * 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(); + } +} diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/lock/ZookeeperDistributedLock.java b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/lock/ZookeeperDistributedLock.java new file mode 100644 index 0000000..28d2480 --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/lock/ZookeeperDistributedLock.java @@ -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 + *

+ * 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; + } +} diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/properties/ZookeeperProperty.java b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/properties/ZookeeperProperty.java new file mode 100644 index 0000000..7ed7d16 --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/properties/ZookeeperProperty.java @@ -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 + *

+ * 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; +} diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..96f338c --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.central.common.zookeeper.ZookeeperAutoConfiguration \ No newline at end of file -- GitLab