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/12/18 06:43:14 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1079]allow consumer-id to be empty when query instance

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 8b7bc87  [SCB-1079]allow consumer-id to be empty when query instance
8b7bc87 is described below

commit 8b7bc8745ff8dccb48de74425e5518a31ba9c3ea
Author: liubao <ba...@huawei.com>
AuthorDate: Thu Dec 13 12:04:18 2018 +0800

    [SCB-1079]allow consumer-id to be empty when query instance
---
 .../servicecomb/it/edge/PreLoadBootListener.java   | 46 ++++++++++++++++++++++
 .../client/http/ServiceRegistryClientImpl.java     | 20 ++++------
 .../client/http/TestClientHttp.java                | 10 ++++-
 .../client/http/TestServiceRegistryClientImpl.java | 18 +++++----
 4 files changed, 74 insertions(+), 20 deletions(-)

diff --git a/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/PreLoadBootListener.java b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/PreLoadBootListener.java
new file mode 100644
index 0000000..c40dad1
--- /dev/null
+++ b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/PreLoadBootListener.java
@@ -0,0 +1,46 @@
+/*
+ * 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.it.edge;
+
+import org.apache.servicecomb.core.BootListener;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Component
+public class PreLoadBootListener implements BootListener {
+  private static final Logger LOGGER = LoggerFactory.getLogger(PreLoadBootListener.class);
+
+  @Override
+  public int getOrder() {
+    return 0;
+  }
+
+  @Override
+  public void onBootEvent(BootEvent bootEvent) {
+    if (bootEvent.getEventType() == EventType.BEFORE_REGISTRY) {
+      MicroserviceVersionRule rule = RegistryUtils.getServiceRegistry().getAppManager()
+          .getOrCreateMicroserviceVersionRule(RegistryUtils.getAppId(), "it-producer", "0+");
+      if (rule.getInstances().size() == 0) {
+        LOGGER.warn("Prefetch not successful, maybe the provider not started.");
+      }
+    }
+  }
+}
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 3ee0fe8..e73033a 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
@@ -90,7 +90,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
         public Map<String, String> load(String key) {
           Holder<List<GetSchemaResponse>> result = getSchemas(key, true, true);
           Map<String, String> schemas = new HashMap<>();
-          if (result.getStatusCode() == Status.OK.getStatusCode() ) {
+          if (result.getStatusCode() == Status.OK.getStatusCode()) {
             result.value.stream().forEach(r -> schemas.put(r.getSchemaId(), r.getSchema()));
           }
           return schemas;
@@ -699,15 +699,6 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
   @Override
   public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName,
       String versionRule, String revision) {
-    // must register self first, and then invoke findServiceInstances
-    if (consumerId == null) {
-      LOGGER.error("find microservice instance {}/{}/{} failed, not registered to serviceCenter.",
-          appId,
-          serviceName,
-          versionRule);
-      return null;
-    }
-
     MicroserviceInstances microserviceInstances = new MicroserviceInstances();
     IpPort ipPort = ipPortManager.getAvailableAddress();
 
@@ -716,8 +707,13 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
     RequestParam requestParam = new RequestParam().addQueryParam("appId", appId)
         .addQueryParam("serviceName", serviceName)
         .addQueryParam("global", "true")
-        .addQueryParam("version", versionRule)
-        .addHeader("X-ConsumerId", consumerId);
+        .addQueryParam("version", versionRule);
+    if (RegistryUtils.getMicroservice().getEnvironment() != null) {
+      requestParam.addQueryParam("env", RegistryUtils.getMicroservice().getEnvironment());
+    }
+    if (consumerId != null) {
+      requestParam.addHeader("X-ConsumerId", consumerId);
+    }
     if (revision != null) {
       requestParam.addQueryParam("rev", revision);
     }
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 96f2daf..b58088d 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
@@ -21,6 +21,7 @@ import java.util.concurrent.CountDownLatch;
 
 import org.apache.servicecomb.foundation.common.net.IpPort;
 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;
@@ -39,6 +40,8 @@ import mockit.MockUp;
 import mockit.Mocked;
 
 public class TestClientHttp {
+  private Microservice microservice = new Microservice();
+
   @SuppressWarnings("unchecked")
   @Test
   public void testServiceRegistryClientImpl(@Mocked IpPortManager manager) {
@@ -49,7 +52,12 @@ public class TestClientHttp {
         result = ipPort;
       }
     };
-
+    new MockUp<RegistryUtils>() {
+      @Mock
+      Microservice getMicroservice() {
+        return microservice;
+      }
+    };
     new MockUp<CountDownLatch>() {
       @Mock
       public void await() throws InterruptedException {
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 900f534..8e45cc2 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
@@ -32,6 +32,7 @@ import org.apache.log4j.Appender;
 import org.apache.log4j.Logger;
 import org.apache.log4j.spi.LoggingEvent;
 import org.apache.servicecomb.foundation.common.net.IpPort;
+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.api.registry.ServiceCenterConfig;
@@ -72,6 +73,8 @@ public class TestServiceRegistryClientImpl {
 
   private ServiceRegistryClientImpl oClient = null;
 
+  private Microservice microservice = new Microservice();
+
   @Before
   public void setUp() throws Exception {
     oClient = new ServiceRegistryClientImpl(ipPortManager);
@@ -82,6 +85,13 @@ public class TestServiceRegistryClientImpl {
       }
     };
 
+    new MockUp<RegistryUtils>() {
+      @Mock
+      Microservice getMicroservice() {
+        return microservice;
+      }
+    };
+
     new MockUp<CountDownLatch>() {
       @Mock
       public void await() {
@@ -444,17 +454,11 @@ public class TestServiceRegistryClientImpl {
 
   @Test
   public void findServiceInstance_consumerId_null() {
-    new MockUp<IpPortManager>(ipPortManager) {
-      @Mock
-      IpPort getAvailableAddress() {
-        throw new Error("must not invoke this.");
-      }
-    };
     new MockUp<RestUtils>() {
       @Mock
       void get(IpPort ipPort, String uri, RequestParam requestParam,
           Handler<RestResponse> responseHandler) {
-        Assert.assertEquals("global=true", requestParam.getQueryParams());
+        Assert.assertEquals("appId=appId&global=true&serviceName=serviceName&version=1.0.0%2B", requestParam.getQueryParams());
       }
     };
     Assert.assertNull(oClient.findServiceInstance(null, "appId", "serviceName", "1.0.0+"));