You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/03/15 11:02:36 UTC

[GitHub] liubao68 closed pull request #598: [SCB-402] normal invocation in edge use private classloader

liubao68 closed pull request #598: [SCB-402] normal invocation in edge use private classloader
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/598
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/schema/ConsumerSchemaFactory.java b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ConsumerSchemaFactory.java
index 1c30a5ce6..50c28cf72 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/schema/ConsumerSchemaFactory.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ConsumerSchemaFactory.java
@@ -20,12 +20,16 @@
 import javax.inject.Inject;
 
 import org.apache.servicecomb.core.definition.MicroserviceMeta;
+import org.apache.servicecomb.core.definition.MicroserviceVersionMeta;
 import org.apache.servicecomb.core.definition.SchemaMeta;
 import org.apache.servicecomb.core.definition.SchemaUtils;
 import org.apache.servicecomb.core.definition.loader.SchemaListenerManager;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
+import org.apache.servicecomb.serviceregistry.consumer.AppManager;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -40,12 +44,18 @@
   @Inject
   protected SchemaListenerManager schemaListenerManager;
 
+  AppManager appManager;
+
   private final Object lock = new Object();
 
   public void setSchemaListenerManager(SchemaListenerManager schemaListenerManager) {
     this.schemaListenerManager = schemaListenerManager;
   }
 
+  public void setAppManager(AppManager appManager) {
+    this.appManager = appManager;
+  }
+
   // 透明rpc场景,因为每次指定schema调用,所以可以懒加载
   // 而rest场景,是根据path反向查找schema,所以必须所有的schema都存在才行
   // 两种场景可能都会来初始化,为避免管理复杂度,改为全部全量初始化
@@ -53,7 +63,7 @@ public void setSchemaListenerManager(SchemaListenerManager schemaListenerManager
   // 用于rest consumer注册的场景,此时启动流程已经完成,需要主动通知listener
   // microserviceName可能是本app内的微服务
   // 也可能是appid:name形式的其他app的微服务
-  public MicroserviceMeta getOrCreateMicroserviceMeta(String microserviceName, String microserviceVersionRule) {
+  public MicroserviceMeta getOrCreateMicroserviceMeta(String microserviceName, String versionRule) {
     MicroserviceMeta microserviceMeta = microserviceMetaManager.findValue(microserviceName);
     if (microserviceMeta != null) {
       return microserviceMeta;
@@ -66,52 +76,40 @@ public MicroserviceMeta getOrCreateMicroserviceMeta(String microserviceName, Str
         return microserviceMeta;
       }
 
-      // 获取指定服务中有哪些schemaId
-      // 先取本地,再从服务中心取,如果服务中心取成功了,则将schema id合并处理
       microserviceMeta = new MicroserviceMeta(microserviceName);
-      Microservice microservice =
-          findMicroservice(microserviceMeta, microserviceVersionRule);
-      if (microservice == null) {
+      if (appManager == null) {
+        appManager = RegistryUtils.getServiceRegistry().getAppManager();
+      }
+      MicroserviceVersionRule microserviceVersionRule =
+          appManager.getOrCreateMicroserviceVersionRule(microserviceMeta.getAppId(),
+              microserviceName,
+              versionRule);
+      MicroserviceVersion microserviceVersion = microserviceVersionRule.getLatestMicroserviceVersion();
+      if (microserviceVersion == null) {
         throw new IllegalStateException(
             String.format("Probably invoke a service before it is registered, appId=%s, name=%s",
                 microserviceMeta.getAppId(),
                 microserviceName));
       }
 
-      getOrCreateConsumerSchema(microserviceMeta, microservice);
+      if (MicroserviceVersionMeta.class.isInstance(microserviceVersion)) {
+        microserviceMeta = ((MicroserviceVersionMeta) microserviceVersion).getMicroserviceMeta();
+      } else {
+        getOrCreateConsumerSchema(microserviceMeta, microserviceVersion.getMicroservice());
+        schemaListenerManager.notifySchemaListener(microserviceMeta);
+      }
+
+      LOGGER.info("Found schema ids from service center, {}:{}:{}:{}",
+          microserviceMeta.getAppId(),
+          microserviceName,
+          versionRule,
+          microserviceVersion.getMicroservice().getSchemas());
 
       microserviceMetaManager.register(microserviceName, microserviceMeta);
-      schemaListenerManager.notifySchemaListener(microserviceMeta);
       return microserviceMeta;
     }
   }
 
-  protected Microservice findMicroservice(MicroserviceMeta microserviceMeta, String microserviceVersionRule) {
-    String appId = microserviceMeta.getAppId();
-    String microserviceName = microserviceMeta.getName();
-    ServiceRegistryClient client = RegistryUtils.getServiceRegistryClient();
-    String microserviceId = client.getMicroserviceId(appId,
-        microserviceMeta.getShortName(),
-        microserviceVersionRule);
-    if (StringUtils.isEmpty(microserviceId)) {
-      LOGGER.error("can not get microservice id, {}:{}:{}", appId, microserviceName, microserviceVersionRule);
-      return null;
-    }
-
-    Microservice microservice = client.getMicroservice(microserviceId);
-    if (microservice == null) {
-      LOGGER.error("can not get microservice, {}:{}:{}", appId, microserviceName, microserviceVersionRule);
-      return null;
-    }
-
-    LOGGER.info("Found schema ids from service center, {}:{}:{}:{}",
-        appId,
-        microserviceName,
-        microserviceVersionRule,
-        microservice.getSchemas());
-    return microservice;
-  }
-
   // 允许consumerIntf与schemaId对应的interface原型不同,用于支持context类型的参数
   // consumerIntf为null,表示原型与契约相同
   public void getOrCreateConsumerSchema(MicroserviceMeta microserviceMeta, Microservice microservice) {
diff --git a/core/src/main/java/org/apache/servicecomb/core/unittest/UnitTestMeta.java b/core/src/main/java/org/apache/servicecomb/core/unittest/UnitTestMeta.java
index 3715b1c62..78ef6fd74 100644
--- a/core/src/main/java/org/apache/servicecomb/core/unittest/UnitTestMeta.java
+++ b/core/src/main/java/org/apache/servicecomb/core/unittest/UnitTestMeta.java
@@ -34,7 +34,6 @@
 import org.apache.servicecomb.core.handler.impl.SimpleLoadBalanceHandler;
 import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
-import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.swagger.generator.core.unittest.UnitTestSwaggerUtils;
 import org.mockito.Mockito;
 import org.springframework.context.ApplicationContext;
@@ -58,13 +57,7 @@ public static synchronized void init() {
 
     ConsumerProviderManager consumerProviderManager = new ConsumerProviderManager();
 
-    ConsumerSchemaFactory consumerSchemaFactory = new ConsumerSchemaFactory() {
-      @Override
-      protected Microservice findMicroservice(MicroserviceMeta microserviceMeta,
-          String microserviceVersionRule) {
-        return null;
-      }
-    };
+    ConsumerSchemaFactory consumerSchemaFactory = new ConsumerSchemaFactory();
     consumerSchemaFactory.setMicroserviceMetaManager(microserviceMetaManager);
     consumerSchemaFactory.setSchemaListenerManager(schemaListenerManager);
 
@@ -77,7 +70,7 @@ protected Microservice findMicroservice(MicroserviceMeta microserviceMeta,
     Config config = new Config();
     Class<?> cls = SimpleLoadBalanceHandler.class;
     config.getHandlerClassMap().put("simpleLB", (Class<Handler>) cls);
-    ProducerHandlerManager.INSTANCE.init(config);
+    ProducerHandlerManager.INSTANCE.init(new Config());
     ConsumerHandlerManager.INSTANCE.init(config);
 
     ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java
index 5d17fe1a5..fb473b751 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java
@@ -34,6 +34,7 @@
 import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
 import org.apache.servicecomb.core.transport.TransportManager;
 import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
@@ -41,8 +42,10 @@
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.event.ContextClosedEvent;
 import org.springframework.context.event.ContextRefreshedEvent;
 import org.springframework.context.support.AbstractApplicationContext;
@@ -53,9 +56,17 @@
 import mockit.Mocked;
 
 public class TestCseApplicationListener {
+  static ApplicationContext orgContext;
+
+  @BeforeClass
+  public static void classSetup() {
+    orgContext = BeanUtils.getContext();
+  }
+
   @AfterClass
   public static void teardown() {
     AbstractEndpointsCache.init(null, null);
+    BeanUtils.setContext(orgContext);
   }
 
   @After
diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/loader/TestDynamicSchemaLoader.java b/core/src/test/java/org/apache/servicecomb/core/definition/loader/TestDynamicSchemaLoader.java
index 534cd1284..7e1a55c32 100644
--- a/core/src/test/java/org/apache/servicecomb/core/definition/loader/TestDynamicSchemaLoader.java
+++ b/core/src/test/java/org/apache/servicecomb/core/definition/loader/TestDynamicSchemaLoader.java
@@ -71,7 +71,7 @@ public static void teardown() {
   public void testRegisterSchemas() {
     DynamicSchemaLoader.INSTANCE.registerSchemas("classpath*:test/test/schema.yaml");
     SchemaMeta schemaMeta = microserviceMetaManager.ensureFindSchemaMeta("perfClient", "schema");
-    Assert.assertEquals("cse.gen.pojotest.perfClient.schema", schemaMeta.getPackageName());
+    Assert.assertEquals("cse.gen.app.perfClient.schema", schemaMeta.getPackageName());
   }
 
   @SuppressWarnings("deprecation")
diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestConsumerSchemaFactory.java b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestConsumerSchemaFactory.java
index 203f11b42..98d2ddf41 100644
--- a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestConsumerSchemaFactory.java
+++ b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestConsumerSchemaFactory.java
@@ -30,24 +30,18 @@
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
-import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory;
 import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext;
 import org.apache.servicecomb.swagger.generator.core.unittest.UnitTestSwaggerUtils;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.mockito.Mockito;
-
-import mockit.Deencapsulation;
 
 public class TestConsumerSchemaFactory {
   private static ConsumerSchemaFactory consumerSchemaFactory = new ConsumerSchemaFactory();
 
-  private static ServiceRegistryClient registryClient = Mockito.mock(ServiceRegistryClient.class);
-
-  private static ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
-
   private static SchemaListener schemaListener = new SchemaListener() {
 
     @Override
@@ -68,8 +62,9 @@ public int add(int x, int y) {
 
   @BeforeClass
   public static void init() {
-    Deencapsulation.setField(RegistryUtils.class, "serviceRegistry", serviceRegistry);
-    Mockito.when(serviceRegistry.getServiceRegistryClient()).thenReturn(registryClient);
+    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
+    serviceRegistry.init();
+    RegistryUtils.setServiceRegistry(serviceRegistry);
 
     SchemaListenerManager schemaListenerManager = new SchemaListenerManager();
     schemaListenerManager.setSchemaListenerList(Arrays.asList(schemaListener));
@@ -92,26 +87,29 @@ public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
     SchemaMeta schemaMeta = new UnitTestMeta().getOrCreateSchemaMeta(TestConsumerSchemaFactoryImpl.class);
     String content = UnitTestSwaggerUtils.pretty(schemaMeta.getSwagger());
 
-    Mockito.when(registryClient.getMicroserviceId("app", "ms", "latest")).thenReturn("0");
-    Mockito.when(registryClient.getSchema("0", "schema")).thenReturn(content);
-
     Microservice microservice = new Microservice();
     microservice.setAppId("app");
     microservice.setServiceId("0");
     microservice.setServiceName("ms");
+    microservice.setVersion("1.0.0");
     microservice.addSchema("schema", content);
-    Mockito.when(registryClient.getMicroservice("0")).thenReturn(microservice);
+    serviceRegistry.getServiceRegistryClient().registerMicroservice(microservice);
+
+    MicroserviceInstance instance = new MicroserviceInstance();
+    instance.setServiceId("0");
+    instance.setInstanceId("0");
+    serviceRegistry.getServiceRegistryClient().registerMicroserviceInstance(instance);
   }
 
   @AfterClass
   public static void teardown() {
-    Deencapsulation.setField(RegistryUtils.class, "serviceRegistry", null);
+    RegistryUtils.setServiceRegistry(null);
   }
 
   @Test
   public void testGetOrCreateConsumer() {
     MicroserviceMeta microserviceMeta =
-        consumerSchemaFactory.getOrCreateMicroserviceMeta("app:ms", "latest");
+        consumerSchemaFactory.getOrCreateMicroserviceMeta("ms", "latest");
     OperationMeta operationMeta = microserviceMeta.ensureFindOperation("schema.add");
     Assert.assertEquals("add", operationMeta.getOperationId());
   }
diff --git a/core/src/test/resources/microservice.yaml b/core/src/test/resources/microservice.yaml
index b895f8ce2..698b20f36 100644
--- a/core/src/test/resources/microservice.yaml
+++ b/core/src/test/resources/microservice.yaml
@@ -15,7 +15,7 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-APPLICATION_ID: pojotest
+APPLICATION_ID: app
 service_description:
   name: perfClient
   version: 0.0.1


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services