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 2018/03/15 11:02:36 UTC

[incubator-servicecomb-java-chassis] branch master updated: SCB-402 normal invocation in edge use private classloader

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/incubator-servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 24e11d0  SCB-402 normal invocation in edge use private classloader
24e11d0 is described below

commit 24e11d0011f89e81ee21c66f211e6c51c545ccb2
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Mar 15 12:41:38 2018 +0800

    SCB-402 normal invocation in edge use private classloader
---
 .../definition/schema/ConsumerSchemaFactory.java   | 66 +++++++++++-----------
 .../servicecomb/core/unittest/UnitTestMeta.java    | 11 +---
 .../core/TestCseApplicationListener.java           | 11 ++++
 .../definition/loader/TestDynamicSchemaLoader.java |  2 +-
 .../schema/TestConsumerSchemaFactory.java          | 30 +++++-----
 core/src/test/resources/microservice.yaml          |  2 +-
 6 files changed, 61 insertions(+), 61 deletions(-)

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 1c30a5c..50c28cf 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 @@ package org.apache.servicecomb.core.definition.schema;
 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 @@ public class ConsumerSchemaFactory extends AbstractSchemaFactory<ConsumerSchemaC
   @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 class ConsumerSchemaFactory extends AbstractSchemaFactory<ConsumerSchemaC
   // 用于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 class ConsumerSchemaFactory extends AbstractSchemaFactory<ConsumerSchemaC
         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 3715b1c..78ef6fd 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.config.Config;
 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 class UnitTestMeta {
 
     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 @@ public class UnitTestMeta {
     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 5d17fe1..fb473b7 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.consumer.ReferenceConfigUtils;
 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.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterT
 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.Injectable;
 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 534cd12..7e1a55c 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 class TestDynamicSchemaLoader {
   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 203f11b..98d2ddf 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.foundation.common.utils.ReflectUtils;
 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 class TestConsumerSchemaFactory {
 
   @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 class TestConsumerSchemaFactory {
     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 b895f8c..698b20f 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

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.