Эх сурвалжийг харах

优化feign请求转发逻辑

woody 5 сар өмнө
parent
commit
8bb834ec6a

+ 15 - 3
framework-feign/src/main/java/com/chelvc/framework/feign/interceptor/FeignInvokeInterceptor.java

@@ -9,6 +9,7 @@ import java.util.Set;
 import java.util.function.Predicate;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
+import com.chelvc.framework.base.config.MultiserverMvcConfigurer;
 import com.chelvc.framework.base.context.Session;
 import com.chelvc.framework.base.context.Session;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.context.Using;
 import com.chelvc.framework.base.context.Using;
@@ -18,6 +19,7 @@ import com.chelvc.framework.common.model.Terminal;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.feign.config.FeignProperties;
 import com.chelvc.framework.feign.config.FeignProperties;
+import com.google.common.collect.Lists;
 import feign.RequestInterceptor;
 import feign.RequestInterceptor;
 import feign.RequestTemplate;
 import feign.RequestTemplate;
 import feign.Target;
 import feign.Target;
@@ -41,15 +43,25 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
 
 
     @SuppressWarnings("unchecked")
     @SuppressWarnings("unchecked")
     public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
     public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
-        Environment environment = applicationContext.getEnvironment();
         RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
         RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
         Map<String, Predicate<Class<?>>> prefixes = handlerMapping.getPathPrefixes();
         Map<String, Predicate<Class<?>>> prefixes = handlerMapping.getPathPrefixes();
         this.servers = ObjectUtils.isEmpty(prefixes) ? Collections.emptySet() : prefixes.keySet();
         this.servers = ObjectUtils.isEmpty(prefixes) ? Collections.emptySet() : prefixes.keySet();
         Object registry = ObjectUtils.getValue(handlerMapping, "mappingRegistry");
         Object registry = ObjectUtils.getValue(handlerMapping, "mappingRegistry");
         Map<?, ?> mappings = (Map<?, ?>) ObjectUtils.getValue(registry, "urlLookup");
         Map<?, ?> mappings = (Map<?, ?>) ObjectUtils.getValue(registry, "urlLookup");
         this.mappings = ObjectUtils.isEmpty(mappings) ? Collections.emptyMap() : (Map<String, ?>) mappings;
         this.mappings = ObjectUtils.isEmpty(mappings) ? Collections.emptyMap() : (Map<String, ?>) mappings;
+
+        // 初始化请求转发配置
+        Environment environment = applicationContext.getEnvironment();
         FeignProperties properties = applicationContext.getBean(FeignProperties.class);
         FeignProperties properties = applicationContext.getBean(FeignProperties.class);
-        this.forwards = ObjectUtils.ifEmpty(properties.getForwards(), forwards -> forwards.stream().map(forward -> {
+        List<FeignProperties.Forward> forwards = Lists.newArrayList(properties.getForwards());
+        if (applicationContext.containsBean(MultiserverMvcConfigurer.class.getName())
+                && forwards.stream().noneMatch(forward -> Objects.equals(forward.getSource(), "*"))) {
+            FeignProperties.Forward forward = new FeignProperties.Forward();
+            forward.setSource("*");
+            forward.setPrefixed(true);
+            forwards.add(forward);
+        }
+        this.forwards = forwards.isEmpty() ? Collections.emptyList() : forwards.stream().map(forward -> {
             FeignProperties.Forward copy = new FeignProperties.Forward();
             FeignProperties.Forward copy = new FeignProperties.Forward();
             copy.setSource(forward.getSource());
             copy.setSource(forward.getSource());
             copy.setTarget(forward.getTarget());
             copy.setTarget(forward.getTarget());
@@ -58,7 +70,7 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
                 copy.setTarget(environment.resolvePlaceholders(copy.getTarget()));
                 copy.setTarget(environment.resolvePlaceholders(copy.getTarget()));
             }
             }
             return copy;
             return copy;
-        }).collect(Collectors.toList()), Collections::emptyList);
+        }).collect(Collectors.toList());
     }
     }
 
 
     /**
     /**