From 944fcaf70adf120d0f2d83e82b704ef533a9eaf4 Mon Sep 17 00:00:00 2001 From: zengxiaobo Date: Thu, 11 Jul 2024 16:59:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96rpc=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/support/apps/AppCenterImpl.java | 6 ++--- .../web/support/rpc/OkHttpClientImpl.java | 9 +++---- .../web/support/rpc/RpcClientImpl.java | 25 ++++++++----------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/apps/AppCenterImpl.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/apps/AppCenterImpl.java index 87fc58b..cc4db24 100644 --- a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/apps/AppCenterImpl.java +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/apps/AppCenterImpl.java @@ -3,7 +3,6 @@ package cn.axzo.foundation.web.support.apps; import cn.axzo.foundation.page.PageResp; import cn.axzo.foundation.util.PageUtils; import cn.axzo.foundation.web.support.TimerRefreshCache; -import cn.axzo.foundation.web.support.rpc.RequestProxy; import cn.axzo.foundation.web.support.rpc.RpcClient; import cn.axzo.foundation.web.support.rpc.RpcClientImpl; import com.alibaba.fastjson.JSONArray; @@ -39,11 +38,12 @@ public class AppCenterImpl implements AppCenter { @Builder public AppCenterImpl(ScheduledThreadPoolExecutor executor, String debugHost, - Map debugAppRoutes) { + Map debugAppRoutes, + RpcClient rpcClient) { Objects.requireNonNull(executor); - this.rpcClient = RpcClientImpl.builder().requestProxy(RequestProxy.SIMPLE_PROXY).build(); + this.rpcClient = Optional.ofNullable(rpcClient).orElseGet(() -> RpcClientImpl.builder().build()); this.appCache = new TimerRefreshCache<>("appCenterCache", INITIAL_DELAY_MILLIS, REFRESH_INTERVAL_MILLIS, executor, this::loadApps); this.debugHost = debugHost; diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/OkHttpClientImpl.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/OkHttpClientImpl.java index d2abf60..389e228 100644 --- a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/OkHttpClientImpl.java +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/OkHttpClientImpl.java @@ -22,12 +22,11 @@ import java.util.function.Function; public class OkHttpClientImpl implements HttpClient { private OkHttpClient okHttpClient; private Call.Factory callFactory; - private Config config; - private List interceptorList; - private Set logResponseHeaderNames; + private final List interceptorList; + private final Set logResponseHeaderNames; - private final static long MAX_RESPONSE_LOG_LENGTH = 10_240L; + private static final long MAX_RESPONSE_LOG_LENGTH = 10_240L; @Builder public OkHttpClientImpl(Config config, List interceptorList, @@ -169,7 +168,7 @@ public class OkHttpClientImpl implements HttpClient { .connectionPool(new ConnectionPool(config.getMaxIdleConnections(), config.getKeepAliveMinutes(), TimeUnit.MINUTES)) .dispatcher(dispatcher); - interceptorList.forEach(interceptor -> builder.addInterceptor(interceptor)); + interceptorList.forEach(builder::addInterceptor); if (null != clientBuilder) { clientBuilder.accept(builder); diff --git a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClientImpl.java b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClientImpl.java index 80a4237..078e9fd 100644 --- a/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClientImpl.java +++ b/web-support-lib/src/main/java/cn/axzo/foundation/web/support/rpc/RpcClientImpl.java @@ -25,7 +25,7 @@ public class RpcClientImpl implements RpcClient { @Getter protected HttpClient httpClient; protected RequestProxy requestProxy; - private static final Set AXZO_HEADERS = ImmutableSet.of("workspaceId", "ouId", "Authorization", "terminal", "userinfo"); + private static final Set AXZO_HEADERS = ImmutableSet.of("workspaceId", "ouId", "Authorization", "terminal", "userinfo", "ctxLogId", "traceId"); // XXX: http/2会把所有Header都转成小写, 历史定义的Header都是大写的,在http/2协议下会透传失败。 private static final TreeSet CASE_INSENSITIVE_AXZO_HEADERS = AXZO_HEADERS.stream() .collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))); @@ -36,7 +36,7 @@ public class RpcClientImpl implements RpcClient { .map(request -> Collections.list(request.getHeaderNames()).stream() // 通过http2协议的请求,默认会把大写转成小写,如"_SESSION_OBJECT" -> "_session_object",导致session无法透传,下一跳需要通过session验签会失败 .filter(p -> CASE_INSENSITIVE_AXZO_HEADERS.contains(p) || p.startsWith(AZXO_HEADER_PREFIX)) - .collect(Collectors.toMap(e -> e, e -> request.getHeader(e), (oldValue, newValue) -> newValue))) + .collect(Collectors.toMap(e -> e, request::getHeader, (oldValue, newValue) -> newValue))) .orElse(Collections.emptyMap()), //设置callerApp () -> AxContext.getRequest().map(e -> { @@ -47,12 +47,12 @@ public class RpcClientImpl implements RpcClient { @Builder public RpcClientImpl(RequestProxy requestProxy, - HttpClient.Config config, - Supplier> requestHeaderSupplier) { + Supplier> requestHeaderSupplier, + HttpClient httpClient) { this.requestProxy = Optional.ofNullable(requestProxy).orElse(RequestProxy.SIMPLE_PROXY); - this.httpClient = OkHttpClientImpl.builder() - .config(config) - .build(); + this.httpClient = Optional.ofNullable(httpClient).orElseGet(() -> OkHttpClientImpl.builder() + .config(HttpClient.Config.DEFAULT) + .build()); this.customHeaderSupplier = requestHeaderSupplier; } @@ -80,17 +80,12 @@ public class RpcClientImpl implements RpcClient { Objects.requireNonNull(requestParams); //XXX 附加默认的header, 以及自定义的header - DEFAULT_HEADER_SUPPLIERS.stream() - .forEach(headerSupplier -> headerSupplier.get().entrySet() - .forEach(headerEntry -> - requestParams.addHeaderIfAbsent(headerEntry.getKey(), headerEntry.getValue()) - )); + DEFAULT_HEADER_SUPPLIERS + .forEach(headerSupplier -> headerSupplier.get().forEach(requestParams::addHeaderIfAbsent)); if (customHeaderSupplier != null) { Map map = customHeaderSupplier.get(); - map.entrySet().forEach(e -> { - requestParams.addHeader(e.getKey(), e.getValue()); - }); + map.forEach(requestParams::addHeader); } return requestProxy.request(token -> {