|
@@ -1,13 +1,9 @@
|
|
|
package com.chelvc.framework.feign.interceptor;
|
|
|
|
|
|
-import java.net.URI;
|
|
|
import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.function.Predicate;
|
|
|
|
|
|
+import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.base.context.Session;
|
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
import com.chelvc.framework.base.context.Using;
|
|
@@ -16,12 +12,8 @@ import com.chelvc.framework.base.util.SpringUtils;
|
|
|
import com.chelvc.framework.common.model.Platform;
|
|
|
import com.chelvc.framework.common.model.Terminal;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
-import com.chelvc.framework.common.util.StringUtils;
|
|
|
-import com.chelvc.framework.feign.config.FeignProperties;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
import feign.RequestInterceptor;
|
|
|
import feign.RequestTemplate;
|
|
|
-import feign.Target;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
@@ -35,59 +27,18 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
|
- private final Set<String> servers;
|
|
|
+ private final String location;
|
|
|
+ private final Map<String, ?> prefixes;
|
|
|
private final Map<String, ?> mappings;
|
|
|
- private final List<FeignProperties.Forward> forwards;
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
|
|
|
+ this.location = "http://" + ApplicationContextHolder.getApplicationName(applicationContext.getEnvironment());
|
|
|
RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
|
|
|
- Map<String, Predicate<Class<?>>> prefixes = handlerMapping.getPathPrefixes();
|
|
|
- this.servers = ObjectUtils.isEmpty(prefixes) ? Collections.emptySet() : prefixes.keySet();
|
|
|
+ this.prefixes = ObjectUtils.ifEmpty(handlerMapping.getPathPrefixes(), Collections::emptyMap);
|
|
|
Object registry = ObjectUtils.getObjectValue(handlerMapping, "mappingRegistry");
|
|
|
- Map<?, ?> mappings = (Map<?, ?>) ObjectUtils.getObjectValue(registry, "urlLookup");
|
|
|
- this.mappings = ObjectUtils.isEmpty(mappings) ? Collections.emptyMap() : (Map<String, ?>) mappings;
|
|
|
-
|
|
|
- // 初始化请求转发配置
|
|
|
- FeignProperties properties = applicationContext.getBean(FeignProperties.class);
|
|
|
- List<FeignProperties.Forward> forwards = Lists.newArrayList(properties.getForwards());
|
|
|
- if (SpringUtils.isMultipleServer()
|
|
|
- && forwards.stream().noneMatch(forward -> Objects.equals(forward.getSource(), "*"))) {
|
|
|
- FeignProperties.Forward forward = new FeignProperties.Forward();
|
|
|
- forward.setSource("*");
|
|
|
- forward.setPrefixed(true);
|
|
|
- forwards.add(forward);
|
|
|
- }
|
|
|
- this.forwards = ObjectUtils.isEmpty(forwards) ? Collections.emptyList() : forwards;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 请求转发
|
|
|
- *
|
|
|
- * @param template 请求模版对象实例
|
|
|
- */
|
|
|
- protected void forward(@NonNull RequestTemplate template) {
|
|
|
- Target<?> target = template.feignTarget();
|
|
|
- String server = target.name();
|
|
|
- for (FeignProperties.Forward forward : this.forwards) {
|
|
|
- String source = forward.getSource(), mapping = forward.getTarget();
|
|
|
- if (ObjectUtils.equals(source, server) || ObjectUtils.equals(source, "*")) {
|
|
|
- boolean internal = this.servers.contains(server);
|
|
|
- if (internal || forward.isPrefixed()) {
|
|
|
- String uri = HttpUtils.uri(server, template.path());
|
|
|
- if (!internal || this.mappings.containsKey(uri)) {
|
|
|
- template.uri(uri);
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.notEmpty(mapping) && !Objects.equals(mapping, server)) {
|
|
|
- URI uri = URI.create(target.url());
|
|
|
- String url = uri.getScheme() + "://" + mapping;
|
|
|
- template.target(url);
|
|
|
- template.feignTarget(new Target.HardCodedTarget<>(target.type(), mapping, url));
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ Map<String, ?> mappings = (Map<String, ?>) ObjectUtils.getObjectValue(registry, "urlLookup");
|
|
|
+ this.mappings = ObjectUtils.ifEmpty(mappings, Collections::emptyMap);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -101,6 +52,7 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
|
return;
|
|
|
}
|
|
|
template.header("x-real-ip", session.getHost());
|
|
|
+ template.header("x-request-id", session.getRid());
|
|
|
template.header(SessionContextHolder.HEADER_ID, (String) ObjectUtils.ifNull(session.getId(), String::valueOf));
|
|
|
template.header(SessionContextHolder.HEADER_USING, ObjectUtils.ifNull(session.getUsing(), Using::name));
|
|
|
template.header(SessionContextHolder.HEADER_SCOPE, session.getScope());
|
|
@@ -119,13 +71,33 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
|
template.header(SessionContextHolder.HEADER_AUTHORITIES, session.getAuthorities());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始化多服务部署接口调用目标
|
|
|
+ *
|
|
|
+ * @param template 请求模版对象实例
|
|
|
+ */
|
|
|
+ protected void initializeMultipleServerTarget(@NonNull RequestTemplate template) {
|
|
|
+ if (!SpringUtils.isMultipleServer()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String server = template.feignTarget().name();
|
|
|
+ if (this.prefixes.containsKey(server)) {
|
|
|
+ String uri = HttpUtils.uri(server, template.path());
|
|
|
+ if (this.mappings.containsKey(uri)) {
|
|
|
+ // 更新接口资源地址
|
|
|
+ template.uri(uri);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置本地调用地址
|
|
|
+ template.target(this.location);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void apply(RequestTemplate template) {
|
|
|
this.initializeRequestHeader(template);
|
|
|
-
|
|
|
- if (ObjectUtils.notEmpty(this.forwards)) {
|
|
|
- this.forward(template);
|
|
|
- }
|
|
|
+ this.initializeMultipleServerTarget(template);
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
log.debug("Feign request: {}", template);
|