You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2023/05/08 08:43:05 UTC
[shenyu] branch master updated: new feature for shenyu discovery service impl zk DiscoveryService( #4562) (#4572)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 0667e7cf9 new feature for shenyu discovery service impl zk DiscoveryService( #4562) (#4572)
0667e7cf9 is described below
commit 0667e7cf99364fa9ae650f6f705595572c434360
Author: 杨文杰 <31...@users.noreply.github.com>
AuthorDate: Mon May 8 16:42:52 2023 +0800
new feature for shenyu discovery service impl zk DiscoveryService( #4562) (#4572)
* impl zk DiscoveryService
* change tcp proxy code
* trigger ci
---------
Co-authored-by: likeguo <33...@users.noreply.github.com>
Co-authored-by: xiaoyu <xi...@apache.org>
---
shenyu-discovery/shenyu-discovery-api/pom.xml | 5 +
.../shenyu-discovery-zookeeper/pom.xml | 15 ++-
.../zookeeper/ZookeeperDiscoveryService.java | 126 ++++++++++++++++++++-
3 files changed, 142 insertions(+), 4 deletions(-)
diff --git a/shenyu-discovery/shenyu-discovery-api/pom.xml b/shenyu-discovery/shenyu-discovery-api/pom.xml
index 2b5e17b81..1daefb58d 100644
--- a/shenyu-discovery/shenyu-discovery-api/pom.xml
+++ b/shenyu-discovery/shenyu-discovery-api/pom.xml
@@ -33,6 +33,11 @@
<artifactId>shenyu-spi</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/shenyu-discovery/shenyu-discovery-zookeeper/pom.xml b/shenyu-discovery/shenyu-discovery-zookeeper/pom.xml
index 89468ccd8..4b494dc58 100644
--- a/shenyu-discovery/shenyu-discovery-zookeeper/pom.xml
+++ b/shenyu-discovery/shenyu-discovery-zookeeper/pom.xml
@@ -33,7 +33,20 @@
<artifactId>shenyu-discovery-api</artifactId>
<version>${project.version}</version>
</dependency>
-
+ <dependency>
+ <groupId>org.apache.curator</groupId>
+ <artifactId>curator-framework</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.curator</groupId>
+ <artifactId>curator-recipes</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-common</artifactId>
+ <version>${project.version}</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/shenyu-discovery/shenyu-discovery-zookeeper/src/main/java/org/apache/shenyu/discovery/zookeeper/ZookeeperDiscoveryService.java b/shenyu-discovery/shenyu-discovery-zookeeper/src/main/java/org/apache/shenyu/discovery/zookeeper/ZookeeperDiscoveryService.java
index 8e6e2ebae..56eed33f6 100644
--- a/shenyu-discovery/shenyu-discovery-zookeeper/src/main/java/org/apache/shenyu/discovery/zookeeper/ZookeeperDiscoveryService.java
+++ b/shenyu-discovery/shenyu-discovery-zookeeper/src/main/java/org/apache/shenyu/discovery/zookeeper/ZookeeperDiscoveryService.java
@@ -17,10 +17,27 @@
package org.apache.shenyu.discovery.zookeeper;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.api.CuratorWatcher;
+import org.apache.curator.framework.state.ConnectionState;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.discovery.api.ShenyuDiscoveryService;
import org.apache.shenyu.discovery.api.config.DiscoveryConfig;
+import org.apache.shenyu.discovery.api.listener.DataChangedEvent;
import org.apache.shenyu.discovery.api.listener.DataChangedEventListener;
import org.apache.shenyu.spi.Join;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.WatchedEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
/**
* The type Zookeeper for shenyu discovery service.
@@ -28,23 +45,126 @@ import org.apache.shenyu.spi.Join;
@Join
public class ZookeeperDiscoveryService implements ShenyuDiscoveryService {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperDiscoveryService.class);
+
+ private CuratorFramework client;
+
+ private final Map<String, String> nodeDataMap = new HashMap<>();
+
@Override
public void init(final DiscoveryConfig config) {
+ String baseSleepTimeMilliseconds = config.getProps().getProperty("baseSleepTimeMilliseconds", "1000");
+ String maxRetries = config.getProps().getProperty("maxRetries", "3");
+ String maxSleepTimeMilliseconds = config.getProps().getProperty("maxSleepTimeMilliseconds", "1000");
+ String connectionTimeoutMilliseconds = config.getProps().getProperty("connectionTimeoutMilliseconds", "1000");
+ String sessionTimeoutMilliseconds = config.getProps().getProperty("sessionTimeoutMilliseconds", "1000");
+ String digest = config.getProps().getProperty("digest", null);
+ ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(Integer.parseInt(baseSleepTimeMilliseconds), Integer.parseInt(maxRetries), Integer.parseInt(maxSleepTimeMilliseconds));
+ CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
+ .connectString(config.getServerList())
+ .retryPolicy(retryPolicy)
+ .connectionTimeoutMs(Integer.parseInt(connectionTimeoutMilliseconds))
+ .sessionTimeoutMs(Integer.parseInt(sessionTimeoutMilliseconds))
+ .namespace(config.getName());
+ if (StringUtils.isNoneBlank(digest)) {
+ builder.authorization("digest", digest.getBytes(StandardCharsets.UTF_8));
+ }
+ this.client = builder.build();
+ this.start();
+ }
+
+ private void start() {
+ this.client.getConnectionStateListenable().addListener((c, newState) -> {
+ if (newState == ConnectionState.RECONNECTED) {
+ nodeDataMap.forEach((k, v) -> {
+ if (!this.isExist(k)) {
+ this.createOrUpdate(k, v, CreateMode.EPHEMERAL);
+ LOGGER.info("zookeeper client register instance success: key={}|value={}", k, v);
+ }
+ });
+ }
+ });
+ this.client.start();
+ try {
+ this.client.blockUntilConnected();
+ } catch (InterruptedException e) {
+ throw new ShenyuException(e);
+ }
+ }
+ private boolean isExist(final String key) {
+ try {
+ return null != client.checkExists().forPath(key);
+ } catch (Exception e) {
+ throw new ShenyuException(e);
+ }
+ }
+
+ private void createOrUpdate(final String key, final String value, final CreateMode mode) {
+ String val = StringUtils.isEmpty(value) ? "" : value;
+ try {
+ this.client.create().orSetData().creatingParentsIfNeeded().withMode(mode).forPath(key, val.getBytes(StandardCharsets.UTF_8));
+ } catch (Exception e) {
+ throw new ShenyuException(e);
+ }
}
@Override
public void watcher(final String key, final DataChangedEventListener listener) {
-
+ try {
+ this.client.getData().usingWatcher(new CuratorWatcher() {
+ @Override
+ public void process(final WatchedEvent watchedEvent) throws Exception {
+ if (listener != null) {
+ String path = Objects.isNull(watchedEvent.getPath()) ? "" : watchedEvent.getPath();
+ if (StringUtils.isNoneBlank(path)) {
+ client.getData().usingWatcher(this).forPath(path);
+ byte[] ret = client.getData().forPath(key);
+ String data = Objects.isNull(ret) ? null : new String(ret, StandardCharsets.UTF_8);
+ LOGGER.info("shenyu ZookeeperDiscoveryService onChange key={}", path);
+ listener.onChange(buildDataChangedEvent(path, data, watchedEvent));
+ }
+ }
+ }
+ }).forPath(key);
+ } catch (Exception e) {
+ throw new ShenyuException(e);
+ }
}
@Override
public void register(final String key, final String value) {
-
+ this.createOrUpdate(key, value, CreateMode.EPHEMERAL);
}
@Override
public String getData(final String key) {
- return null;
+ try {
+ byte[] ret = client.getData().forPath(key);
+ return Objects.isNull(ret) ? null : new String(ret, StandardCharsets.UTF_8);
+ } catch (Exception e) {
+ throw new ShenyuException(e);
+ }
+ }
+
+ private DataChangedEvent buildDataChangedEvent(final String key, final String value, final WatchedEvent watchedEvent) {
+ DataChangedEvent.Event event = null;
+ switch (watchedEvent.getType()) {
+ case NodeCreated:
+ event = DataChangedEvent.Event.ADDED;
+ break;
+ case NodeDeleted:
+ event = DataChangedEvent.Event.DELETED;
+ break;
+ case NodeDataChanged:
+ event = DataChangedEvent.Event.UPDATED;
+ break;
+ case NodeChildrenChanged:
+ case DataWatchRemoved:
+ case ChildWatchRemoved:
+ default:
+ event = DataChangedEvent.Event.IGNORED;
+ }
+ return new DataChangedEvent(key, value, event);
}
}