You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ya...@apache.org on 2020/03/09 16:15:03 UTC

[servicecomb-java-chassis] 14/19: [SCB-1691] Each registry client use isolated IpPortManager

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

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

commit 4abbc6def108d196679618491542da48318129ca
Author: yhs0092 <yh...@163.com>
AuthorDate: Sun Feb 16 16:05:09 2020 +0800

    [SCB-1691] Each registry client use isolated IpPortManager
---
 .../serviceregistry/client/IpPortManager.java      |  8 +--
 .../client/http/ServiceRegistryClientImpl.java     | 14 ++++-
 .../registry/AbstractServiceRegistry.java          |  9 +---
 .../registry/RemoteServiceRegistry.java            | 12 +----
 .../serviceregistry/client/TestIpPortManager.java  |  7 +--
 .../client/http/TestClientHttp.java                | 62 ++++++++++------------
 .../client/http/TestServiceRegistryClientImpl.java |  2 +-
 .../registry/TestRemoteServiceRegistry.java        |  8 ---
 8 files changed, 50 insertions(+), 72 deletions(-)

diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
index 18115c1..947a386 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
@@ -30,7 +30,9 @@ import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
 import org.apache.servicecomb.serviceregistry.cache.CacheEndpoint;
 import org.apache.servicecomb.serviceregistry.cache.InstanceCache;
 import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager;
+import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManagerNew;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+import org.apache.servicecomb.serviceregistry.consumer.AppManager;
 import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,7 +42,7 @@ public class IpPortManager {
 
   private ServiceRegistryConfig serviceRegistryConfig;
 
-  private InstanceCacheManager instanceCacheManager;
+  InstanceCacheManager instanceCacheManager;
 
   private String defaultTransport = "rest";
 
@@ -60,9 +62,9 @@ public class IpPortManager {
     return maxRetryTimes;
   }
 
-  public IpPortManager(ServiceRegistryConfig serviceRegistryConfig, InstanceCacheManager instanceCacheManager) {
+  public IpPortManager(ServiceRegistryConfig serviceRegistryConfig) {
     this.serviceRegistryConfig = serviceRegistryConfig;
-    this.instanceCacheManager = instanceCacheManager;
+    this.instanceCacheManager = new InstanceCacheManagerNew(new AppManager());
 
     defaultTransport = serviceRegistryConfig.getTransport();
     defaultIpPort = serviceRegistryConfig.getIpPort();
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index 5ab309d..1201907 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -61,6 +61,8 @@ import org.apache.servicecomb.serviceregistry.client.ClientException;
 import org.apache.servicecomb.serviceregistry.client.IpPortManager;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
+import org.apache.servicecomb.serviceregistry.task.HeartbeatResult;
+import org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceHeartbeatTask;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -68,6 +70,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+import com.google.common.eventbus.Subscribe;
 
 import io.netty.handler.codec.http.HttpStatusClass;
 import io.vertx.core.Handler;
@@ -93,8 +96,8 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
 
   private WebsocketClientUtil websocketClientUtil;
 
-  public ServiceRegistryClientImpl(IpPortManager ipPortManager, ServiceRegistryConfig serviceRegistryConfig) {
-    this.ipPortManager = ipPortManager;
+  public ServiceRegistryClientImpl(ServiceRegistryConfig serviceRegistryConfig) {
+    this.ipPortManager = new IpPortManager(serviceRegistryConfig);
     this.restClientUtil = new RestClientUtil(serviceRegistryConfig);
     this.websocketClientUtil = new WebsocketClientUtil(serviceRegistryConfig);
   }
@@ -943,4 +946,11 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
     }
     return false;
   }
+
+  @Subscribe
+  public void onMicroserviceHeartbeatTask(MicroserviceInstanceHeartbeatTask event) {
+    if (HeartbeatResult.SUCCESS.equals(event.getHeartbeatResult())) {
+      ipPortManager.initAutoDiscovery();
+    }
+  }
 }
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 214968a..a8c4bc3 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
@@ -39,7 +39,6 @@ import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
-import org.apache.servicecomb.serviceregistry.client.IpPortManager;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
@@ -77,8 +76,6 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry {
 
   protected Microservice microservice;
 
-  protected IpPortManager ipPortManager;
-
   protected ServiceRegistryClient srClient;
 
   protected ServiceRegistryConfig serviceRegistryConfig;
@@ -102,9 +99,9 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry {
 
   @Override
   public void init() {
-    ipPortManager = new IpPortManager(serviceRegistryConfig, RegistryUtils.getInstanceCacheManager());
     if (srClient == null) {
       srClient = createServiceRegistryClient();
+      eventBus.register(srClient);
     }
 
     createServiceCenterTask();
@@ -144,10 +141,6 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry {
     this.srClient = serviceRegistryClient;
   }
 
-  public IpPortManager getIpPortManager() {
-    return ipPortManager;
-  }
-
   @Override
   public String getAppId() {
     return microservice.getAppId();
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
index 2002177..a53e4ac 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
@@ -27,13 +27,10 @@ import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
 import org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition;
-import org.apache.servicecomb.serviceregistry.task.HeartbeatResult;
-import org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceHeartbeatTask;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.Subscribe;
 
 public class RemoteServiceRegistry extends AbstractServiceRegistry {
   private static final Logger LOGGER = LoggerFactory.getLogger(RemoteServiceRegistry.class);
@@ -71,7 +68,7 @@ public class RemoteServiceRegistry extends AbstractServiceRegistry {
 
   @Override
   protected ServiceRegistryClient createServiceRegistryClient() {
-    return new ServiceRegistryClientImpl(ipPortManager, serviceRegistryConfig);
+    return new ServiceRegistryClientImpl(serviceRegistryConfig);
   }
 
   @Override
@@ -96,13 +93,6 @@ public class RemoteServiceRegistry extends AbstractServiceRegistry {
     }
   }
 
-  @Subscribe
-  public void onMicroserviceHeartbeatTask(MicroserviceInstanceHeartbeatTask event) {
-    if (HeartbeatResult.SUCCESS.equals(event.getHeartbeatResult())) {
-      ipPortManager.initAutoDiscovery();
-    }
-  }
-
   public ScheduledThreadPoolExecutor getTaskPool() {
     return this.taskPool;
   }
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
index f3a747b..595673e 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
@@ -48,16 +48,12 @@ public class TestIpPortManager {
 
   AbstractServiceRegistry serviceRegistry;
 
-  IpPortManager manager;
-
   @Before
   public void setup() {
     ConfigUtil.createLocalConfig();
     serviceRegistry = (AbstractServiceRegistry) ServiceRegistryFactory.createLocal();
     serviceRegistry.setServiceRegistryClient(srClient);
     serviceRegistry.init();
-
-    manager = serviceRegistry.getIpPortManager();
   }
 
   @Test
@@ -79,7 +75,8 @@ public class TestIpPortManager {
       }
     };
 
-    IpPortManager manager = new IpPortManager(config, cacheManager);
+    IpPortManager manager = new IpPortManager(config);
+    manager.instanceCacheManager = cacheManager;
     IpPort address1 = manager.getAvailableAddress();
 
     if (address1.getPort() == 9980) {
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java
index 48ab807..bea5c61 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java
@@ -24,7 +24,6 @@ import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory;
-import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager;
 import org.apache.servicecomb.serviceregistry.client.Endpoints;
 import org.apache.servicecomb.serviceregistry.client.IpPortManager;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
@@ -90,36 +89,31 @@ public class TestClientHttp {
     MicroserviceFactory microserviceFactory = new MicroserviceFactory();
     Microservice microservice = microserviceFactory.create("app", "ms");
 
-    ServiceRegistryClientImpl oClient = new ServiceRegistryClientImpl(manager, ServiceRegistryConfig.INSTANCE);
+    ServiceRegistryClientImpl oClient = new ServiceRegistryClientImpl(ServiceRegistryConfig.INSTANCE);
     oClient.init();
     oClient.registerMicroservice(microservice);
     oClient.registerMicroserviceInstance(microservice.getInstance());
-    Assert.assertEquals(null, oClient.getMicroservice(microservice.getServiceId()));
-    Assert.assertEquals(null, oClient.getMicroserviceInstance("testConsumerID", "testproviderID"));
-    Assert.assertEquals(null,
-        oClient.findServiceInstance(microservice.getServiceId(),
-            microservice.getAppId(),
-            microservice.getServiceName(),
-            microservice.getVersion()));
-    Assert.assertEquals(null,
-        oClient.findServiceInstances(microservice.getServiceId(),
-            microservice.getAppId(),
-            microservice.getServiceName(),
-            microservice.getVersion(),
-            "0"));
-    Assert.assertEquals(null,
-        oClient.getMicroserviceId(microservice.getAppId(),
-            microservice.getServiceName(),
-            microservice.getVersion(),
-            microservice.getEnvironment()));
-    Assert.assertEquals(null,
-        oClient.heartbeat(microservice.getServiceId(),
-            microservice.getInstance().getInstanceId()));
+    Assert.assertNull(oClient.getMicroservice(microservice.getServiceId()));
+    Assert.assertNull(oClient.getMicroserviceInstance("testConsumerID", "testproviderID"));
+    Assert.assertNull(oClient.findServiceInstance(microservice.getServiceId(),
+        microservice.getAppId(),
+        microservice.getServiceName(),
+        microservice.getVersion()));
+    Assert.assertNull(oClient.findServiceInstances(microservice.getServiceId(),
+        microservice.getAppId(),
+        microservice.getServiceName(),
+        microservice.getVersion(),
+        "0"));
+    Assert.assertNull(oClient.getMicroserviceId(microservice.getAppId(),
+        microservice.getServiceName(),
+        microservice.getVersion(),
+        microservice.getEnvironment()));
+    Assert.assertNull(oClient.heartbeat(microservice.getServiceId(),
+        microservice.getInstance().getInstanceId()));
     oClient.watch("",
         Mockito.mock(AsyncResultCallback.class));
-    Assert.assertEquals(false,
-        oClient.unregisterMicroserviceInstance(microservice.getServiceId(),
-            microservice.getInstance().getInstanceId()));
+    Assert.assertFalse(oClient.unregisterMicroserviceInstance(microservice.getServiceId(),
+        microservice.getInstance().getInstanceId()));
   }
 
   @Test
@@ -133,12 +127,12 @@ public class TestClientHttp {
     Assert.assertEquals("//test", oContext.getUri());
     Assert.assertEquals(io.vertx.core.http.HttpMethod.POST, oContext.getMethod());
     Assert.assertEquals(8080, oContext.getIpPort().getPort());
-    Assert.assertEquals(null, oContext.getParams());
+    Assert.assertNull(oContext.getParams());
 
     RestResponse oResponse = new RestResponse(null, null);
     oResponse.setRequestContext(oContext);
     Assert.assertEquals(oContext, oResponse.getRequestContext());
-    Assert.assertEquals(null, oResponse.getResponse());
+    Assert.assertNull(oResponse.getResponse());
   }
 
   @Test
@@ -150,24 +144,24 @@ public class TestClientHttp {
     oParam.addHeader("testKey", "testValue");
     oParam.addQueryParam("testParam", "ValueParam");
     oParam.addQueryParam("testParam1", "ValueParam");
-    Assert.assertEquals(null, oParam.getCookies());
-    Assert.assertEquals(null, oParam.getBody());
+    Assert.assertNull(oParam.getCookies());
+    Assert.assertNull(oParam.getBody());
     Assert.assertNotEquals(null, oParam.getHeaders());
     Assert.assertNotEquals(null, oParam.getQueryParams());
     oParam.setQueryParams(null);
     Assert.assertEquals("", oParam.getQueryParams());
     oParam.setFormFields(null);
-    Assert.assertEquals(null, oParam.getFormFields());
+    Assert.assertNull(oParam.getFormFields());
     Endpoints oEndpoints = new Endpoints();
     oEndpoints.setInstances(null);
     oEndpoints.setVersion("1.0");
-    Assert.assertEquals(null, oEndpoints.getInstances());
+    Assert.assertNull(oEndpoints.getInstances());
     Assert.assertEquals("1.0", oEndpoints.getVersion());
   }
 
   @Test
-  public void testIpPortManager(@Mocked InstanceCacheManager instanceCacheManager) throws Exception {
-    IpPortManager oManager = new IpPortManager(ServiceRegistryConfig.INSTANCE, instanceCacheManager);
+  public void testIpPortManager() {
+    IpPortManager oManager = new IpPortManager(ServiceRegistryConfig.INSTANCE);
     IpPort oIPPort = oManager.getNextAvailableAddress(new IpPort("", 33));
     Assert.assertEquals(oIPPort.getHostOrIp(), oManager.getAvailableAddress().getHostOrIp());
   }
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
index 6e260d9..a63b7bc 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
@@ -78,7 +78,7 @@ public class TestServiceRegistryClientImpl {
 
   @Before
   public void setUp() throws Exception {
-    oClient = new ServiceRegistryClientImpl(ipPortManager, ServiceRegistryConfig.buildFromConfiguration());
+    oClient = new ServiceRegistryClientImpl(ServiceRegistryConfig.buildFromConfiguration());
 
     new MockUp<RegistryUtils>() {
       @Mock
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
index c035aae..b562c29 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java
@@ -78,18 +78,10 @@ public class TestRemoteServiceRegistry {
       {
         definition.getConfiguration();
         result = ConfigUtil.createLocalConfig();
-        config.getIpPort();
-        result = ipPortList;
-        config.getTransport();
-        result = "rest";
-        config.isRegistryAutoDiscovery();
-        result = true;
         config.getHeartbeatInterval();
         result = 30;
         config.getInstancePullInterval();
         result = 30;
-        config.isWatch();
-        result = false;
         config.getRegistryName();
         result = "TestRegistry";
         SPIServiceUtils.getOrLoadSortedService(ServiceRegistryTaskInitializer.class);