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

commit b15678af629f69d3d8e7e5aabcc2187b74c50441
Author: yaohaishi <[email protected]>
AuthorDate: Wed Sep 19 16:36:59 2018 +0800

    [SCB-926] add entry point of adding 3rd party service instances
---
 .../serviceregistry/ServiceRegistry.java           | 27 +++++++
 .../consumer/MicroserviceVersions.java             |  2 +-
 .../consumer/StaticMicroserviceVersions.java       | 44 ++++++++++
 .../registry/AbstractServiceRegistry.java          | 32 +++++++-
 .../registry/TestAbstractServiceRegistry.java      | 94 +++++++++++++++++++++-
 5 files changed, 195 insertions(+), 4 deletions(-)

diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
index a8cf785..bd79dd3 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java
@@ -59,4 +59,31 @@ public interface ServiceRegistry {
   Microservice getRemoteMicroservice(String microserviceId);
 
   Features getFeatures();
+
+  /**
+   * <p>
+   * Register a third party service if not registered before, and add it's 
instances into
+   * {@linkplain 
org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersions 
StaticMicroserviceVersions}.
+   * </p>
+   * <p>
+   * The registered third party service has the same {@code appId} and {@code 
environment} as this microservice instance has,
+   * and there is only one schema represented by {@code schemaIntfCls}, whose 
name is the same as {@code microserviceName}.
+   * </p>
+   *
+   * @param microserviceName name of the 3rd party service, and this param 
also specifies the schemaId
+   * @param version version of this 3rd party service
+   * @param schemaIntfCls the producer interface of the service. This 
interface is used to generate swagger schema and
+   * can also be used for the proxy interface of RPC style invocation.
+   * @param instances the instances of this 3rd party service. Users only need 
to specify the endpoint information, other
+   * necessary information will be generate and set in the implementation of 
this method.
+   */
+  void registryMicroserviceMapping(String microserviceName, String version, 
Class<?> schemaIntfCls,
+      List<MicroserviceInstance> instances);
+
+  /**
+   * @see #registryMicroserviceMapping(String, String, Class, List)
+   * @param endpoints the endpoints of 3rd party service. Each of endpoints 
will be treated as a separated instance.
+   */
+  void registryMicroserviceMappingByEndpoints(String microserviceName, String 
version,
+      Class<?> schemaIntfCls, List<String> endpoints);
 }
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
index ab4f479..4e4ae38 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
@@ -74,7 +74,7 @@ public class MicroserviceVersions {
   // only pendingPullCount is 0, then do a real pull 
   private AtomicInteger pendingPullCount = new AtomicInteger();
 
-  private boolean validated = false;
+  boolean validated = false;
 
   public MicroserviceVersions(AppManager appManager, String appId, String 
microserviceName) {
     this.appManager = appManager;
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/StaticMicroserviceVersions.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/StaticMicroserviceVersions.java
new file mode 100644
index 0000000..692a5a2
--- /dev/null
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/StaticMicroserviceVersions.java
@@ -0,0 +1,44 @@
+/*
+ * 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.serviceregistry.consumer;
+
+import java.util.List;
+
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+
+public class StaticMicroserviceVersions extends MicroserviceVersions {
+
+  private Class<?> schemaIntfCls;
+
+  public StaticMicroserviceVersions(AppManager appManager, String appId, 
String microserviceName,
+      Class<?> schemaIntfCls) {
+    super(appManager, appId, microserviceName);
+
+    validated = true;
+    this.schemaIntfCls = schemaIntfCls;
+  }
+
+  @Override
+  public void pullInstances() {
+    // instance information is stored locally, do not pull from sc
+  }
+
+  public void addInstances(String version, List<MicroserviceInstance> 
instances) {
+
+  }
+}
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
index 6e880fa..c9e310d 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
@@ -19,12 +19,15 @@ package org.apache.servicecomb.serviceregistry.registry;
 import static 
org.apache.servicecomb.foundation.common.base.ServiceCombConstants.CONFIG_DEFAULT_REGISTER_BY;
 import static 
org.apache.servicecomb.foundation.common.base.ServiceCombConstants.CONFIG_FRAMEWORK_DEFAULT_NAME;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.servicecomb.serviceregistry.Features;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
 import org.apache.servicecomb.serviceregistry.api.Const;
 import org.apache.servicecomb.serviceregistry.api.registry.BasePath;
@@ -40,7 +43,9 @@ import 
org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import 
org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
 import org.apache.servicecomb.serviceregistry.consumer.AppManager;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager;
 import 
org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionFactory;
+import 
org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersions;
 import 
org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition;
 import 
org.apache.servicecomb.serviceregistry.task.MicroserviceServiceCenterTask;
 import org.apache.servicecomb.serviceregistry.task.ServiceCenterTask;
@@ -181,7 +186,6 @@ public abstract class AbstractServiceRegistry implements 
ServiceRegistry {
     microservice.setRegisterBy(CONFIG_DEFAULT_REGISTER_BY);
   }
 
-
   private void loadStaticConfiguration() {
     // TODO 如果yaml定义了paths规则属性,替换默认值,现需要DynamicPropertyFactory支持数组获取
     List<BasePath> paths = microservice.getPaths();
@@ -303,4 +307,30 @@ public abstract class AbstractServiceRegistry implements 
ServiceRegistry {
     eventBus.post(new ShutdownEvent());
     unregisterInstance();
   }
+
+  @Override
+  public void registryMicroserviceMapping(String microserviceName, String 
version, Class<?> schemaIntfCls,
+      List<MicroserviceInstance> instances) {
+    String app = RegistryUtils.getAppId();
+    MicroserviceManager microserviceManager = 
appManager.getOrCreateMicroserviceManager(app);
+    StaticMicroserviceVersions microserviceVersions =
+        (StaticMicroserviceVersions) microserviceManager.getVersionsByName()
+            .computeIfAbsent(microserviceName,
+                svcName -> new StaticMicroserviceVersions(this.appManager, 
app, microserviceName, schemaIntfCls));
+
+    microserviceVersions.addInstances(version, instances);
+  }
+
+  @Override
+  public void registryMicroserviceMappingByEndpoints(String microserviceName, 
String version,
+      Class<?> schemaIntfCls, List<String> endpoints) {
+    ArrayList<MicroserviceInstance> microserviceInstances = new ArrayList<>();
+    for (String endpoint : endpoints) {
+      MicroserviceInstance instance = new MicroserviceInstance();
+      instance.setEndpoints(Collections.singletonList(endpoint));
+      microserviceInstances.add(instance);
+    }
+
+    registryMicroserviceMapping(microserviceName, version, schemaIntfCls, 
microserviceInstances);
+  }
 }
diff --git 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java
 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java
index b223c3d..d4582c9 100644
--- 
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java
+++ 
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestAbstractServiceRegistry.java
@@ -17,20 +17,38 @@
 
 package org.apache.servicecomb.serviceregistry.registry;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.xml.ws.Holder;
+
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+import org.apache.servicecomb.serviceregistry.consumer.AppManager;
 import 
org.apache.servicecomb.serviceregistry.consumer.DefaultMicroserviceVersionFactory;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions;
+import 
org.apache.servicecomb.serviceregistry.consumer.StaticMicroserviceVersions;
 import 
org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition;
 import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
 
 import com.google.common.eventbus.EventBus;
 
 import mockit.Expectations;
+import mockit.Mock;
+import mockit.MockUp;
 import mockit.Mocked;
 
 public class TestAbstractServiceRegistry {
@@ -70,6 +88,17 @@ public class TestAbstractServiceRegistry {
   public void setup() {
     registry =
         new AbstractServiceRegistryForTest(eventBus, serviceRegistryConfig, 
microserviceDefinition);
+    RegistryUtils.setServiceRegistry(registry);
+  }
+
+  @After
+  public void tearDown() {
+    registry.appManager = null;
+  }
+
+  @AfterClass
+  public static void afterClass() {
+    RegistryUtils.setServiceRegistry(null);
   }
 
   @Test
@@ -96,8 +125,7 @@ public class TestAbstractServiceRegistry {
   }
 
   @Test
-  public void initAppManagerSpecialMicroserviceVersionFactoryFailed()
-      throws InstantiationException, IllegalAccessException, 
ClassNotFoundException {
+  public void initAppManagerSpecialMicroserviceVersionFactoryFailed() {
     new Expectations() {
       {
         serviceRegistryConfig.getMicroserviceVersionFactory();
@@ -109,4 +137,66 @@ public class TestAbstractServiceRegistry {
 
     registry.init();
   }
+
+  @Test
+  public void registryMicroserviceMapping() {
+    String testServiceName = "testService";
+    final String testVersion = "1.0.1";
+
+    registry.appManager = Mockito.mock(AppManager.class);
+    MicroserviceManager microserviceManager = 
Mockito.mock(MicroserviceManager.class);
+    
Mockito.when(registry.appManager.getOrCreateMicroserviceManager(this.appId)).thenReturn(microserviceManager);
+    HashMap<String, MicroserviceVersions> versionsByName = new HashMap<>();
+    
Mockito.when(microserviceManager.getVersionsByName()).thenReturn(versionsByName);
+
+    ArrayList<MicroserviceInstance> instancesParam = new ArrayList<>();
+    Holder<Boolean> checked = new Holder<>(false);
+    StaticMicroserviceVersions microserviceVersions = new 
MockUp<StaticMicroserviceVersions>() {
+      @Mock
+      void addInstances(String version, List<MicroserviceInstance> instances) {
+        Assert.assertEquals(version, version);
+        Assert.assertSame(instancesParam, instances);
+        checked.value = true;
+      }
+    }.getMockInstance();
+    versionsByName.put(testServiceName, microserviceVersions);
+
+    registry.registryMicroserviceMapping(testServiceName, testVersion, 
Test3rdPartyServiceIntf.class, instancesParam);
+    Assert.assertTrue(checked.value);
+  }
+
+  @Test
+  public void registryMicroserviceMappingByEndpoints() {
+    String testServiceName = "testService";
+    final String testVersion = "1.0.1";
+
+    registry.appManager = Mockito.mock(AppManager.class);
+    MicroserviceManager microserviceManager = 
Mockito.mock(MicroserviceManager.class);
+    
Mockito.when(registry.appManager.getOrCreateMicroserviceManager(this.appId)).thenReturn(microserviceManager);
+    HashMap<String, MicroserviceVersions> versionsByName = new HashMap<>();
+    
Mockito.when(microserviceManager.getVersionsByName()).thenReturn(versionsByName);
+
+    ArrayList<MicroserviceInstance> instancesParam = new ArrayList<>();
+    Holder<Boolean> checked = new Holder<>(false);
+    StaticMicroserviceVersions microserviceVersions = new 
MockUp<StaticMicroserviceVersions>() {
+      @Mock
+      void addInstances(String version, List<MicroserviceInstance> instances) {
+        Assert.assertEquals("1.0.1", version);
+        Assert.assertEquals(2, instances.size());
+        Assert.assertEquals(1, instances.get(0).getEndpoints().size());
+        Assert.assertEquals("http://127.0.0.1:8080";, 
instances.get(0).getEndpoints().get(0));
+        Assert.assertEquals(1, instances.get(1).getEndpoints().size());
+        Assert.assertEquals("http://127.0.0.1:8081";, 
instances.get(1).getEndpoints().get(0));
+        checked.value = true;
+      }
+    }.getMockInstance();
+    versionsByName.put(testServiceName, microserviceVersions);
+
+    registry.registryMicroserviceMappingByEndpoints("testService", "1.0.1", 
Test3rdPartyServiceIntf.class,
+        Arrays.asList("http://127.0.0.1:8080";, "http://127.0.0.1:8081";));
+    Assert.assertTrue(checked.value);
+  }
+
+  private interface Test3rdPartyServiceIntf {
+  }
 }

Reply via email to