Skip to content

Commit 813fed5

Browse files
kezhenxu94lixiaojiee
authored andcommitted
[Enhancement]: RestProtocol (#3480)
1 parent 2b12c16 commit 813fed5

File tree

1 file changed

+20
-30
lines changed
  • dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest

1 file changed

+20
-30
lines changed

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@
2828

2929
import org.apache.http.HeaderElement;
3030
import org.apache.http.HeaderElementIterator;
31-
import org.apache.http.HttpResponse;
3231
import org.apache.http.client.config.RequestConfig;
3332
import org.apache.http.config.SocketConfig;
34-
import org.apache.http.conn.ConnectionKeepAliveStrategy;
3533
import org.apache.http.impl.client.CloseableHttpClient;
3634
import org.apache.http.impl.client.HttpClientBuilder;
3735
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
3836
import org.apache.http.message.BasicHeaderElementIterator;
3937
import org.apache.http.protocol.HTTP;
40-
import org.apache.http.protocol.HttpContext;
4138
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
4239
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
4340
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
@@ -65,7 +62,7 @@ public class RestProtocol extends AbstractProxyProtocol {
6562
private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS = 1000;
6663
private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S = 30;
6764

68-
private final Map<String, RestServer> servers = new ConcurrentHashMap<String, RestServer>();
65+
private final Map<String, RestServer> servers = new ConcurrentHashMap<>();
6966

7067
private final RestServerFactory serverFactory = new RestServerFactory();
7168

@@ -91,12 +88,11 @@ public int getDefaultPort() {
9188
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
9289
String addr = getAddr(url);
9390
Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInstance().getClass();
94-
RestServer server = servers.get(addr);
95-
if (server == null) {
96-
server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER));
97-
server.start(url);
98-
servers.put(addr, server);
99-
}
91+
RestServer server = servers.computeIfAbsent(addr, restServer -> {
92+
RestServer s = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER));
93+
s.start(url);
94+
return s;
95+
});
10096

10197
String contextPath = getContextPath(url);
10298
if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER))) {
@@ -124,13 +120,10 @@ protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcExcept
124120
server.deploy(resourceDef, impl, contextPath);
125121

126122
final RestServer s = server;
127-
return new Runnable() {
128-
@Override
129-
public void run() {
130-
// TODO due to dubbo's current architecture,
131-
// it will be called from registry protocol in the shutdown process and won't appear in logs
132-
s.undeploy(resourceDef);
133-
}
123+
return () -> {
124+
// TODO due to dubbo's current architecture,
125+
// it will be called from registry protocol in the shutdown process and won't appear in logs
126+
s.undeploy(resourceDef);
134127
};
135128
}
136129

@@ -159,20 +152,17 @@ protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
159152
.build();
160153

161154
CloseableHttpClient httpClient = HttpClientBuilder.create()
162-
.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
163-
@Override
164-
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
165-
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
166-
while (it.hasNext()) {
167-
HeaderElement he = it.nextElement();
168-
String param = he.getName();
169-
String value = he.getValue();
170-
if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) {
171-
return Long.parseLong(value) * 1000;
172-
}
155+
.setKeepAliveStrategy((response, context) -> {
156+
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
157+
while (it.hasNext()) {
158+
HeaderElement he = it.nextElement();
159+
String param = he.getName();
160+
String value = he.getValue();
161+
if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) {
162+
return Long.parseLong(value) * 1000;
173163
}
174-
return HTTPCLIENT_KEEPALIVEDURATION;
175164
}
165+
return HTTPCLIENT_KEEPALIVEDURATION;
176166
})
177167
.setDefaultRequestConfig(requestConfig)
178168
.setDefaultSocketConfig(socketConfig)
@@ -245,7 +235,7 @@ protected String getContextPath(URL url) {
245235

246236
protected class ConnectionMonitor extends Thread {
247237
private volatile boolean shutdown;
248-
private final List<PoolingHttpClientConnectionManager> connectionManagers = Collections.synchronizedList(new LinkedList<PoolingHttpClientConnectionManager>());
238+
private final List<PoolingHttpClientConnectionManager> connectionManagers = Collections.synchronizedList(new LinkedList<>());
249239

250240
public void addConnectionManager(PoolingHttpClientConnectionManager connectionManager) {
251241
connectionManagers.add(connectionManager);

0 commit comments

Comments
 (0)