liubao68 commented on code in PR #4647:
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/4647#discussion_r1899880583


##########
service-registry/registry-consul/src/main/java/org/apache/servicecomb/registry/consul/ConsulDiscovery.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.consul;
+
+import com.ecwid.consul.v1.ConsulClient;
+import com.ecwid.consul.v1.QueryParams;
+import com.ecwid.consul.v1.Response;
+import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
+import com.ecwid.consul.v1.health.HealthServicesRequest;
+import com.ecwid.consul.v1.health.model.Check;
+import com.ecwid.consul.v1.health.model.HealthService;
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
+import jakarta.annotation.Resource;
+import jakarta.validation.constraints.NotNull;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.config.BootStrapProperties;
+import org.apache.servicecomb.registry.api.Discovery;
+import org.apache.servicecomb.registry.consul.config.ConsulDiscoveryProperties;
+import org.apache.servicecomb.registry.consul.config.ConsulProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.env.Environment;
+import org.springframework.scheduling.TaskScheduler;
+import org.springframework.util.CollectionUtils;
+
+import java.lang.reflect.Type;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledFuture;
+
+public class ConsulDiscovery implements Discovery<ConsulDiscoveryInstance> {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(ConsulDiscovery.class);
+
+  @Resource
+  private ConsulProperties consulProperties;
+
+  @Resource
+  private ConsulDiscoveryProperties consulDiscoveryProperties;
+
+  @Resource
+  private ConsulClient consulClient;
+
+  @Resource
+  private Environment environment;
+
+  @Resource
+  private TaskScheduler taskScheduler;
+
+  @Value("${servicecomb.rest.address:127.0.0.1:8080}")
+  private String restAddress;
+
+  private String serverPort = "";
+
+  private List<ConsulDiscoveryInstance> consulDiscoveryInstanceList;
+
+  private InstanceChangedListener<ConsulDiscoveryInstance> 
instanceChangedListener;
+
+  private ScheduledFuture<?> scheduledFuture;
+
+  @Override
+  public String name() {
+    return ConsulConst.CONSUL_REGISTRY_NAME;
+  }
+
+  @Override
+  public boolean enabled(String application, String serviceName) {
+    return consulDiscoveryProperties.isEnabled();
+  }
+
+  @Override
+  public List<ConsulDiscoveryInstance> findServiceInstances(String 
application, String serviceName) {
+    logger.info("findServiceInstances application:{}, serviceName:{}", 
application, serviceName);
+    consulDiscoveryInstanceList = getInstances(serviceName, new 
QueryParams(consulDiscoveryProperties.getConsistencyMode()));
+    return consulDiscoveryInstanceList;
+  }
+
+  @Override
+  public List<String> findServices(String application) {
+    logger.info("ConsulDiscovery findServices(application={})", application);
+    CatalogServicesRequest.Builder builder = 
CatalogServicesRequest.newBuilder()
+        .setQueryParams(QueryParams.DEFAULT);
+    if (StringUtils.isNotBlank(consulDiscoveryProperties.getAclToken())) {
+      builder.setToken(consulDiscoveryProperties.getAclToken());
+    }
+    CatalogServicesRequest request = builder.build();
+    return new 
ArrayList<>(consulClient.getCatalogServices(request).getValue().keySet());
+  }
+
+  @Override
+  public void 
setInstanceChangedListener(InstanceChangedListener<ConsulDiscoveryInstance> 
instanceChangedListener) {
+    this.instanceChangedListener = instanceChangedListener;
+  }
+
+  @Override
+  public boolean enabled() {
+    return consulDiscoveryProperties.isEnabled();
+  }
+
+  @Override
+  public void init() {
+    logger.info("ConsulDiscovery init");
+    if (restAddress.contains("?")) {
+      serverPort = restAddress.substring(restAddress.indexOf(":") + 1, 
restAddress.indexOf("?"));
+    } else {
+      serverPort = restAddress.substring(restAddress.indexOf(":") + 1);
+    }
+  }
+
+  @Override
+  public void run() {
+    logger.info("ConsulDiscovery run");
+    String serviceName = BootStrapProperties.readServiceName(environment);
+    scheduledFuture = taskScheduler.scheduleWithFixedDelay(
+        () -> instanceChangedListener.onInstanceChanged(
+            name(), BootStrapProperties.readApplication(environment), 
serviceName, getInstances(serviceName, new 
QueryParams(consulDiscoveryProperties.getConsistencyMode()))
+        ), Duration.ofMillis(consulDiscoveryProperties.getDelayTime())

Review Comment:
   call `onInstanceChanged` must base on instance list already changed. Make 
sure if consul can indication instance change. 



-- 
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