You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/06/12 01:20:19 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1994]local registry will send MicroserviceInstanceRegisteredEvent cause boot earlier

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9d1ad1c  [SCB-1994]local registry will send MicroserviceInstanceRegisteredEvent cause boot earlier
9d1ad1c is described below

commit 9d1ad1c14fd927610919023a39b9eb6edb5e086a
Author: liubao <bi...@qq.com>
AuthorDate: Thu Jun 11 15:23:02 2020 +0800

    [SCB-1994]local registry will send MicroserviceInstanceRegisteredEvent cause boot earlier
---
 .../org/apache/servicecomb/core/SCBEngine.java     |  9 ++--
 .../core/bootup/ServiceInformationCollector.java   | 14 +----
 .../servicecomb/registry/RegistrationManager.java  | 61 +++++++++++++++++++++-
 .../event/MicroserviceInstanceRegisteredEvent.java | 24 +++++++++
 .../localregistry/LocalRegistration.java           |  3 +-
 .../servicecomb/serviceregistry/RegistryUtils.java |  8 ++-
 .../zeroconfig/ZeroConfigRegistration.java         |  8 +++
 .../zeroconfig/client/ZeroConfigClient.java        |  3 --
 8 files changed, 106 insertions(+), 24 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index 2bbef3d..feea662 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -55,10 +55,10 @@ import org.apache.servicecomb.foundation.vertx.VertxUtils;
 import org.apache.servicecomb.foundation.vertx.client.http.HttpClients;
 import org.apache.servicecomb.registry.DiscoveryManager;
 import org.apache.servicecomb.registry.RegistrationManager;
+import org.apache.servicecomb.registry.api.event.MicroserviceInstanceRegisteredEvent;
 import org.apache.servicecomb.registry.api.registry.MicroserviceInstanceStatus;
 import org.apache.servicecomb.registry.consumer.MicroserviceVersions;
 import org.apache.servicecomb.registry.definition.MicroserviceNameParser;
-import org.apache.servicecomb.registry.api.event.MicroserviceInstanceRegisteredEvent;
 import org.apache.servicecomb.registry.swagger.SwaggerLoader;
 import org.apache.servicecomb.swagger.engine.SwaggerEnvironment;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
@@ -582,14 +582,13 @@ public class SCBEngine {
 
     @Subscribe
     @EnableExceptionPropagation
-    public void afterRegistryInstance(MicroserviceInstanceRegisteredEvent microserviceInstanceRegisteredEvent) {
-      LOGGER.info("receive MicroserviceInstanceRegisteredEvent event, check instance Id...");
-
-      if (!StringUtils.isEmpty(RegistrationManager.INSTANCE.getMicroserviceInstance().getInstanceId())) {
+    public void afterRegistryInstance(MicroserviceInstanceRegisteredEvent event) {
+      if (event.isRegistrationManager()) {
         LOGGER.info("instance registry succeeds for the first time, will send AFTER_REGISTRY event.");
         engine.setStatus(SCBStatus.UP);
         engine.triggerEvent(EventType.AFTER_REGISTRY);
         EventManager.unregister(this);
+        // keep this message to be WARN, used to detect service ready. 
         LOGGER.warn("ServiceComb is ready.");
       }
     }
diff --git a/core/src/main/java/org/apache/servicecomb/core/bootup/ServiceInformationCollector.java b/core/src/main/java/org/apache/servicecomb/core/bootup/ServiceInformationCollector.java
index f4c347b..b94b947 100644
--- a/core/src/main/java/org/apache/servicecomb/core/bootup/ServiceInformationCollector.java
+++ b/core/src/main/java/org/apache/servicecomb/core/bootup/ServiceInformationCollector.java
@@ -19,23 +19,11 @@ package org.apache.servicecomb.core.bootup;
 
 import org.apache.servicecomb.registry.RegistrationManager;
 
-import io.vertx.core.spi.json.JsonCodec;
-
 public class ServiceInformationCollector implements BootUpInformationCollector {
 
   @Override
   public String collect() {
-    return "App ID: " + RegistrationManager.INSTANCE.getMicroservice().getAppId()
-        + "\n" + "Service Name: " + RegistrationManager.INSTANCE.getMicroservice().getServiceName()
-        + "\n" + "Version: " + RegistrationManager.INSTANCE.getMicroservice().getVersion()
-        + "\n" + "Environment: " + RegistrationManager.INSTANCE.getMicroservice().getEnvironment()
-        + "\n" + "Service ID: " + RegistrationManager.INSTANCE.getMicroserviceInstance().getServiceId()
-        + "\n" + "Instance ID: " + RegistrationManager.INSTANCE.getMicroserviceInstance().getInstanceId()
-        + "\n" + "Endpoints: " + getEndpoints();
-  }
-
-  private String getEndpoints() {
-    return JsonCodec.INSTANCE.toString(RegistrationManager.INSTANCE.getMicroserviceInstance().getEndpoints());
+    return RegistrationManager.INSTANCE.info();
   }
 
   @Override
diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java
index c052c45..14aaae5 100644
--- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java
+++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java
@@ -24,13 +24,17 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import org.apache.http.client.utils.URIBuilder;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.common.net.IpPort;
 import org.apache.servicecomb.foundation.common.net.NetUtils;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.registry.api.Registration;
+import org.apache.servicecomb.registry.api.event.MicroserviceInstanceRegisteredEvent;
 import org.apache.servicecomb.registry.api.registry.BasePath;
 import org.apache.servicecomb.registry.api.registry.Microservice;
 import org.apache.servicecomb.registry.api.registry.MicroserviceInstance;
@@ -42,8 +46,11 @@ import org.apache.servicecomb.registry.swagger.SwaggerLoader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.eventbus.Subscribe;
 import com.netflix.config.DynamicPropertyFactory;
 
+import io.vertx.core.spi.json.JsonCodec;
+
 public class RegistrationManager {
   private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationManager.class);
 
@@ -115,6 +122,7 @@ public class RegistrationManager {
   }
 
   public void run() {
+    EventManager.getEventBus().register(new AfterServiceInstanceRegistryHandler(registrationList.size()));
     registrationList.forEach(registration -> registration.run());
   }
 
@@ -273,4 +281,55 @@ public class RegistrationManager {
     int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting;
     return new IpPort(publicAddressSetting, publishPort);
   }
-}
+
+  public String info() {
+    StringBuilder result = new StringBuilder();
+    AtomicBoolean first = new AtomicBoolean(true);
+    registrationList.forEach(registration -> {
+      if (first.getAndSet(false)) {
+        result.append("App ID: " + registration.getAppId() + "\n");
+        result.append("Service Name: " + registration.getMicroservice().getServiceName() + "\n");
+        result.append("Version: " + registration.getMicroservice().getVersion() + "\n");
+        result.append("Environment: " + registration.getMicroservice().getEnvironment() + "\n");
+        result.append("Endpoints: " + getEndpoints(registration.getMicroserviceInstance().getEndpoints()) + "\n");
+        result.append("Registration implementations:\n");
+      }
+
+      result.append("\tname:" + registration.name() + "\n");
+      result.append("\t\tService ID: " + registration.getMicroservice().getServiceId() + "\n");
+      result.append("\t\tInstance ID: " + registration.getMicroserviceInstance().getInstanceId() + "\n");
+    });
+    return result.toString();
+  }
+
+  private String getEndpoints(List<String> endpoints) {
+    return JsonCodec.INSTANCE.toString(endpoints);
+  }
+
+  public static class AfterServiceInstanceRegistryHandler {
+    private AtomicInteger instanceRegisterCounter;
+
+    AfterServiceInstanceRegistryHandler(int counter) {
+      instanceRegisterCounter = new AtomicInteger(counter);
+    }
+
+    @Subscribe
+    public void afterRegistryInstance(MicroserviceInstanceRegisteredEvent event) {
+      LOGGER.info("receive MicroserviceInstanceRegisteredEvent event, registration={}, instance id={}",
+          event.getRegistrationName(),
+          event.getInstanceId());
+
+      if (instanceRegisterCounter.decrementAndGet() > 0) {
+        return;
+      }
+
+      EventManager.unregister(this);
+
+      EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent(
+          "Registration Manager",
+          null,
+          true
+      ));
+    }
+  }
+}
\ No newline at end of file
diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/event/MicroserviceInstanceRegisteredEvent.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/event/MicroserviceInstanceRegisteredEvent.java
index 9455f2b..cb5de0c 100644
--- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/event/MicroserviceInstanceRegisteredEvent.java
+++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/event/MicroserviceInstanceRegisteredEvent.java
@@ -21,5 +21,29 @@ package org.apache.servicecomb.registry.api.event;
  * when registration is ready, should post this event.
  */
 public class MicroserviceInstanceRegisteredEvent {
+  private String registrationName;
 
+  private String instanceId;
+
+  // If this event is sent by RegistrationManager, which means all Registration are successful.
+  private boolean registrationManager;
+
+  public MicroserviceInstanceRegisteredEvent(String registrationName, String instanceId,
+      boolean registrationManager) {
+    this.registrationName = registrationName;
+    this.instanceId = instanceId;
+    this.registrationManager = registrationManager;
+  }
+
+  public String getInstanceId() {
+    return instanceId;
+  }
+
+  public String getRegistrationName() {
+    return registrationName;
+  }
+
+  public boolean isRegistrationManager() {
+    return registrationManager;
+  }
 }
diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistration.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistration.java
index 77b758f..e4130cc 100644
--- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistration.java
+++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistration.java
@@ -42,7 +42,8 @@ public class LocalRegistration implements Registration {
   @Override
   public void run() {
     localRegistrationStore.run();
-    EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent());
+    EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent(NAME,
+        localRegistrationStore.getSelfMicroserviceInstance().getInstanceId(), false));
   }
 
   @Override
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
index 4a04459..0bc7477 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java
@@ -300,7 +300,13 @@ public final class RegistryUtils {
       if (instanceRegisterCounter.decrementAndGet() > 0) {
         return;
       }
-      EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent());
+
+      // for simplicity , only send the last one event. can do it better, maybe.
+      EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent(
+          ServiceCenterRegistration.NAME,
+          serviceRegistry.getMicroserviceInstance().getInstanceId(),
+          false
+      ));
     }
   }
 }
diff --git a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigRegistration.java b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigRegistration.java
index 6e49d6c..22b56dd 100644
--- a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigRegistration.java
+++ b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigRegistration.java
@@ -21,7 +21,9 @@ import static org.apache.servicecomb.zeroconfig.ZeroConfigRegistryConstants.ORDE
 
 import java.util.Collection;
 
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.registry.api.Registration;
+import org.apache.servicecomb.registry.api.event.MicroserviceInstanceRegisteredEvent;
 import org.apache.servicecomb.registry.api.registry.BasePath;
 import org.apache.servicecomb.registry.api.registry.Microservice;
 import org.apache.servicecomb.registry.api.registry.MicroserviceInstance;
@@ -63,6 +65,12 @@ public class ZeroConfigRegistration implements Registration {
 
     if (!registerResult) {
       LOGGER.error("Failed to Register Service Instance in Zero-Config mode");
+    } else {
+      EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent(
+          NAME,
+          getMicroserviceInstance().getInstanceId(),
+          false
+      ));
     }
   }
 
diff --git a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/client/ZeroConfigClient.java b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/client/ZeroConfigClient.java
index c4c10be..acc1272 100644
--- a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/client/ZeroConfigClient.java
+++ b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/client/ZeroConfigClient.java
@@ -115,9 +115,6 @@ public class ZeroConfigClient {
 
   public boolean register() {
     Map<String, String> data = prepareRegisterData();
-    if (data == null || data.isEmpty()) {
-      return false;
-    }
     String serviceInstanceId = doRegister(data);
     return StringUtils.isNotEmpty(serviceInstanceId);
   }