You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2020/07/29 17:49:34 UTC

[GitHub] [incubator-dolphinscheduler] yangyichao-mango commented on a change in pull request #3337: [Improvement][common] HttpClient singleton mode #3332

yangyichao-mango commented on a change in pull request #3337:
URL: https://github.com/apache/incubator-dolphinscheduler/pull/3337#discussion_r462246650



##########
File path: dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
##########
@@ -18,41 +18,116 @@
 
 import org.apache.dolphinscheduler.common.Constants;
 import org.apache.http.HttpEntity;
+import org.apache.http.client.config.AuthSchemes;
+import org.apache.http.client.config.CookieSpecs;
 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.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+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.DefaultHttpClient;
 import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
 import java.io.IOException;
-
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+import java.util.Arrays;
 /**
  * http utils
  */
 public class HttpUtils {
-	
-	
+
+
 	public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
 
+
+	private static CloseableHttpClient httpClient;
+
+	private HttpUtils() {
+
+	}
+
+	private static PoolingHttpClientConnectionManager cm;
+
+	private static SSLContext ctx = null;
+
+	private static SSLConnectionSocketFactory socketFactory;
+
+	private static RequestConfig requestConfig;
+
+	private static Registry<ConnectionSocketFactory> socketFactoryRegistry;
+
+	private static X509TrustManager xtm = new X509TrustManager() {
+		@Override
+		public void checkClientTrusted(X509Certificate[] chain, String authType) {
+		}
+
+		@Override
+		public void checkServerTrusted(X509Certificate[] chain, String authType) {
+		}
+
+		@Override
+		public X509Certificate[] getAcceptedIssuers() {
+			return null;
+		}
+	};
+
+	static {
+		try {
+			ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
+			ctx.init(null, new TrustManager[] { xtm }, null);
+		} catch (NoSuchAlgorithmException e) {
+			logger.error("SSLContext init with NoSuchAlgorithmException", e);
+		} catch (KeyManagementException e) {
+			logger.error("SSLContext init with KeyManagementException", e);
+		}
+		socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
+		/** set timeout、request time、socket timeout */
+		requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES)
+				.setExpectContinueEnabled(Boolean.TRUE)
+				.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
+				.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
+				.setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT).setSocketTimeout(Constants.SOCKET_TIMEOUT)
+				.setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT).setRedirectsEnabled(true)
+				.build();
+		socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+				.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
+		cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
+		cm.setDefaultMaxPerRoute(60);
+		cm.setMaxTotal(100);
+
+	}
+
+
+	public static synchronized CloseableHttpClient getHttpClient() {

Review comment:
       public static synchronized CloseableHttpClient getHttpClient() {
   
   This singleton mode will cause bad performance.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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