You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/12/18 06:43:11 UTC

[GitHub] liubao68 closed pull request #1036: [SCB-1079]allow consumer-id to be empty when query instance

liubao68 closed pull request #1036: [SCB-1079]allow consumer-id to be empty when query instance
URL: https://github.com/apache/servicecomb-java-chassis/pull/1036
 
 
   

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/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 000000000..c40dad16f
--- /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 3ee0fe818..e73033a1c 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 ServiceRegistryClientImpl(IpPortManager ipPortManager) {
         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 void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceIns
   @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 MicroserviceInstances findServiceInstances(String consumerId, String appI
     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 96f2daf3e..b58088d72 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 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.Mocked;
 
 public class TestClientHttp {
+  private Microservice microservice = new Microservice();
+
   @SuppressWarnings("unchecked")
   @Test
   public void testServiceRegistryClientImpl(@Mocked IpPortManager manager) {
@@ -49,7 +52,12 @@ public void testServiceRegistryClientImpl(@Mocked IpPortManager manager) {
         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 900f534d6..8e45cc2fc 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.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 @@
 
   private ServiceRegistryClientImpl oClient = null;
 
+  private Microservice microservice = new Microservice();
+
   @Before
   public void setUp() throws Exception {
     oClient = new ServiceRegistryClientImpl(ipPortManager);
@@ -82,6 +85,13 @@ void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler
       }
     };
 
+    new MockUp<RegistryUtils>() {
+      @Mock
+      Microservice getMicroservice() {
+        return microservice;
+      }
+    };
+
     new MockUp<CountDownLatch>() {
       @Mock
       public void await() {
@@ -444,17 +454,11 @@ void get(IpPort ipPort, String uri, RequestParam requestParam,
 
   @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+"));


 

----------------------------------------------------------------
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