Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
liyuanzhong001
DolphinScheduler
提交
e026d303
DolphinScheduler
项目概览
liyuanzhong001
/
DolphinScheduler
与 Fork 源项目一致
Fork自
apache / DolphinScheduler
通知
11
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
DolphinScheduler
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e026d303
编写于
7月 29, 2020
作者:
F
felix.wang
提交者:
GitHub
7月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize the httpclient (#3337)
上级
e2a11703
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
94 addition
and
17 deletion
+94
-17
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
...a/org/apache/dolphinscheduler/common/utils/HttpUtils.java
+86
-17
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java
...g/apache/dolphinscheduler/common/utils/HttpUtilsTest.java
+8
-0
未找到文件。
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java
浏览文件 @
e026d303
...
...
@@ -18,41 +18,116 @@ package org.apache.dolphinscheduler.common.utils;
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
()
{
if
(
null
==
httpClient
)
{
httpClient
=
HttpClients
.
custom
().
setConnectionManager
(
cm
).
setDefaultRequestConfig
(
requestConfig
).
build
();
;
}
return
httpClient
;
}
/**
* get http request content
* @param url url
* @return http get request response content
*/
public
static
String
get
(
String
url
){
CloseableHttpClient
httpclient
=
Http
Clients
.
createDefaul
t
();
CloseableHttpClient
httpclient
=
Http
Utils
.
getHttpClien
t
();
HttpGet
httpget
=
new
HttpGet
(
url
);
/** set timeout、request time、socket timeout */
RequestConfig
requestConfig
=
RequestConfig
.
custom
().
setConnectTimeout
(
Constants
.
HTTP_CONNECT_TIMEOUT
)
.
setConnectionRequestTimeout
(
Constants
.
HTTP_CONNECTION_REQUEST_TIMEOUT
)
.
setSocketTimeout
(
Constants
.
SOCKET_TIMEOUT
)
.
setRedirectsEnabled
(
true
)
.
build
();
httpget
.
setConfig
(
requestConfig
);
String
responseContent
=
null
;
CloseableHttpResponse
response
=
null
;
...
...
@@ -85,12 +160,6 @@ public class HttpUtils {
httpget
.
releaseConnection
();
httpget
.
abort
();
}
try
{
httpclient
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
e
.
getMessage
(),
e
);
}
}
return
responseContent
;
}
...
...
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java
浏览文件 @
e026d303
...
...
@@ -17,6 +17,7 @@
package
org.apache.dolphinscheduler.common.utils
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.slf4j.Logger
;
...
...
@@ -42,4 +43,11 @@ public class HttpUtilsTest {
result
=
HttpUtils
.
get
(
"https://123.333.111.33/ccc"
);
Assert
.
assertNull
(
result
);
}
@Test
public
void
testGetHttpClient
()
{
CloseableHttpClient
httpClient1
=
HttpUtils
.
getHttpClient
();
CloseableHttpClient
httpClient2
=
HttpUtils
.
getHttpClient
();
Assert
.
assertEquals
(
httpClient1
,
httpClient2
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录