funky-eyes commented on code in PR #7492:
URL: https://github.com/apache/incubator-seata/pull/7492#discussion_r2220771338
##########
common/pom.xml:
##########
@@ -48,6 +48,10 @@
<artifactId>httpclient</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents.client5</groupId>
Review Comment:
Since we've upgraded the HttpClient version to 5, is it still necessary to
keep version 4?
##########
common/src/main/java/org/apache/seata/common/util/HttpClientUtil.java:
##########
@@ -110,6 +118,61 @@ public static CloseableHttpResponse doPost(
return null;
}
+ // post request for http2
+ public static CompletableFuture<SimpleHttpResponse> doPostHttp2(
+ String url, Map<String, String> params, Map<String, String>
headers, int timeout) throws IOException {
+ try (CloseableHttpAsyncClient http2Client = HttpAsyncClients.custom()
+ .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
+
.setDefaultRequestConfig(org.apache.hc.client5.http.config.RequestConfig.custom()
+ .setConnectTimeout(timeout, TimeUnit.MILLISECONDS)
+ .setResponseTimeout(timeout, TimeUnit.MILLISECONDS)
+ .setConnectionRequestTimeout(timeout,
TimeUnit.MILLISECONDS)
+ .build())
+ .build()) {
+ http2Client.start();
+
+ SimpleHttpRequest request = new SimpleHttpRequest("POST", url);
+ String contentType = "";
+ if (headers != null) {
+ headers.forEach(request::setHeader);
+ contentType = headers.get("Content-Type");
+ }
+ if (StringUtils.isNotBlank(contentType)) {
+ if
(ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType)) {
+ List<NameValuePair> nameValuePairs = new ArrayList<>();
+ params.forEach((k, v) -> nameValuePairs.add(new
BasicNameValuePair(k, v)));
+ String requestBody =
URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8);
+ request.setBody(requestBody,
org.apache.hc.core5.http.ContentType.APPLICATION_FORM_URLENCODED);
+ } else if
(ContentType.APPLICATION_JSON.getMimeType().equals(contentType)) {
+ String requestBody =
OBJECT_MAPPER.writeValueAsString(params);
+ request.setBody(requestBody,
org.apache.hc.core5.http.ContentType.APPLICATION_JSON);
+ }
+ }
+
+ CompletableFuture<SimpleHttpResponse> future = new
CompletableFuture<>();
+ http2Client.execute(request, new
FutureCallback<SimpleHttpResponse>() {
Review Comment:
In the Server-side streaming RPC mode, can it ensure that a single request
receives multiple responses? Is it necessary to define an AsyncResponseConsumer
to handle the streaming responses?
##########
common/src/main/java/org/apache/seata/common/util/HttpClientUtil.java:
##########
@@ -143,6 +206,56 @@ public static CloseableHttpResponse doPost(String url,
String body, Map<String,
return null;
}
+ // post request for http2
+ public static CompletableFuture<SimpleHttpResponse> doPostHttp2(
+ String url, String body, Map<String, String> headers, int timeout)
throws IOException {
+ try (CloseableHttpAsyncClient http2Client = HttpAsyncClients.custom()
+ .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
+
.setDefaultRequestConfig(org.apache.hc.client5.http.config.RequestConfig.custom()
+ .setConnectTimeout(timeout, TimeUnit.MILLISECONDS)
+ .setResponseTimeout(timeout, TimeUnit.MILLISECONDS)
+ .setConnectionRequestTimeout(timeout,
TimeUnit.MILLISECONDS)
+ .build())
+ .build()) {
+ http2Client.start();
+
+ String contentType = "";
+ SimpleHttpRequest request = new SimpleHttpRequest("POST", url);
+ if (headers != null) {
+ headers.forEach(request::setHeader);
+ contentType = headers.get("Content-Type");
+ }
+
+ if (StringUtils.isNotBlank(contentType)) {
+ if
(ContentType.APPLICATION_JSON.getMimeType().equals(contentType)) {
+ request.setBody(body,
org.apache.hc.core5.http.ContentType.APPLICATION_JSON);
+ }
+ }
+
+ CompletableFuture<SimpleHttpResponse> future = new
CompletableFuture<>();
+ http2Client.execute(request, new
FutureCallback<SimpleHttpResponse>() {
Review Comment:
ditto
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]