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/10/23 08:40:58 UTC

[incubator-servicecomb-java-chassis] 02/03: [SCB-967] Support all transport

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 c798f554bb71cc92843dd429a4336f78bb429c07
Author: weichao666 <we...@huawei.com>
AuthorDate: Thu Oct 18 11:33:12 2018 +0800

    [SCB-967] Support all transport
---
 .../loadbalance/LoadbalanceHandler.java            | 15 ++++++++++----
 .../loadbalance/TestLoadBalanceHandler2.java       | 24 ++++++++++++++++++++--
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
index ba10f8f..d8ae0e1 100644
--- a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
+++ b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
@@ -17,6 +17,7 @@
 
 package org.apache.servicecomb.loadbalance;
 
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -66,7 +67,7 @@ import rx.Observable;
 public class LoadbalanceHandler implements Handler {
   public static final String CONTEXT_KEY_SERVER_LIST = "x-context-server-list";
 
-  public static final String SERVICECOMB_SERVER_ENDPOINT = "servicecomb-server-endpoint";
+  public static final String SERVICECOMB_SERVER_ENDPOINT = "scb-endpoint";
 
   // just a wrapper to make sure in retry mode to choose a different server.
   class RetryLoadBalancer implements ILoadBalancer {
@@ -177,12 +178,18 @@ public class LoadbalanceHandler implements Handler {
   public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
     String endpointUri = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT);
     if (endpointUri != null) {
-      boolean isRest = endpointUri.startsWith("rest://");
-      if (!isRest) {
+      String endpointRule = "[a-z]+://.+";
+      if (!endpointUri.matches(endpointRule)) {
         throw new InvocationException(Status.BAD_REQUEST,
             "the endpoint's format of the configuration is incorrect, e.g rest://127.0.0.1:8080");
       }
-      Transport transport = CseContext.getInstance().getTransportManager().findTransport("rest");
+      URI formatUri = new URI(endpointUri);
+      Transport transport = CseContext.getInstance().getTransportManager().findTransport(formatUri.getScheme());
+      if (transport == null) {
+        LOGGER.error("not deployed transport {}, ignore {}.", formatUri.getScheme(), endpointUri);
+        throw new InvocationException(Status.BAD_REQUEST,
+            "the endpoint's transport is not found.");
+      }
       Endpoint endpoint = new Endpoint(transport, endpointUri);
       invocation.setEndpoint(endpoint);
     }
diff --git a/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
index efd011e..51cb333 100644
--- a/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
+++ b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
@@ -254,12 +254,32 @@ public class TestLoadBalanceHandler2 {
     }
     Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
 
-    invocation.addLocalContext("servicecomb-server-endpoint", "rest://127.0.0.1:8080");
+    //success
+    invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
     try {
       handler.handle(invocation, asyncResp);
     } catch (Exception e) {
 
     }
-    Assert.assertEquals("rest://127.0.0.1:8080", invocation.getEndpoint().getEndpoint());
+    Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
+
+    //endpoint format is not correct
+    invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
+    try {
+      handler.handle(invocation, asyncResp);
+      Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
+    } catch (Exception e) {
+      Assert.assertTrue(e.getMessage()
+          .contains("the endpoint's format of the configuration is incorrect, e.g rest://127.0.0.1:8080"));
+    }
+
+    //transport is not find
+    invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
+    try {
+      handler.handle(invocation, asyncResp);
+      Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
+    } catch (Exception e) {
+      Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
+    }
   }
 }