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();
+ }
+}