Browse Source

修复feign请求转发异常问题

woody 5 months ago
parent
commit
bc963835a2

+ 18 - 2
framework-feign/src/main/java/com/chelvc/framework/feign/config/FeignProperties.java

@@ -18,15 +18,31 @@ import org.springframework.context.annotation.Configuration;
 @ConfigurationProperties("feign")
 @ConfigurationProperties("feign")
 public class FeignProperties {
 public class FeignProperties {
     /**
     /**
-     * 请求转发配置列表
+     * 请求转发配置
      */
      */
-    private List<Forward> forwards = Collections.emptyList();
+    private final Forward forward = new Forward();
 
 
     /**
     /**
      * 请求转发配置
      * 请求转发配置
      */
      */
     @Data
     @Data
     public static class Forward {
     public static class Forward {
+        /**
+         * 是否启用
+         */
+        private boolean enabled;
+
+        /**
+         * 请求转发映射列表
+         */
+        private List<Mapping> mappings = Collections.emptyList();
+    }
+
+    /**
+     * 请求转发映射
+     */
+    @Data
+    public static class Mapping {
         /**
         /**
          * 原服务
          * 原服务
          */
          */

+ 31 - 19
framework-feign/src/main/java/com/chelvc/framework/feign/interceptor/FeignInvokeInterceptor.java

@@ -36,13 +36,25 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
  */
  */
 @Slf4j
 @Slf4j
 public class FeignInvokeInterceptor implements RequestInterceptor {
 public class FeignInvokeInterceptor implements RequestInterceptor {
-    private final List<FeignProperties.Forward> forwards;
+    private final List<FeignProperties.Mapping> forwards;
 
 
     public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
     public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
-        // 加载请求转发配置
-        FeignProperties properties = applicationContext.getBean(FeignProperties.class);
-        List<FeignProperties.Forward> forwards = ObjectUtils.ifNull(
-                properties.getForwards(), Lists::newArrayList, Lists::newArrayList
+        this.forwards = this.mappings(applicationContext);
+    }
+
+    /**
+     * 获取请求转发映射表
+     *
+     * @param applicationContext 应用上下文
+     * @return 请求转发映射列表
+     */
+    private List<FeignProperties.Mapping> mappings(ApplicationContext applicationContext) {
+        FeignProperties.Forward forward = applicationContext.getBean(FeignProperties.class).getForward();
+        if (!forward.isEnabled()) {
+            return Collections.emptyList();
+        }
+        List<FeignProperties.Mapping> mappings = ObjectUtils.ifNull(
+                forward.getMappings(), Lists::newArrayList, Lists::newArrayList
         );
         );
         List<Resource> resources = ApplicationContextHolder.getApplicationResources();
         List<Resource> resources = ApplicationContextHolder.getApplicationResources();
         RequestMappingHandlerMapping handlers = applicationContext.getBean(RequestMappingHandlerMapping.class);
         RequestMappingHandlerMapping handlers = applicationContext.getBean(RequestMappingHandlerMapping.class);
@@ -53,17 +65,17 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
             for (Resource resource : resources) {
             for (Resource resource : resources) {
                 String name = ApplicationContextHolder.getApplicationName(resource);
                 String name = ApplicationContextHolder.getApplicationName(resource);
                 if (StringUtils.isEmpty(name) || ObjectUtils.equals(name, server)
                 if (StringUtils.isEmpty(name) || ObjectUtils.equals(name, server)
-                        || forwards.stream().anyMatch(forward -> Objects.equals(forward.getSource(), name))) {
+                        || mappings.stream().anyMatch(mapping -> Objects.equals(mapping.getSource(), name))) {
                     continue;
                     continue;
                 }
                 }
-                FeignProperties.Forward forward = new FeignProperties.Forward();
-                forward.setSource(name);
-                forward.setTarget(HostUtils.DEFAULT_LOCAL_NAME + ":" + port);
-                forward.setPrefixed(prefixes.contains(name));
-                forwards.add(forward);
+                FeignProperties.Mapping mapping = new FeignProperties.Mapping();
+                mapping.setSource(name);
+                mapping.setTarget(HostUtils.DEFAULT_LOCAL_NAME + ":" + port);
+                mapping.setPrefixed(prefixes.contains(name));
+                mappings.add(mapping);
             }
             }
         }
         }
-        this.forwards = forwards.isEmpty() ? Collections.emptyList() : Lists.newArrayList(forwards);
+        return mappings.isEmpty() ? Collections.emptyList() : Lists.newArrayList(mappings);
     }
     }
 
 
     /**
     /**
@@ -101,17 +113,17 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
         // 请求转发处理
         // 请求转发处理
         if (ObjectUtils.notEmpty(this.forwards)) {
         if (ObjectUtils.notEmpty(this.forwards)) {
             Target<?> target = template.feignTarget();
             Target<?> target = template.feignTarget();
-            for (FeignProperties.Forward forward : this.forwards) {
-                if (ObjectUtils.equals(forward.getSource(), target.name())
-                        || ObjectUtils.equals(forward.getSource(), "*")) {
-                    if (forward.isPrefixed()) {
+            for (FeignProperties.Mapping mapping : this.forwards) {
+                if (ObjectUtils.equals(mapping.getSource(), target.name())
+                        || ObjectUtils.equals(mapping.getSource(), "*")) {
+                    if (mapping.isPrefixed()) {
                         template.uri(HttpUtils.uri(target.name(), template.path()));
                         template.uri(HttpUtils.uri(target.name(), template.path()));
                     }
                     }
-                    if (!Objects.equals(forward.getTarget(), target.name())) {
+                    if (!Objects.equals(mapping.getTarget(), target.name())) {
                         URI uri = URI.create(target.url());
                         URI uri = URI.create(target.url());
-                        String url = uri.getScheme() + "://" + forward.getTarget();
+                        String url = uri.getScheme() + "://" + mapping.getTarget();
                         template.target(url);
                         template.target(url);
-                        template.feignTarget(new Target.HardCodedTarget<>(target.type(), forward.getTarget(), url));
+                        template.feignTarget(new Target.HardCodedTarget<>(target.type(), mapping.getTarget(), url));
                     }
                     }
                     break;
                     break;
                 }
                 }