+ * 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 00000000..28d24808 --- /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 00000000..7ed7d167 --- /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 00000000..96f338c0 --- /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