feature(all): 增加zookeeper作为注册中心

This commit is contained in:
2023-07-12 10:26:23 +08:00
parent 2cfc009c51
commit 72bdda5546
17 changed files with 95 additions and 30 deletions

View File

@@ -22,15 +22,6 @@
<artifactId>service-configuration</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,41 @@
package com.lanyuanxiaoyao.service.zookeeper;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Zookeeper 配置
*
* @author ZhangJiacheng
* @date 2023-05-04
*/
@ConfigurationProperties("connector.zookeeper")
@Component
public class ZookeeperConfiguration {
private static final Logger logger = LoggerFactory.getLogger(ZookeeperConfiguration.class);
private String connectUrl;
@PostConstruct
private void init() {
logger.info("Configuration initial: {}", this);
}
public String getConnectUrl() {
return connectUrl;
}
public void setConnectUrl(String connectUrl) {
this.connectUrl = connectUrl;
}
@Override
public String toString() {
return "ZookeeperConfiguration{" +
"connectUrl='" + connectUrl + '\'' +
'}';
}
}

View File

@@ -1,10 +1,15 @@
package com.lanyuanxiaoyao.service.zookeeper;
import cn.hutool.core.util.ObjectUtil;
import com.eshore.odcp.hudi.connector.Constants;
import com.lanyuanxiaoyao.service.configuration.entity.zookeeper.ZookeeperNode;
import java.util.concurrent.TimeUnit;
import javax.annotation.PreDestroy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
@@ -16,8 +21,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* Zookeeper 查询
*
@@ -32,8 +35,21 @@ public class ZookeeperController {
private final CuratorFramework client;
public ZookeeperController(CuratorFramework client) {
this.client = client;
public ZookeeperController(ZookeeperConfiguration zookeeperConfiguration) {
this.client = CuratorFrameworkFactory.builder()
.connectString(zookeeperConfiguration.getConnectUrl())
.retryPolicy(new ExponentialBackoffRetry((int) Constants.SECOND, 3))
.sessionTimeoutMs((int) Constants.SECOND)
.connectionTimeoutMs((int) (30 * Constants.SECOND))
.build();
client.start();
}
@PreDestroy
public void destroy() {
if (ObjectUtil.isNotNull(client)) {
client.close();
}
}
@GetMapping("exists_path")

View File

@@ -2,7 +2,7 @@ spring:
application:
name: service-zookeeper-query
profiles:
include: random-port,common,eureka,metrics
cloud:
zookeeper:
connect-string: b5m1.hdp.dc:2181,b5m2.hdp.dc:2181,b5m3.hdp.dc:2181
include: random-port,common,discovery,metrics
connector:
zookeeper:
connect-string: b5m1.hdp.dc:2181,b5m2.hdp.dc:2181,b5m3.hdp.dc:2181