SweetWuXiaoMei commented on code in PR #4564:
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/4564#discussion_r1817709519


##########
service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/EtcdDiscovery.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.registry.etcd;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.config.BootStrapProperties;
+import org.apache.servicecomb.foundation.common.utils.JsonUtils;
+import org.apache.servicecomb.registry.api.Discovery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+
+import com.google.common.collect.Lists;
+
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.Watch;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.WatchOption;
+
+public class EtcdDiscovery implements Discovery<EtcdDiscoveryInstance> {
+
+  private Environment environment;
+
+  private String basePath;
+
+  private EtcdRegistryProperties etcdRegistryProperties;
+
+  private Client client;
+
+  private InstanceChangedListener<EtcdDiscoveryInstance> 
instanceChangedListener;
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(EtcdDiscovery.class);
+
+  @Autowired
+  @SuppressWarnings("unused")
+  public void setEnvironment(Environment environment) {
+    this.environment = environment;
+  }
+
+  @Autowired
+  @SuppressWarnings("unused")
+  public void setEtcdRegistryProperties(EtcdRegistryProperties 
etcdRegistryProperties) {
+    this.etcdRegistryProperties = etcdRegistryProperties;
+  }
+
+  @Override
+  public String name() {
+    return EtcdConst.ETCD_REGISTRY_NAME;
+  }
+
+  @Override
+  public boolean enabled(String application, String serviceName) {
+    return 
environment.getProperty(String.format(EtcdConst.ETCD_DISCOVERY_ENABLED, 
application, serviceName),
+        boolean.class, true);
+  }
+
+  @Override
+  public List<EtcdDiscoveryInstance> findServiceInstances(String application, 
String serviceName) {
+
+    String prefixPath = basePath + "/" + application + "/" + serviceName;
+    SingletonManager.getInstance().computeIfAbsent(prefixPath, serName -> {
+      Watch watchClient = client.getWatchClient();
+      try {
+        ByteSequence prefixByteSeq = ByteSequence.from(prefixPath, 
Charset.defaultCharset());
+        watchClient.watch(
+            prefixByteSeq,
+            WatchOption.builder().withPrefix(prefixByteSeq).build(),
+            resp -> {
+              Map<String, EtcdDiscoveryInstance> instanceMap = 
SingletonManager.getInstance()
+                  .get("instance:" + prefixPath);
+              resp.getEvents().forEach(event -> {
+                String key = 
event.getKeyValue().getKey().toString(Charset.defaultCharset());
+                switch (event.getEventType()) {
+                  case PUT -> {
+                    EtcdDiscoveryInstance newInstance = 
getEtcdDiscoveryInstance(event.getKeyValue());
+                    instanceMap.put(key, newInstance);
+                  }
+                  case DELETE -> instanceMap.remove(key);
+                  default -> LOGGER.error("unkonw event");
+                }
+              });
+              List<EtcdDiscoveryInstance> discoveryInstanceList = new 
ArrayList<>(instanceMap.values());
+              instanceChangedListener.onInstanceChanged(name(), application, 
serviceName, discoveryInstanceList);
+            }
+        );
+      } catch (Exception e) {
+        LOGGER.error("Failed to add watch", e);
+      }
+      return watchClient;
+    });
+
+    List<KeyValue> endpointKv = getValuesByPrefix(prefixPath);
+    List<EtcdDiscoveryInstance> etcdDiscoveryInstances = 
convertServiceInstanceList(endpointKv);
+    SingletonManager.getInstance().computeIfAbsent("instance:" + prefixPath, v 
-> etcdDiscoveryInstances.stream()

Review Comment:
   My first version of the code was written according to this idea. When the 
node changes, all instances will be queried, but an exception will be thrown 
that the event loop is blocked. I think vert may have some checks to prevent 
the event loop from blocking. The second reason is that the performance of the 
query is also average.I will modify some hard code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to