优化httpclient
This commit is contained in:
@@ -77,11 +77,7 @@ public class HttpClusterRpcClient extends HttpRpcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<HttpResult<byte[]>> httpAsync(boolean produce, Serializable userid, HttpSimpleRequest req) {
|
private CompletableFuture<HttpResult<byte[]>> httpAsync(boolean produce, Serializable userid, HttpSimpleRequest req) {
|
||||||
if (isEmpty(req.getTraceid())) {
|
req.setTraceid(Traces.computeIfAbsent(req.getTraceid(), Traces.currentTraceid()));
|
||||||
req.setTraceid(Traces.currentTraceid());
|
|
||||||
} else {
|
|
||||||
Traces.computeIfAbsent(req.getTraceid());
|
|
||||||
}
|
|
||||||
final WorkThread workThread = WorkThread.currentWorkThread();
|
final WorkThread workThread = WorkThread.currentWorkThread();
|
||||||
String module = req.getRequestURI();
|
String module = req.getRequestURI();
|
||||||
module = module.substring(1); //去掉/
|
module = module.substring(1); //去掉/
|
||||||
@@ -169,7 +165,9 @@ public class HttpClusterRpcClient extends HttpRpcClient {
|
|||||||
if (httpSimpleClient != null) {
|
if (httpSimpleClient != null) {
|
||||||
return httpSimpleClient.postAsync(url, clientHeaders, clientBody);
|
return httpSimpleClient.postAsync(url, clientHeaders, clientBody);
|
||||||
}
|
}
|
||||||
java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder().uri(URI.create(url))
|
java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.header(Rest.REST_HEADER_TRACEID, req.getTraceid())
|
||||||
.timeout(Duration.ofMillis(10_000))
|
.timeout(Duration.ofMillis(10_000))
|
||||||
//存在sendHeader后不发送body数据的问题, java.net.http.HttpRequest的bug?
|
//存在sendHeader后不发送body数据的问题, java.net.http.HttpRequest的bug?
|
||||||
.method("POST", clientBody == null ? java.net.http.HttpRequest.BodyPublishers.noBody() : java.net.http.HttpRequest.BodyPublishers.ofByteArray(clientBody));
|
.method("POST", clientBody == null ? java.net.http.HttpRequest.BodyPublishers.noBody() : java.net.http.HttpRequest.BodyPublishers.ofByteArray(clientBody));
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ public class HttpSimpleClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> CompletableFuture<HttpResult<T>> sendAsync(String method, String url, Map<String, String> headers, byte[] body, Convert convert, Type valueType) {
|
public <T> CompletableFuture<HttpResult<T>> sendAsync(String method, String url, Map<String, String> headers, byte[] body, Convert convert, Type valueType) {
|
||||||
final String traceid = Traces.currentTraceid();
|
final String traceid = Traces.computeIfAbsent(Traces.currentTraceid());
|
||||||
final WorkThread workThread = WorkThread.currentWorkThread();
|
final WorkThread workThread = WorkThread.currentWorkThread();
|
||||||
final URI uri = URI.create(url);
|
final URI uri = URI.create(url);
|
||||||
final String host = uri.getHost();
|
final String host = uri.getHost();
|
||||||
@@ -219,6 +219,7 @@ public class HttpSimpleClient {
|
|||||||
int urlpos = url.indexOf("/", url.indexOf("//") + 3);
|
int urlpos = url.indexOf("/", url.indexOf("//") + 3);
|
||||||
array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n"
|
array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n"
|
||||||
+ "Host: " + uri.getHost() + "\r\n"
|
+ "Host: " + uri.getHost() + "\r\n"
|
||||||
|
+ Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n"
|
||||||
+ "Content-Length: " + (body == null ? 0 : body.length) + "\r\n").getBytes(StandardCharsets.UTF_8));
|
+ "Content-Length: " + (body == null ? 0 : body.length) + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
if (headers == null || !headers.containsKey("User-Agent")) {
|
if (headers == null || !headers.containsKey("User-Agent")) {
|
||||||
array.put(header_bytes_useragent);
|
array.put(header_bytes_useragent);
|
||||||
|
|||||||
Reference in New Issue
Block a user