liubao68 closed pull request #658: [SCB-474] add endpoint filter (only rest 
endpoints) for zuul integration
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/658
 
 
   

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/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 95d89ae36..1be593db4 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 7de82872f..ed3a9631a 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 @@
   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 void getsRemoteServiceThroughGateway() throws 
Exception {
   @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 4581ea358..23ae4fab2 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/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 000000000..65f72d7a3
--- /dev/null
+++ 
b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/CseRibbonEndpointDiscoveryFilter.java
@@ -0,0 +1,45 @@
+/*
+ * 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.foundation.common.net.URIEndpointObject;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import 
org.apache.servicecomb.serviceregistry.discovery.AbstractEndpointDiscoveryFilter;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+
+import com.netflix.loadbalancer.Server;
+
+public class CseRibbonEndpointDiscoveryFilter extends 
AbstractEndpointDiscoveryFilter {
+  @Override
+  protected String findTransportName(DiscoveryContext context, 
DiscoveryTreeNode parent) {
+    //only need rest endpoints
+    return "rest";
+  }
+
+  @Override
+  protected Object createEndpoint(String transportName, String endpoint, 
MicroserviceInstance instance) {
+    URIEndpointObject uri = new URIEndpointObject(endpoint);
+    return new Server(uri.getHostOrIp(), uri.getPort());
+  }
+
+  @Override
+  public int getOrder() {
+    return (int) Short.MAX_VALUE - 1;
+  }
+}
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 4de81d5b1..13f092bd0 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
@@ -16,14 +16,10 @@
  */
 package org.apache.servicecomb.springboot.starter.discovery;
 
-import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
 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 +35,7 @@
   private String serviceId;
 
   public ServiceCombServerList() {
+    discoveryTree.addFilter(new CseRibbonEndpointDiscoveryFilter());
   }
 
   @Override
@@ -49,15 +46,7 @@ public ServiceCombServerList() {
         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()));
-      }
-    }
-    return instances;
+    return serversVersionedCache.data();
   }
 
   @Override
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 6fc015cae..8201641d3 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,9 +17,7 @@
 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.serviceregistry.RegistryUtils;
 import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
@@ -29,6 +27,7 @@
 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 +41,13 @@ public void testServiceCombServerList(@Injectable 
IClientConfig iClientConfig,
       @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 +59,15 @@ public void testServiceCombServerList(@Injectable 
IClientConfig iClientConfig,
         discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, 
anyString);
         result = versionedCache;
         versionedCache.data();
-        result = servers;
+        result = Lists.newArrayList(new Server("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());
   }
 }


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to