feat: 优化rpc构建
This commit is contained in:
parent
537ccadd0a
commit
944fcaf70a
@ -3,7 +3,6 @@ package cn.axzo.foundation.web.support.apps;
|
|||||||
import cn.axzo.foundation.page.PageResp;
|
import cn.axzo.foundation.page.PageResp;
|
||||||
import cn.axzo.foundation.util.PageUtils;
|
import cn.axzo.foundation.util.PageUtils;
|
||||||
import cn.axzo.foundation.web.support.TimerRefreshCache;
|
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.RpcClient;
|
||||||
import cn.axzo.foundation.web.support.rpc.RpcClientImpl;
|
import cn.axzo.foundation.web.support.rpc.RpcClientImpl;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
@ -39,11 +38,12 @@ public class AppCenterImpl implements AppCenter {
|
|||||||
@Builder
|
@Builder
|
||||||
public AppCenterImpl(ScheduledThreadPoolExecutor executor,
|
public AppCenterImpl(ScheduledThreadPoolExecutor executor,
|
||||||
String debugHost,
|
String debugHost,
|
||||||
Map<String, String> debugAppRoutes) {
|
Map<String, String> debugAppRoutes,
|
||||||
|
RpcClient rpcClient) {
|
||||||
|
|
||||||
Objects.requireNonNull(executor);
|
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,
|
this.appCache = new TimerRefreshCache<>("appCenterCache", INITIAL_DELAY_MILLIS,
|
||||||
REFRESH_INTERVAL_MILLIS, executor, this::loadApps);
|
REFRESH_INTERVAL_MILLIS, executor, this::loadApps);
|
||||||
this.debugHost = debugHost;
|
this.debugHost = debugHost;
|
||||||
|
|||||||
@ -22,12 +22,11 @@ import java.util.function.Function;
|
|||||||
public class OkHttpClientImpl implements HttpClient {
|
public class OkHttpClientImpl implements HttpClient {
|
||||||
private OkHttpClient okHttpClient;
|
private OkHttpClient okHttpClient;
|
||||||
private Call.Factory callFactory;
|
private Call.Factory callFactory;
|
||||||
|
|
||||||
private Config config;
|
private Config config;
|
||||||
private List<Interceptor> interceptorList;
|
private final List<Interceptor> interceptorList;
|
||||||
private Set<String> logResponseHeaderNames;
|
private final Set<String> logResponseHeaderNames;
|
||||||
|
|
||||||
private final static long MAX_RESPONSE_LOG_LENGTH = 10_240L;
|
private static final long MAX_RESPONSE_LOG_LENGTH = 10_240L;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
public OkHttpClientImpl(Config config, List<Interceptor> interceptorList,
|
public OkHttpClientImpl(Config config, List<Interceptor> interceptorList,
|
||||||
@ -169,7 +168,7 @@ public class OkHttpClientImpl implements HttpClient {
|
|||||||
.connectionPool(new ConnectionPool(config.getMaxIdleConnections(), config.getKeepAliveMinutes(), TimeUnit.MINUTES))
|
.connectionPool(new ConnectionPool(config.getMaxIdleConnections(), config.getKeepAliveMinutes(), TimeUnit.MINUTES))
|
||||||
.dispatcher(dispatcher);
|
.dispatcher(dispatcher);
|
||||||
|
|
||||||
interceptorList.forEach(interceptor -> builder.addInterceptor(interceptor));
|
interceptorList.forEach(builder::addInterceptor);
|
||||||
|
|
||||||
if (null != clientBuilder) {
|
if (null != clientBuilder) {
|
||||||
clientBuilder.accept(builder);
|
clientBuilder.accept(builder);
|
||||||
|
|||||||
@ -25,7 +25,7 @@ public class RpcClientImpl implements RpcClient {
|
|||||||
@Getter
|
@Getter
|
||||||
protected HttpClient httpClient;
|
protected HttpClient httpClient;
|
||||||
protected RequestProxy requestProxy;
|
protected RequestProxy requestProxy;
|
||||||
private static final Set<String> AXZO_HEADERS = ImmutableSet.of("workspaceId", "ouId", "Authorization", "terminal", "userinfo");
|
private static final Set<String> AXZO_HEADERS = ImmutableSet.of("workspaceId", "ouId", "Authorization", "terminal", "userinfo", "ctxLogId", "traceId");
|
||||||
// XXX: http/2会把所有Header都转成小写, 历史定义的Header都是大写的,在http/2协议下会透传失败。
|
// XXX: http/2会把所有Header都转成小写, 历史定义的Header都是大写的,在http/2协议下会透传失败。
|
||||||
private static final TreeSet<String> CASE_INSENSITIVE_AXZO_HEADERS = AXZO_HEADERS.stream()
|
private static final TreeSet<String> CASE_INSENSITIVE_AXZO_HEADERS = AXZO_HEADERS.stream()
|
||||||
.collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
|
.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()
|
.map(request -> Collections.list(request.getHeaderNames()).stream()
|
||||||
// 通过http2协议的请求,默认会把大写转成小写,如"_SESSION_OBJECT" -> "_session_object",导致session无法透传,下一跳需要通过session验签会失败
|
// 通过http2协议的请求,默认会把大写转成小写,如"_SESSION_OBJECT" -> "_session_object",导致session无法透传,下一跳需要通过session验签会失败
|
||||||
.filter(p -> CASE_INSENSITIVE_AXZO_HEADERS.contains(p) || p.startsWith(AZXO_HEADER_PREFIX))
|
.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()),
|
.orElse(Collections.emptyMap()),
|
||||||
//设置callerApp
|
//设置callerApp
|
||||||
() -> AxContext.getRequest().map(e -> {
|
() -> AxContext.getRequest().map(e -> {
|
||||||
@ -47,12 +47,12 @@ public class RpcClientImpl implements RpcClient {
|
|||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
public RpcClientImpl(RequestProxy requestProxy,
|
public RpcClientImpl(RequestProxy requestProxy,
|
||||||
HttpClient.Config config,
|
Supplier<Map<String, String>> requestHeaderSupplier,
|
||||||
Supplier<Map<String, String>> requestHeaderSupplier) {
|
HttpClient httpClient) {
|
||||||
this.requestProxy = Optional.ofNullable(requestProxy).orElse(RequestProxy.SIMPLE_PROXY);
|
this.requestProxy = Optional.ofNullable(requestProxy).orElse(RequestProxy.SIMPLE_PROXY);
|
||||||
this.httpClient = OkHttpClientImpl.builder()
|
this.httpClient = Optional.ofNullable(httpClient).orElseGet(() -> OkHttpClientImpl.builder()
|
||||||
.config(config)
|
.config(HttpClient.Config.DEFAULT)
|
||||||
.build();
|
.build());
|
||||||
|
|
||||||
this.customHeaderSupplier = requestHeaderSupplier;
|
this.customHeaderSupplier = requestHeaderSupplier;
|
||||||
}
|
}
|
||||||
@ -80,17 +80,12 @@ public class RpcClientImpl implements RpcClient {
|
|||||||
Objects.requireNonNull(requestParams);
|
Objects.requireNonNull(requestParams);
|
||||||
|
|
||||||
//XXX 附加默认的header, 以及自定义的header
|
//XXX 附加默认的header, 以及自定义的header
|
||||||
DEFAULT_HEADER_SUPPLIERS.stream()
|
DEFAULT_HEADER_SUPPLIERS
|
||||||
.forEach(headerSupplier -> headerSupplier.get().entrySet()
|
.forEach(headerSupplier -> headerSupplier.get().forEach(requestParams::addHeaderIfAbsent));
|
||||||
.forEach(headerEntry ->
|
|
||||||
requestParams.addHeaderIfAbsent(headerEntry.getKey(), headerEntry.getValue())
|
|
||||||
));
|
|
||||||
|
|
||||||
if (customHeaderSupplier != null) {
|
if (customHeaderSupplier != null) {
|
||||||
Map<String, String> map = customHeaderSupplier.get();
|
Map<String, String> map = customHeaderSupplier.get();
|
||||||
map.entrySet().forEach(e -> {
|
map.forEach(requestParams::addHeader);
|
||||||
requestParams.addHeader(e.getKey(), e.getValue());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return requestProxy.request(token -> {
|
return requestProxy.request(token -> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user