From 82826fa8865197998080625747a9333943915ee8 Mon Sep 17 00:00:00 2001 From: polarbear567 <269739606@qq.com> Date: Wed, 16 Dec 2020 09:34:40 +0800 Subject: [PATCH] Support both HTTP and HTTPS protocol heart beating to dashboard (#1896) Apply to both simple-http and netty-http modules. --- .../transport/config/TransportConfig.java | 25 +++++--- .../sentinel/transport/endpoint/Endpoint.java | 49 ++++++++++++++++ .../sentinel/transport/endpoint/Protocol.java | 19 ++++++ .../sentinel/transport/ssl/SslFactory.java | 58 +++++++++++++++++++ .../transport/config/TransportConfigTest.java | 50 ++++++++++------ .../heartbeat/HttpHeartbeatSender.java | 19 +++--- .../heartbeat/client/HttpClientsFactory.java | 23 ++++++++ .../heartbeat/SimpleHttpHeartbeatSender.java | 19 +++--- .../heartbeat/client/SimpleHttpClient.java | 11 ++-- .../heartbeat/client/SimpleHttpRequest.java | 17 +++--- .../heartbeat/client/SocketFactory.java | 23 ++++++++ 11 files changed, 256 insertions(+), 57 deletions(-) create mode 100644 sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Endpoint.java create mode 100644 sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Protocol.java create mode 100644 sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/ssl/SslFactory.java create mode 100644 sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/HttpClientsFactory.java create mode 100644 sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SocketFactory.java diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java index 3c0f89d8..2294aebf 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java @@ -19,7 +19,8 @@ import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.HostNameUtil; import com.alibaba.csp.sentinel.util.StringUtil; -import com.alibaba.csp.sentinel.util.function.Tuple2; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; +import com.alibaba.csp.sentinel.transport.endpoint.Protocol; import java.util.ArrayList; import java.util.List; @@ -27,6 +28,7 @@ import java.util.List; /** * @author Carpenter Lee * @author Jason Joo + * @author Leo Li */ public class TransportConfig { @@ -56,20 +58,20 @@ public class TransportConfig { } /** - * Get a list of (ip/domain, port) indicating Sentinel Dashboard's address.
- * NOTE: only support HTTP protocol + * Get a list of Endpoint(protocol, ip/domain, port) indicating Sentinel Dashboard's address.
+ * NOTE: only support HTTP and HTTPS protocol * - * @return list of (ip/domain, port) pair.
+ * @return list of Endpoint(protocol, ip/domain, port).
* May not be null.
* An empty list returned when not configured. */ - public static List> getConsoleServerList() { + public static List getConsoleServerList() { String config = SentinelConfig.getConfig(CONSOLE_SERVER); - List> list = new ArrayList>(); + List list = new ArrayList(); if (StringUtil.isBlank(config)) { return list; } - + int pos = -1; int cur = 0; while (true) { @@ -92,11 +94,16 @@ public class TransportConfig { continue; } ipPortStr = ipPortStr.trim(); + int port = 80; + Protocol protocol = Protocol.HTTP; if (ipPortStr.startsWith("http://")) { ipPortStr = ipPortStr.substring(7); + } else if (ipPortStr.startsWith("https://")) { + ipPortStr = ipPortStr.substring(8); + port = 443; + protocol = Protocol.HTTPS; } int index = ipPortStr.indexOf(":"); - int port = 80; if (index == 0) { // skip continue; @@ -115,7 +122,7 @@ public class TransportConfig { } host = ipPortStr.substring(0, index); } - list.add(Tuple2.of(host, port)); + list.add(new Endpoint(protocol, host, port)); } return list; } diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Endpoint.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Endpoint.java new file mode 100644 index 00000000..d1181e58 --- /dev/null +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Endpoint.java @@ -0,0 +1,49 @@ +package com.alibaba.csp.sentinel.transport.endpoint; + +import java.net.InetSocketAddress; + +/** + * @author Leo Li + */ +public class Endpoint { + private Protocol protocol; + + private String host; + + private int port; + + public Endpoint(Protocol protocol, String host, int port) { + this.protocol = protocol; + this.host = host; + this.port = port; + } + + public Protocol getProtocol() { + return protocol; + } + + public void setProtocol(Protocol protocol) { + this.protocol = protocol; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + @Override + public String toString() { + return "Endpoint{" + "protocol=" + protocol + ", host='" + host + ", port=" + port + '}'; + } +} diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Protocol.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Protocol.java new file mode 100644 index 00000000..5a635099 --- /dev/null +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/endpoint/Protocol.java @@ -0,0 +1,19 @@ +package com.alibaba.csp.sentinel.transport.endpoint; + +/** + * @author Leo Li + */ +public enum Protocol { + HTTP("http"), + HTTPS("https"); + + private String protocol; + + Protocol(String protocol) { + this.protocol = protocol; + } + + public String getProtocol() { + return protocol; + } +} diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/ssl/SslFactory.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/ssl/SslFactory.java new file mode 100644 index 00000000..d82b0da5 --- /dev/null +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/ssl/SslFactory.java @@ -0,0 +1,58 @@ +package com.alibaba.csp.sentinel.transport.ssl; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import com.alibaba.csp.sentinel.log.RecordLog; + +/** + * @author Leo Li + */ +public class SslFactory { + + private static class SslContextInstance { + private static final SSLContext SSL_CONTEXT = initSslContext(); + } + + private static SSLContext initSslContext() { + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("TLS"); + X509TrustManager x509TrustManager = new X509TrustManager() { + public boolean isServerTrusted(X509Certificate[] certs) { + return true; + } + + public boolean isClientTrusted(X509Certificate[] certs) { + return true; + } + + @Override + public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { + } + + @Override + public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + }; + + sslContext.init(null, new TrustManager[] { x509TrustManager }, null); + } catch (Exception e) { + RecordLog.error("get ssl socket factory error", e); + } + return sslContext; + } + + public static SSLContext getSslConnectionSocketFactory() { + return SslContextInstance.SSL_CONTEXT; + } +} diff --git a/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java index 94ef251f..a21e9a01 100644 --- a/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java +++ b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java @@ -17,7 +17,8 @@ package com.alibaba.csp.sentinel.transport.config; import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.util.StringUtil; -import com.alibaba.csp.sentinel.util.function.Tuple2; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; +import com.alibaba.csp.sentinel.transport.endpoint.Protocol; import org.junit.After; import org.junit.Before; @@ -90,7 +91,7 @@ public class TransportConfigTest { public void testGetConsoleServerList() { // empty SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, ""); - List> list = TransportConfig.getConsoleServerList(); + List list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(0, list.size()); @@ -99,38 +100,38 @@ public class TransportConfigTest { list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); - assertEquals("112.13.223.3", list.get(0).r1); - assertEquals(new Integer(80), list.get(0).r2); - + assertEquals("112.13.223.3", list.get(0).getHost()); + assertEquals(80, list.get(0).getPort()); + // single domain SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); - assertEquals("www.dashboard.org", list.get(0).r1); - assertEquals(new Integer(80), list.get(0).r2); + assertEquals("www.dashboard.org", list.get(0).getHost()); + assertEquals(80, list.get(0).getPort()); // single ip including port SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); - assertEquals("www.dashboard.org", list.get(0).r1); - assertEquals(new Integer(81), list.get(0).r2); + assertEquals("www.dashboard.org", list.get(0).getHost()); + assertEquals(81, list.get(0).getPort()); // mixed SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:81,112.13.223.3,112.13.223.4:8080,www.dashboard.org"); list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(4, list.size()); - assertEquals("www.dashboard.org", list.get(0).r1); - assertEquals(new Integer(81), list.get(0).r2); - assertEquals("112.13.223.3", list.get(1).r1); - assertEquals(new Integer(80), list.get(1).r2); - assertEquals("112.13.223.4", list.get(2).r1); - assertEquals(new Integer(8080), list.get(2).r2); - assertEquals("www.dashboard.org", list.get(3).r1); - assertEquals(new Integer(80), list.get(3).r2); + assertEquals("www.dashboard.org", list.get(0).getHost()); + assertEquals(81, list.get(0).getPort()); + assertEquals("112.13.223.3", list.get(1).getHost()); + assertEquals(80, list.get(1).getPort()); + assertEquals("112.13.223.4", list.get(2).getHost()); + assertEquals(8080, list.get(2).getPort()); + assertEquals("www.dashboard.org", list.get(3).getHost()); + assertEquals(80, list.get(3).getPort()); // malformed SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "www.dashboard.org:0"); @@ -162,7 +163,18 @@ public class TransportConfigTest { list = TransportConfig.getConsoleServerList(); assertNotNull(list); assertEquals(1, list.size()); - assertEquals("www.dashboard.org", list.get(0).r1); - assertEquals(new Integer(81), list.get(0).r2); + assertEquals("www.dashboard.org", list.get(0).getHost()); + assertEquals(81, list.get(0).getPort()); + + SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "https://www.dashboard.org,http://www.dashboard.org:8080,www.dashboard.org,www.dashboard.org:8080"); + list = TransportConfig.getConsoleServerList(); + assertNotNull(list); + assertEquals(4, list.size()); + assertEquals(Protocol.HTTPS, list.get(0).getProtocol()); + assertEquals(Protocol.HTTP, list.get(1).getProtocol()); + assertEquals(Protocol.HTTP, list.get(2).getProtocol()); + assertEquals(Protocol.HTTP, list.get(3).getProtocol()); + assertEquals(443, list.get(0).getPort()); + assertEquals(80, list.get(2).getPort()); } } diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index c684156e..7fe68379 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -21,24 +21,26 @@ import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.spi.SpiOrder; import com.alibaba.csp.sentinel.transport.HeartbeatSender; import com.alibaba.csp.sentinel.transport.config.TransportConfig; +import com.alibaba.csp.sentinel.transport.endpoint.Protocol; +import com.alibaba.csp.sentinel.transport.heartbeat.client.HttpClientsFactory; import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.HostNameUtil; import com.alibaba.csp.sentinel.util.PidUtil; import com.alibaba.csp.sentinel.util.StringUtil; -import com.alibaba.csp.sentinel.util.function.Tuple2; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; import java.util.List; /** * @author Eric Zhao * @author Carpenter Lee + * @author Leo Li */ @SpiOrder(SpiOrder.LOWEST_PRECEDENCE - 100) public class HttpHeartbeatSender implements HeartbeatSender { @@ -54,21 +56,24 @@ public class HttpHeartbeatSender implements HeartbeatSender { .setSocketTimeout(timeoutMs) .build(); + private final Protocol consoleProtocol; private final String consoleHost; private final int consolePort; public HttpHeartbeatSender() { - this.client = HttpClients.createDefault(); - List> dashboardList = TransportConfig.getConsoleServerList(); + List dashboardList = TransportConfig.getConsoleServerList(); if (dashboardList == null || dashboardList.isEmpty()) { RecordLog.info("[NettyHttpHeartbeatSender] No dashboard server available"); + consoleProtocol = Protocol.HTTP; consoleHost = null; consolePort = -1; } else { - consoleHost = dashboardList.get(0).r1; - consolePort = dashboardList.get(0).r2; + consoleProtocol = dashboardList.get(0).getProtocol(); + consoleHost = dashboardList.get(0).getHost(); + consolePort = dashboardList.get(0).getPort(); RecordLog.info("[NettyHttpHeartbeatSender] Dashboard address parsed: <{}:{}>", consoleHost, consolePort); } + this.client = HttpClientsFactory.getHttpClientsByProtocol(consoleProtocol); } @Override @@ -77,7 +82,7 @@ public class HttpHeartbeatSender implements HeartbeatSender { return false; } URIBuilder uriBuilder = new URIBuilder(); - uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort) + uriBuilder.setScheme(consoleProtocol.getProtocol()).setHost(consoleHost).setPort(consolePort) .setPath(TransportConfig.getHeartbeatApiPath()) .setParameter("app", AppNameUtil.getAppName()) .setParameter("app_type", String.valueOf(SentinelConfig.getAppType())) diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/HttpClientsFactory.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/HttpClientsFactory.java new file mode 100644 index 00000000..9ff26f59 --- /dev/null +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/HttpClientsFactory.java @@ -0,0 +1,23 @@ +package com.alibaba.csp.sentinel.transport.heartbeat.client; + +import com.alibaba.csp.sentinel.transport.endpoint.Protocol; +import com.alibaba.csp.sentinel.transport.ssl.SslFactory; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +/** + * @author Leo Li + */ +public class HttpClientsFactory { + + private static class SslConnectionSocketFactoryInstance { + private static final SSLConnectionSocketFactory SSL_CONNECTION_SOCKET_FACTORY = new SSLConnectionSocketFactory(SslFactory.getSslConnectionSocketFactory(), NoopHostnameVerifier.INSTANCE); + } + + public static CloseableHttpClient getHttpClientsByProtocol(Protocol protocol) { + return protocol == Protocol.HTTP ? HttpClients.createDefault() : HttpClients.custom(). + setSSLSocketFactory(SslConnectionSocketFactoryInstance.SSL_CONNECTION_SOCKET_FACTORY).build(); + } +} diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java index a8b82e9a..179d3856 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java @@ -21,9 +21,8 @@ import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpClient; import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest; import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse; -import com.alibaba.csp.sentinel.util.function.Tuple2; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; -import java.net.InetSocketAddress; import java.util.List; /** @@ -32,6 +31,7 @@ import java.util.List; * * @author Eric Zhao * @author Carpenter Lee + * @author Leo Li */ public class SimpleHttpHeartbeatSender implements HeartbeatSender { @@ -42,13 +42,13 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender { private final HeartbeatMessage heartBeat = new HeartbeatMessage(); private final SimpleHttpClient httpClient = new SimpleHttpClient(); - private final List> addressList; + private final List addressList; private int currentAddressIdx = 0; public SimpleHttpHeartbeatSender() { // Retrieve the list of default addresses. - List> newAddrs = TransportConfig.getConsoleServerList(); + List newAddrs = TransportConfig.getConsoleServerList(); if (newAddrs.isEmpty()) { RecordLog.warn("[SimpleHttpHeartbeatSender] Dashboard server address not configured or not available"); } else { @@ -63,24 +63,23 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender { RecordLog.info("[SimpleHttpHeartbeatSender] Command server port not initialized, won't send heartbeat"); return false; } - Tuple2 addrInfo = getAvailableAddress(); + Endpoint addrInfo = getAvailableAddress(); if (addrInfo == null) { return false; } - InetSocketAddress addr = new InetSocketAddress(addrInfo.r1, addrInfo.r2); - SimpleHttpRequest request = new SimpleHttpRequest(addr, TransportConfig.getHeartbeatApiPath()); + SimpleHttpRequest request = new SimpleHttpRequest(addrInfo, TransportConfig.getHeartbeatApiPath()); request.setParams(heartBeat.generateCurrentMessage()); try { SimpleHttpResponse response = httpClient.post(request); if (response.getStatusCode() == OK_STATUS) { return true; } else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) { - RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addrInfo + ", http status code: " + response.getStatusCode()); } } catch (Exception e) { - RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e); + RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addrInfo, e); } return false; } @@ -90,7 +89,7 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender { return DEFAULT_INTERVAL; } - private Tuple2 getAvailableAddress() { + private Endpoint getAvailableAddress() { if (addressList == null || addressList.isEmpty()) { return null; } diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java index 5ec54e9f..6e843cf4 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpClient.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Map.Entry; import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; /** *

@@ -47,6 +48,7 @@ import com.alibaba.csp.sentinel.log.RecordLog; *

* * @author leyou + * @author Leo Li */ public class SimpleHttpClient { @@ -61,7 +63,7 @@ public class SimpleHttpClient { if (request == null) { return null; } - return request(request.getSocketAddress(), + return request(request.getEndpoint(), RequestMethod.GET, request.getRequestPath(), request.getParams(), request.getCharset(), request.getSoTimeout()); } @@ -77,20 +79,21 @@ public class SimpleHttpClient { if (request == null) { return null; } - return request(request.getSocketAddress(), + return request(request.getEndpoint(), RequestMethod.POST, request.getRequestPath(), request.getParams(), request.getCharset(), request.getSoTimeout()); } - private SimpleHttpResponse request(InetSocketAddress socketAddress, + private SimpleHttpResponse request(Endpoint endpoint, RequestMethod type, String requestPath, Map paramsMap, Charset charset, int soTimeout) throws IOException { Socket socket = null; BufferedWriter writer; + InetSocketAddress socketAddress = new InetSocketAddress(endpoint.getHost(), endpoint.getPort()); try { - socket = new Socket(); + socket = SocketFactory.getSocket(endpoint.getProtocol()); socket.setSoTimeout(soTimeout); socket.connect(socketAddress, soTimeout); diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpRequest.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpRequest.java index a9d3abdf..fa7016f6 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpRequest.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpRequest.java @@ -15,38 +15,39 @@ */ package com.alibaba.csp.sentinel.transport.heartbeat.client; -import java.net.InetSocketAddress; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.csp.sentinel.transport.endpoint.Endpoint; /** * Simple HTTP request representation. * * @author leyou + * @author Leo Li */ public class SimpleHttpRequest { - private InetSocketAddress socketAddress; + private Endpoint endpoint; private String requestPath = ""; private int soTimeout = 3000; private Map params; private Charset charset = Charset.forName(SentinelConfig.charset()); - public SimpleHttpRequest(InetSocketAddress socketAddress, String requestPath) { - this.socketAddress = socketAddress; + public SimpleHttpRequest(Endpoint endpoint, String requestPath) { + this.endpoint = endpoint; this.requestPath = requestPath; } - public InetSocketAddress getSocketAddress() { - return socketAddress; + public Endpoint getEndpoint() { + return endpoint; } - public SimpleHttpRequest setSocketAddress(InetSocketAddress socketAddress) { - this.socketAddress = socketAddress; + public SimpleHttpRequest setEndpoint(Endpoint endpoint) { + this.endpoint = endpoint; return this; } diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SocketFactory.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SocketFactory.java new file mode 100644 index 00000000..5b5de264 --- /dev/null +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SocketFactory.java @@ -0,0 +1,23 @@ +package com.alibaba.csp.sentinel.transport.heartbeat.client; + +import java.io.IOException; +import java.net.Socket; + +import javax.net.ssl.SSLSocketFactory; + +import com.alibaba.csp.sentinel.transport.endpoint.Protocol; +import com.alibaba.csp.sentinel.transport.ssl.SslFactory; + +/** + * @author Leo Li + */ +public class SocketFactory { + + private static class SSLSocketFactoryInstance { + private static final SSLSocketFactory SSL_SOCKET_FACTORY = SslFactory.getSslConnectionSocketFactory().getSocketFactory(); + } + + public static Socket getSocket(Protocol protocol) throws IOException { + return protocol == Protocol.HTTP ? new Socket() : SSLSocketFactoryInstance.SSL_SOCKET_FACTORY.createSocket(); + } +}