This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch 2.8.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/2.8.x by this push:
     new 1383912c6 [#4687] fixed service center isolated address cannot be 
restored problem (#4688)
1383912c6 is described below

commit 1383912c69e937a7b85c854ef0bbe2f943b68d61
Author: Alex <[email protected]>
AuthorDate: Thu Feb 6 11:47:52 2025 +0800

    [#4687] fixed service center isolated address cannot be restored problem 
(#4688)
---
 .../utils/ServiceCombServiceAvailableUtils.java    |  5 ++-
 .../serviceregistry/client/IpPortManager.java      | 12 ++++++
 .../client/ServiceRegistryClient.java              |  5 +++
 .../client/http/ServiceRegistryClientImpl.java     | 47 ++++++++++++++++++++++
 .../refresh/ServiceRegistryAddressManager.java     |  2 +-
 .../task/MicroserviceInstanceRegisterTask.java     | 21 ++++++++++
 .../client/LocalServiceRegistryClientImpl.java     |  5 +++
 7 files changed, 94 insertions(+), 3 deletions(-)

diff --git 
a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/utils/ServiceCombServiceAvailableUtils.java
 
b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/utils/ServiceCombServiceAvailableUtils.java
index 9139e65f9..3f7af48d7 100644
--- 
a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/utils/ServiceCombServiceAvailableUtils.java
+++ 
b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/utils/ServiceCombServiceAvailableUtils.java
@@ -55,7 +55,7 @@ public class ServiceCombServiceAvailableUtils {
     }
   }
 
-  private static boolean telnetCheckAddress(String address) {
+  public static boolean telnetCheckAddress(String address) {
     URI ipPort = parseIpPortFromURI(address);
     if (ipPort == null) {
       return false;
@@ -71,7 +71,8 @@ public class ServiceCombServiceAvailableUtils {
 
   private static URI parseIpPortFromURI(String address) {
     try {
-      return new URI(address);
+      String newAddress = address.startsWith("http") ? address : "https://"; + 
address;
+      return new URI(newAddress);
     } catch (URISyntaxException e) {
       LOGGER.error("build uri error with address [{}].", address);
       return null;
diff --git 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
index f1dce18ca..1dcd10a0e 100644
--- 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
+++ 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
@@ -88,4 +88,16 @@ public class IpPortManager {
   public void recordState(String address) {
     addressManger.recordFailState(address);
   }
+
+  public void recordSuccessState(String address) {
+    addressManger.recordSuccessState(address);
+  }
+
+  public List<String> getIsolationAddresses() {
+    return addressManger.getIsolationAddresses();
+  }
+
+  public IpPort transformIpPort(String address) {
+    return addressManger.transformIpPort(address);
+  }
 }
diff --git 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
index e1328d88a..7dae87eed 100644
--- 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
+++ 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
@@ -181,4 +181,9 @@ public interface ServiceRegistryClient {
    * @return whether this operation success
    */
   boolean updateMicroserviceInstanceStatus(String microserviceId, String 
instanceId, MicroserviceInstanceStatus status);
+
+  /**
+   * Check service center isolation addresses
+   */
+  void checkIsolationAddressAvailable();
 }
diff --git 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index 5227f7810..28311a0eb 100644
--- 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -35,6 +35,7 @@ import javax.ws.rs.core.Response.Status;
 import org.apache.servicecomb.foundation.common.net.IpPort;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
+import 
org.apache.servicecomb.http.client.utils.ServiceCombServiceAvailableUtils;
 import 
org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent;
 import org.apache.servicecomb.registry.api.registry.FindInstancesResponse;
 import org.apache.servicecomb.registry.api.registry.Framework;
@@ -90,6 +91,8 @@ public final class ServiceRegistryClientImpl implements 
ServiceRegistryClient {
 
   private static final String ERR_SCHEMA_NOT_EXISTS = "400016";
 
+  private static final String ADDRESS_CHECK_PATH = 
"/v4/default/registry/health/readiness";
+
   private final IpPortManager ipPortManager;
 
   // key是本进程的微服务id和服务管理中心的id
@@ -275,11 +278,13 @@ public final class ServiceRegistryClientImpl implements 
ServiceRegistryClient {
               switch (response.statusCode()) {
                 case 304:
                   mInstances.setNeedRefresh(false);
+                  
ipPortManager.recordSuccessState(requestContext.getIpPort().toString());
                   break;
                 case 200:
                   mInstances
                       
.setInstancesResponse(JsonUtils.readValue(bodyBuffer.getBytes(), 
FindInstancesResponse.class));
                   mInstances.setNeedRefresh(true);
+                  
ipPortManager.recordSuccessState(requestContext.getIpPort().toString());
                   break;
                 case 400: {
                   @SuppressWarnings("unchecked")
@@ -289,6 +294,7 @@ public final class ServiceRegistryClientImpl implements 
ServiceRegistryClient {
                     mInstances.setNeedRefresh(false);
                   }
                   LOGGER.warn("failed to findInstances: " + bodyBuffer);
+                  
ipPortManager.recordState(requestContext.getIpPort().toString());
                 }
                 break;
                 default:
@@ -965,6 +971,47 @@ public final class ServiceRegistryClientImpl implements 
ServiceRegistryClient {
     return false;
   }
 
+  @Override
+  public void checkIsolationAddressAvailable() {
+    Holder<GetAllServicesResponse> holder = new Holder<>();
+    List<String> isolationAddresses = ipPortManager.getIsolationAddresses();
+    for (String address : isolationAddresses) {
+      CountDownLatch countDownLatch = new CountDownLatch(1);
+      restClientUtil.get(ipPortManager.transformIpPort(address),
+          ADDRESS_CHECK_PATH,
+          new RequestParam(),
+          addressSyncHandler(countDownLatch, GetAllServicesResponse.class, 
holder));
+      try {
+        countDownLatch.await();
+        if (holder.statusCode == 200) {
+          ipPortManager.recordSuccessState(address);
+          return;
+        }
+        if (holder.statusCode == 404 && 
ServiceCombServiceAvailableUtils.telnetCheckAddress(address)) {
+          ipPortManager.recordSuccessState(address);
+          return;
+        }
+      } catch (Exception e) {
+        LOGGER.error("check service center isolation address [{}] failed", 
address);
+      }
+    }
+  }
+
+  public <T> Handler<RestResponse> addressSyncHandler(CountDownLatch 
countDownLatch, Class<T> cls,
+      Holder<T> holder) {
+    return restResponse -> {
+      HttpClientResponse response = restResponse.getResponse();
+      if (response == null) {
+        countDownLatch.countDown();
+        holder.setStatusCode(500);
+        return;
+      }
+      holder.setStatusCode(response.statusCode());
+      sendUnAuthorizedEvent(response);
+      countDownLatch.countDown();
+    };
+  }
+
   @Subscribe
   public void onMicroserviceHeartbeatTask(MicroserviceInstanceHeartbeatTask 
event) {
     if (HeartbeatResult.SUCCESS.equals(event.getHeartbeatResult())) {
diff --git 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/ServiceRegistryAddressManager.java
 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/ServiceRegistryAddressManager.java
index 875b8b643..4f25eafba 100644
--- 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/ServiceRegistryAddressManager.java
+++ 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/ServiceRegistryAddressManager.java
@@ -45,7 +45,7 @@ public class ServiceRegistryAddressManager extends 
AbstractAddressManager {
     return new URIEndpointObject(endpoint).toString();
   }
 
-  private IpPort transformIpPort(String address) {
+  public IpPort transformIpPort(String address) {
     URI uri = URI.create(URI_PREFIX + address);
     return new IpPort(uri.getHost(), uri.getPort());
   }
diff --git 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
index 119c7aa02..0e98fb3b5 100644
--- 
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
+++ 
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceInstanceRegisterTask.java
@@ -16,6 +16,10 @@
  */
 package org.apache.servicecomb.serviceregistry.task;
 
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.commons.lang3.StringUtils;
 import org.apache.servicecomb.registry.RegistrationManager;
 import org.apache.servicecomb.registry.api.registry.Microservice;
@@ -36,6 +40,10 @@ public class MicroserviceInstanceRegisterTask extends 
AbstractRegisterTask {
 
   private final MicroserviceInstance microserviceInstance;
 
+  private final ScheduledExecutorService addrCheckExecutor;
+
+  private boolean isAddrCheckInit = false;
+
   public MicroserviceInstanceRegisterTask(EventBus eventBus, 
ServiceRegistryConfig serviceRegistryConfig,
       ServiceRegistryClient srClient,
       Microservice microservice) {
@@ -43,6 +51,7 @@ public class MicroserviceInstanceRegisterTask extends 
AbstractRegisterTask {
 
     this.serviceRegistryConfig = serviceRegistryConfig;
     this.microserviceInstance = microservice.getInstance();
+    this.addrCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new 
Thread(t, "sc-addr-check"));
   }
 
   @Subscribe
@@ -90,6 +99,18 @@ public class MicroserviceInstanceRegisterTask extends 
AbstractRegisterTask {
         microserviceInstance.getEndpoints(),
         microserviceInstance.getHealthCheck().getTTL());
 
+    if (!isAddrCheckInit) {
+      addrCheckExecutor.scheduleWithFixedDelay(new CheckAddressTask(), 0,
+          serviceRegistryConfig.getHeartbeatInterval(), TimeUnit.SECONDS);
+      isAddrCheckInit = true;
+    }
     return true;
   }
+
+  class CheckAddressTask implements Runnable {
+    @Override
+    public void run() {
+      srClient.checkIsolationAddressAvailable();
+    }
+  }
 }
diff --git 
a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
 
b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
index f0cdcf7ea..47a2371dd 100644
--- 
a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
+++ 
b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
@@ -458,4 +458,9 @@ public class LocalServiceRegistryClientImpl implements 
ServiceRegistryClient {
     microserviceInstance.setStatus(status);
     return true;
   }
+
+  @Override
+  public void checkIsolationAddressAvailable() {
+    // NOT SUPPORT
+  }
 }

Reply via email to