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/04/20 03:30:22 UTC

[incubator-servicecomb-java-chassis] 01/02: SCB-474 add endpoint filter (only rest endpoints) for zuul integration

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 e701d92bb5a8be073467590c51831d4c1b32de64
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Wed Apr 18 17:55:42 2018 +0800

    SCB-474 add endpoint filter (only rest endpoints) for zuul integration
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../src/main/resources/microservice.yaml           |  3 ++
 .../demo/discovery/zuul/DiscoveryZuulProxyIT.java  | 17 ++++----
 samples/bmi/webapp/pom.xml                         |  5 +++
 .../spring-boot-starter-discovery/pom.xml          |  2 +-
 .../CseRibbonEndpointDiscoveryFilter.java          | 30 ++++++++++++++
 .../starter/discovery/ServiceCombServerList.java   | 16 ++++----
 .../discovery/TestServiceCombServerList.java       | 47 ++++++++++++++++++----
 7 files changed, 95 insertions(+), 25 deletions(-)

diff --git a/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/src/main/resources/microservice.yaml b/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/src/main/resources/microservice.yaml
index 95d89ae..1be593d 100644
--- a/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/src/main/resources/microservice.yaml
+++ b/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/src/main/resources/microservice.yaml
@@ -25,6 +25,9 @@ cse:
       address: http://127.0.0.1:30100
   rest:
     address: 0.0.0.0:8080
+  #add highway for test filter
+  highway:
+    address: 0.0.0.0:7070
   handler:
     chain:
       Provider:
diff --git a/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/java/org/apache/servicecomb/demo/discovery/zuul/DiscoveryZuulProxyIT.java b/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/java/org/apache/servicecomb/demo/discovery/zuul/DiscoveryZuulProxyIT.java
index 7de8287..ed3a963 100644
--- a/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/java/org/apache/servicecomb/demo/discovery/zuul/DiscoveryZuulProxyIT.java
+++ b/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/java/org/apache/servicecomb/demo/discovery/zuul/DiscoveryZuulProxyIT.java
@@ -40,13 +40,16 @@ public class DiscoveryZuulProxyIT {
   private TestRestTemplate restTemplate;
 
   @Test
-  public void getsRemoteServiceThroughGateway() throws Exception {
-    String response = restTemplate.getForObject(
-        "/gateway/greeting/sayhello/{name}",
-        String.class,
-        "Mike");
+  public void getsRemoteServiceThroughGateway() {
+    //loop three time to insure only rest endpoint get
+    for (int i = 0; i < 3; i++) {
+      String response = restTemplate.getForObject(
+          "/gateway/greeting/sayhello/{name}",
+          String.class,
+          "Mike");
 
-    assertThat(response).isEqualTo("hello Mike");
+      assertThat(response).isEqualTo("hello Mike");
+    }
   }
 
   @SpringBootApplication
@@ -55,7 +58,7 @@ public class DiscoveryZuulProxyIT {
   @EnableServiceComb
   static class DiscoveryZuulProxy {
 
-    public static void main(String[] args) throws Exception {
+    public static void main(String[] args) {
       SpringApplication.run(DiscoveryZuulProxy.class, args);
     }
   }
diff --git a/samples/bmi/webapp/pom.xml b/samples/bmi/webapp/pom.xml
index 4581ea3..23ae4fa 100644
--- a/samples/bmi/webapp/pom.xml
+++ b/samples/bmi/webapp/pom.xml
@@ -33,6 +33,11 @@
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter</artifactId>
     </dependency>
+    <!--add rest transport-->
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>transport-rest-vertx</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
       <artifactId>spring-boot-starter-servicecomb</artifactId>
diff --git a/spring-boot-starter/spring-boot-starter-discovery/pom.xml b/spring-boot-starter/spring-boot-starter-discovery/pom.xml
index 96d535a..8453465 100644
--- a/spring-boot-starter/spring-boot-starter-discovery/pom.xml
+++ b/spring-boot-starter/spring-boot-starter-discovery/pom.xml
@@ -73,7 +73,7 @@
 		</dependency>
 		<dependency>
 			<groupId>org.apache.servicecomb</groupId>
-			<artifactId>service-registry</artifactId>
+			<artifactId>java-chassis-core</artifactId>
 		</dependency>
 	</dependencies>
 </project>
diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/CseRibbonEndpointDiscoveryFilter.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/CseRibbonEndpointDiscoveryFilter.java
new file mode 100644
index 0000000..741dd0a
--- /dev/null
+++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/CseRibbonEndpointDiscoveryFilter.java
@@ -0,0 +1,30 @@
+/*
+ * 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.springboot.starter.discovery;
+
+import org.apache.servicecomb.core.filter.EndpointDiscoveryFilter;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+
+public class CseRibbonEndpointDiscoveryFilter extends EndpointDiscoveryFilter {
+  @Override
+  protected String findTransportName(DiscoveryContext context, DiscoveryTreeNode parent) {
+    //only need rest endpoints
+    return "rest";
+  }
+}
diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/ServiceCombServerList.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/ServiceCombServerList.java
index 4de81d5..6393dbc 100644
--- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/ServiceCombServerList.java
+++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/ServiceCombServerList.java
@@ -18,12 +18,11 @@ package org.apache.servicecomb.springboot.starter.discovery;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
+import org.apache.servicecomb.core.Endpoint;
 import org.apache.servicecomb.foundation.common.cache.VersionedCache;
 import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
-import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
@@ -39,6 +38,7 @@ public class ServiceCombServerList extends AbstractServerList<Server> {
   private String serviceId;
 
   public ServiceCombServerList() {
+    discoveryTree.addFilter(new CseRibbonEndpointDiscoveryFilter());
   }
 
   @Override
@@ -49,13 +49,11 @@ public class ServiceCombServerList extends AbstractServerList<Server> {
         RegistryUtils.getAppId(),
         serviceId,
         DefinitionConst.VERSION_RULE_ALL);
-    Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
-    List<Server> instances = new ArrayList<>(servers.size());
-    for (MicroserviceInstance s : servers.values()) {
-      for (String endpoint : s.getEndpoints()) {
-        URIEndpointObject uri = new URIEndpointObject(endpoint);
-        instances.add(new Server(uri.getHostOrIp(), uri.getPort()));
-      }
+    ArrayList<Endpoint> endpoints = serversVersionedCache.data();
+    List<Server> instances = new ArrayList<>(endpoints.size());
+    for (Endpoint endpoint : endpoints) {
+      URIEndpointObject uri = new URIEndpointObject(endpoint.getEndpoint());
+      instances.add(new Server(uri.getHostOrIp(), uri.getPort()));
     }
     return instances;
   }
diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/org/apache/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/org/apache/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java
index 6fc015c..5f76c4b 100644
--- a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/org/apache/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java
+++ b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/org/apache/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java
@@ -17,18 +17,21 @@
 package org.apache.servicecomb.springboot.starter.discovery;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
+import org.apache.servicecomb.core.Endpoint;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.Transport;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.junit.Assert;
 import org.junit.Test;
 
+import com.google.common.collect.Lists;
 import com.netflix.client.config.IClientConfig;
 import com.netflix.loadbalancer.Server;
 
@@ -42,15 +45,13 @@ public class TestServiceCombServerList {
       @Mocked RegistryUtils registryUtils,
       @Mocked DiscoveryTree discoveryTree,
       @Injectable DiscoveryTreeNode versionedCache) {
-    Map<String, MicroserviceInstance> servers = new HashMap<>();
     List<String> endpoints = new ArrayList<>();
     endpoints.add("rest://localhost:3333");
-    endpoints.add("rest://localhost:4444");
+    endpoints.add("highway://localhost:4444");
     MicroserviceInstance instance1 = new MicroserviceInstance();
     instance1.setServiceId("service1");
     instance1.setInstanceId("service1-instance1");
     instance1.setEndpoints(endpoints);
-    servers.put("service1-instance1", instance1);
 
     new Expectations() {
       {
@@ -62,15 +63,45 @@ public class TestServiceCombServerList {
         discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString);
         result = versionedCache;
         versionedCache.data();
-        result = servers;
+        result = Lists.newArrayList(new Endpoint(new Transport() {
+          @Override
+          public String getName() {
+            return null;
+          }
+
+          @Override
+          public boolean init() {
+            return false;
+          }
+
+          @Override
+          public Object parseAddress(String endpoint) {
+            return null;
+          }
+
+          @Override
+          public Endpoint getEndpoint() {
+            return null;
+          }
+
+          @Override
+          public Endpoint getPublishEndpoint() {
+            return null;
+          }
+
+          @Override
+          public void send(Invocation invocation, AsyncResponse asyncResp) {
+
+          }
+        }, "rest://localhost:3333"));
       }
     };
 
     ServiceCombServerList list = new ServiceCombServerList();
     list.initWithNiwsConfig(iClientConfig);
     List<Server> serverList = list.getInitialListOfServers();
-    Assert.assertEquals(2, serverList.size());
-    Assert.assertEquals(4444, serverList.get(1).getPort());
+    Assert.assertEquals(1, serverList.size());
+    Assert.assertEquals(3333, serverList.get(0).getPort());
     Assert.assertEquals(serverList.size(), list.getUpdatedListOfServers().size());
   }
 }

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