-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync 2.x to branch 2.0.0-test (#6053)
- Loading branch information
Showing
8 changed files
with
209 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
common/src/main/java/io/seata/common/util/HttpClientUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.common.util; | ||
|
||
|
||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.ClientProtocolException; | ||
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.methods.HttpPost; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.apache.http.client.utils.URLEncodedUtils; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.message.BasicNameValuePair; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* @author funkye | ||
*/ | ||
public class HttpClientUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class); | ||
|
||
private static final Map<Integer/*timeout*/, CloseableHttpClient> HTTP_CLIENT_MAP = new ConcurrentHashMap<>(); | ||
|
||
private static final PoolingHttpClientConnectionManager POOLING_HTTP_CLIENT_CONNECTION_MANAGER = | ||
new PoolingHttpClientConnectionManager(); | ||
|
||
static { | ||
POOLING_HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(10); | ||
POOLING_HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(10); | ||
Runtime.getRuntime().addShutdownHook(new Thread(() -> HTTP_CLIENT_MAP.values().parallelStream().forEach(client -> { | ||
try { | ||
client.close(); | ||
} catch (IOException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
}))); | ||
} | ||
|
||
// post request | ||
public static CloseableHttpResponse doPost(String url, Map<String, String> params, Map<String, String> header, | ||
int timeout) throws IOException { | ||
try { | ||
URIBuilder builder = new URIBuilder(url); | ||
URI uri = builder.build(); | ||
HttpPost httpPost = new HttpPost(uri); | ||
if (header != null) { | ||
header.forEach(httpPost::addHeader); | ||
} | ||
List<NameValuePair> nameValuePairs = new ArrayList<>(); | ||
params.forEach((k, v) -> { | ||
nameValuePairs.add(new BasicNameValuePair(k, v)); | ||
}); | ||
String requestBody = URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8); | ||
|
||
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_FORM_URLENCODED); | ||
httpPost.setEntity(stringEntity); | ||
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); | ||
CloseableHttpClient client = HTTP_CLIENT_MAP.computeIfAbsent(timeout, | ||
k -> HttpClients.custom().setConnectionManager(POOLING_HTTP_CLIENT_CONNECTION_MANAGER) | ||
.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout) | ||
.setSocketTimeout(timeout).setConnectTimeout(timeout).build()) | ||
.build()); | ||
return client.execute(httpPost); | ||
} catch (URISyntaxException | ClientProtocolException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
return null; | ||
} | ||
|
||
// get request | ||
public static CloseableHttpResponse doGet(String url, Map<String, String> param, Map<String, String> header, | ||
int timeout) throws IOException { | ||
try { | ||
URIBuilder builder = new URIBuilder(url); | ||
if (param != null) { | ||
for (String key : param.keySet()) { | ||
builder.addParameter(key, param.get(key)); | ||
} | ||
} | ||
URI uri = builder.build(); | ||
HttpGet httpGet = new HttpGet(uri); | ||
if (header != null) { | ||
header.forEach(httpGet::addHeader); | ||
} | ||
CloseableHttpClient client = HTTP_CLIENT_MAP.computeIfAbsent(timeout, | ||
k -> HttpClients.custom().setConnectionManager(POOLING_HTTP_CLIENT_CONNECTION_MANAGER) | ||
.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout) | ||
.setSocketTimeout(timeout).setConnectTimeout(timeout).build()) | ||
.build()); | ||
return client.execute(httpGet); | ||
} catch (URISyntaxException | ClientProtocolException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.