feat: 设置callerApp
This commit is contained in:
parent
05999904da
commit
77766035bc
@ -12,6 +12,7 @@ import lombok.*;
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
public class CallerApp {
|
||||
|
||||
String appName;
|
||||
AppEnvEnum appEnv;
|
||||
/**
|
||||
|
||||
@ -4,8 +4,10 @@ import cn.axzo.foundation.util.FastjsonUtils;
|
||||
import cn.axzo.foundation.web.support.AppRuntime;
|
||||
import cn.axzo.foundation.web.support.context.AxContextInterceptor;
|
||||
import cn.axzo.foundation.web.support.exception.AbstractExceptionHandler;
|
||||
import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor;
|
||||
import cn.axzo.foundation.web.support.interceptors.PrettyPrintInterceptor;
|
||||
import cn.axzo.foundation.web.support.interceptors.PrintVerboseInterceptor;
|
||||
import cn.axzo.foundation.web.support.resolvers.CallerAppResolver;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
@ -180,6 +182,7 @@ public class DefaultWebMvcConfig extends DelegatingWebMvcConfiguration implement
|
||||
// 不建议使用DelegatingWebMvcConfiguration.addCorsMappings. 使用该方法配置之后再使用自定义拦截器时跨域相关配置就会失效
|
||||
registry.addInterceptor(new CorsInterceptor(corsProcessorType));
|
||||
registry.addInterceptor(new PrettyPrintInterceptor());
|
||||
registry.addInterceptor(new CallerAppInterceptor(appRuntime));
|
||||
|
||||
super.addInterceptors(registry);
|
||||
|
||||
@ -204,6 +207,7 @@ public class DefaultWebMvcConfig extends DelegatingWebMvcConfiguration implement
|
||||
argumentResolvers.add(e);
|
||||
});
|
||||
}
|
||||
argumentResolvers.add(new CallerAppResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
package cn.axzo.foundation.web.support.interceptors;
|
||||
|
||||
import cn.axzo.foundation.caller.CallerApp;
|
||||
import cn.axzo.foundation.web.support.AppRuntime;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.base.Strings;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 将header中的pretty放到response. JsonCovert会根据该header选择format的方式
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CallerAppInterceptor implements HandlerInterceptor {
|
||||
public static final String HTTP_REQUEST_HEADER = "axzo-caller-app";
|
||||
public static final String HTTP_REQUEST_ATTRIBUTE = "__att_caller_app";
|
||||
public static final String NEXT_HTTP_REQUEST_ATTRIBUTE = "__att_caller_app_for_next";
|
||||
|
||||
private final AppRuntime appRuntime;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String header = request.getHeader(HTTP_REQUEST_HEADER);
|
||||
CallerApp callerApp = null;
|
||||
if (!Strings.isNullOrEmpty(header)) {
|
||||
callerApp = JSONObject.parseObject(header, CallerApp.class);
|
||||
request.setAttribute(HTTP_REQUEST_ATTRIBUTE, callerApp);
|
||||
}
|
||||
request.setAttribute(NEXT_HTTP_REQUEST_ATTRIBUTE, CallerApp.builder()
|
||||
.appEnv(appRuntime.getEnv())
|
||||
.appName(appRuntime.getAppName())
|
||||
.previous(callerApp)
|
||||
.build());
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package cn.axzo.foundation.web.support.resolvers;
|
||||
|
||||
import cn.axzo.foundation.caller.CallerApp;
|
||||
import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
public class CallerAppResolver implements HandlerMethodArgumentResolver {
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
||||
return methodParameter.getParameterType().isAssignableFrom(CallerApp.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter,
|
||||
ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest,
|
||||
WebDataBinderFactory binderFactory) throws Exception {
|
||||
return webRequest.getAttribute(CallerAppInterceptor.HTTP_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
|
||||
}
|
||||
}
|
||||
@ -2,11 +2,14 @@ package cn.axzo.foundation.web.support.rpc;
|
||||
|
||||
import cn.axzo.foundation.page.PageResp;
|
||||
import cn.axzo.foundation.result.ApiResult;
|
||||
import cn.axzo.foundation.web.support.context.AxContext;
|
||||
import cn.axzo.foundation.web.support.interceptors.CallerAppInterceptor;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@ -53,12 +56,6 @@ public interface RpcClient {
|
||||
return post(url, content, typeReference, Boolean.TRUE);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default <T> T post(String url, Object content, Class<T> clz) {
|
||||
return post(url, content, new TypeReference<ApiResult<T>>(clz) {
|
||||
});
|
||||
}
|
||||
|
||||
/* get 相关方法 */
|
||||
default <T> T get(String url, TypeReference<ApiResult<T>> typeReference, RequestParams requestParams) {
|
||||
return execute(HttpClient.HttpMethod.GET, url, requestParams, s -> JSONObject.parseObject(s, typeReference));
|
||||
@ -118,7 +115,14 @@ public interface RpcClient {
|
||||
// 将axzo-开头的header复制到请求的下一跳
|
||||
String AZXO_HEADER_PREFIX = "axzo-";
|
||||
|
||||
List<Supplier<Map<String, String>>> DEFAULT_HEADER_SUPPLIERS = ImmutableList.of();
|
||||
List<Supplier<Map<String, String>>> DEFAULT_HEADER_SUPPLIERS = ImmutableList.of(
|
||||
//设置callerApp
|
||||
() -> AxContext.getRequest().map(e -> {
|
||||
Object caller = e.getAttribute(CallerAppInterceptor.NEXT_HTTP_REQUEST_ATTRIBUTE);
|
||||
return ImmutableMap.of(CallerAppInterceptor.HTTP_REQUEST_HEADER, JSONObject.toJSONString(caller));
|
||||
}).orElse(ImmutableMap.of())
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 使用builder模式来发起请求, 先通过request()获得builder对象. 再构建具体的请求方式
|
||||
|
||||
Loading…
Reference in New Issue
Block a user