From 09bf183cc4120577d57d18d7138757d4bc8ce0f9 Mon Sep 17 00:00:00 2001 From: crazyhzm Date: Tue, 12 Feb 2019 17:43:42 +0800 Subject: [PATCH 1/3] fix connectionMonitor in RestProtocol seems not work #3237 --- .../org/apache/dubbo/rpc/protocol/rest/RestProtocol.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 7f8bc79f401..b1f84501321 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 @@ -61,7 +61,7 @@ public class RestProtocol extends AbstractProxyProtocol { 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 HTTPCLIENT_KEEPALIVEDURATION = 30 * 1000; private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS = 1000; private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S = 30; @@ -70,7 +70,7 @@ public class RestProtocol extends AbstractProxyProtocol { private final RestServerFactory serverFactory = new RestServerFactory(); // TODO in the future maybe we can just use a single rest client and connection manager - private final List clients = Collections.synchronizedList(new LinkedList()); + private final List clients = Collections.synchronizedList(new LinkedList<>()); private volatile ConnectionMonitor connectionMonitor; @@ -147,6 +147,7 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE)); connectionMonitor.addConnectionManager(connectionManager); + connectionMonitor.run(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.CONNECT_TIMEOUT_KEY, Constants.DEFAULT_CONNECT_TIMEOUT)) .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)) @@ -239,7 +240,7 @@ public void destroy() { protected String getContextPath(URL url) { String contextPath = url.getPath(); - return contextPath.endsWith("/") ? contextPath.substring(0,contextPath.length()-1) : contextPath; + return contextPath.endsWith("/") ? contextPath.substring(0, contextPath.length() - 1) : contextPath; } protected class ConnectionMonitor extends Thread { From 0dfed8680b0da2128257e18f7e7555c93f569234 Mon Sep 17 00:00:00 2001 From: crazyhzm Date: Tue, 12 Feb 2019 20:13:25 +0800 Subject: [PATCH 2/3] modify --- .../java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b1f84501321..6de5fb14b48 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 @@ -147,7 +147,7 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE)); connectionMonitor.addConnectionManager(connectionManager); - connectionMonitor.run(); + connectionMonitor.start(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.CONNECT_TIMEOUT_KEY, Constants.DEFAULT_CONNECT_TIMEOUT)) .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)) From f3e646ddcfe53373ee01c609ef9c6a1886e34b05 Mon Sep 17 00:00:00 2001 From: crazyhzm Date: Tue, 12 Feb 2019 22:06:32 +0800 Subject: [PATCH 3/3] modify --- .../org/apache/dubbo/rpc/protocol/rest/RestProtocol.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 6de5fb14b48..4290ef20b71 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 @@ -136,9 +136,6 @@ public void run() { @Override protected T doRefer(Class serviceType, URL url) throws RpcException { - if (connectionMonitor == null) { - connectionMonitor = new ConnectionMonitor(); - } // TODO more configs to add PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); @@ -146,8 +143,11 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL)); connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE)); + if (connectionMonitor == null) { + connectionMonitor = new ConnectionMonitor(); + connectionMonitor.start(); + } connectionMonitor.addConnectionManager(connectionManager); - connectionMonitor.start(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.CONNECT_TIMEOUT_KEY, Constants.DEFAULT_CONNECT_TIMEOUT)) .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT))