You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/07/18 12:35:21 UTC

[GitHub] lovepoem closed pull request #2092: fix #2088 Miss context path when export a rest service explicitly

lovepoem closed pull request #2092: fix #2088 Miss context path when export a rest service explicitly
URL: https://github.com/apache/incubator-dubbo/pull/2092
 
 
   

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/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java
index 6ed1029143..82883f4e3e 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java
@@ -57,6 +57,13 @@
 public class RestProtocol extends AbstractProxyProtocol {
 
     private static final int DEFAULT_PORT = 80;
+    private static final String DEFAULT_SERVER = "jetty";
+
+    private static final int HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE = 20;
+    private static final int HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL = 20;
+    private static final int HTTPCLIENT_KEEPALIVEDURATION = 30*1000;
+    private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS = 1000;
+    private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S = 30;
 
     private final Map<String, RestServer> servers = new ConcurrentHashMap<String, RestServer>();
 
@@ -86,13 +93,13 @@ public int getDefaultPort() {
         Class implClass = ServiceClassHolder.getInstance().popServiceClass();
         RestServer server = servers.get(addr);
         if (server == null) {
-            server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, "jetty"));
+            server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER));
             server.start(url);
             servers.put(addr, server);
         }
 
         String contextPath = getContextPath(url);
-        if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, "jetty"))) {
+        if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER))) {
             ServletContext servletContext = ServletManager.getInstance().getServletContext(ServletManager.EXTERNAL_SERVER_PORT);
             if (servletContext == null) {
                 throw new RpcException("No servlet context found. Since you are using server='servlet', " +
@@ -136,8 +143,8 @@ public void run() {
         // TODO more configs to add
         PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
         // 20 is the default maxTotal of current PoolingClientConnectionManager
-        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
-        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
+        connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL));
+        connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE));
 
         connectionMonitor.addConnectionManager(connectionManager);
         RequestConfig requestConfig = RequestConfig.custom()
@@ -159,12 +166,11 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                             HeaderElement he = it.nextElement();
                             String param = he.getName();
                             String value = he.getValue();
-                            if (value != null && param.equalsIgnoreCase("timeout")) {
+                            if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) {
                                 return Long.parseLong(value) * 1000;
                             }
                         }
-                        // TODO constant
-                        return 30 * 1000;
+                        return HTTPCLIENT_KEEPALIVEDURATION;
                     }
                 })
                 .setDefaultRequestConfig(requestConfig)
@@ -232,8 +238,8 @@ public void destroy() {
     }
 
     protected String getContextPath(URL url) {
-        int pos = url.getPath().lastIndexOf("/");
-        return pos > 0 ? url.getPath().substring(0, pos) : "";
+        String contextPath = url.getPath();
+        return contextPath.endsWith("/") ? contextPath.substring(0,contextPath.length()-1) : contextPath;
     }
 
     protected class ConnectionMonitor extends Thread {
@@ -249,11 +255,10 @@ public void run() {
             try {
                 while (!shutdown) {
                     synchronized (this) {
-                        wait(1000);
+                        wait(HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS);
                         for (PoolingHttpClientConnectionManager connectionManager : connectionManagers) {
                             connectionManager.closeExpiredConnections();
-                            // TODO constant
-                            connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
+                            connectionManager.closeIdleConnections(HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S, TimeUnit.SECONDS);
                         }
                     }
                 }
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java
index 2b004a6cb0..bd2d66a5c4 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protol/rest/RestProtocolTest.java
@@ -45,4 +45,22 @@ public void testRestProtocol() {
         invoker.destroy();
         exporter.unexport();
     }
+
+    @Test
+    public void testRestProtocolWithContextPath() {
+        ServiceClassHolder.getInstance().pushServiceClass(RestServiceImpl.class);
+        RestServiceImpl server = new RestServiceImpl();
+        Assert.assertFalse(server.isCalled());
+        URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0");
+        Exporter<RestService> exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url));
+
+        url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0");
+        Invoker<RestService> invoker = protocol.refer(RestService.class, url);
+        RestService client = proxyFactory.getProxy(invoker);
+        String result = client.sayHello("haha");
+        Assert.assertTrue(server.isCalled());
+        Assert.assertEquals("Hello, haha", result);
+        invoker.destroy();
+        exporter.unexport();
+    }
 }


 

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

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org