Spring Cloud Gateway - 找不到名称为 [Filter_Name] 的 GatewayFilterFactory

Posted

技术标签:

【中文标题】Spring Cloud Gateway - 找不到名称为 [Filter_Name] 的 GatewayFilterFactory【英文标题】:Spring Cloud Gateway - Unable to find GatewayFilterFactory with name [Filter_Name] 【发布时间】:2021-01-24 11:09:08 【问题描述】:

我有一个 spring 云网关应用程序。我正在尝试设置网关过滤器。 Spring Boot 版本为2.3.4.RELEASE 依赖项如下:

dependencies 
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation platform(SpringBootPlugin.BOM_COORDINATES)
    implementation platform('org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR8')
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'


这是网关客户端的配置

server:
  port: 8081
spring:
  cloud:
    gateway:
      routes:
        - id: onboard_redirect
          uri: http://localhost:8080/api/v1/onboard
          predicates:
            - Path=/api/v1/onboard
          filters:
            - name: MyLogging
              args:
                baseMessage: My Custom Message
                preLogger: true
                postLogger: true

这是我的过滤器类:

@Component
public class MyLoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<MyLoggingGatewayFilterFactory.Config> 

    final Logger logger =
            LoggerFactory.getLogger(MyLoggingGatewayFilterFactory.class);

    public MyLoggingGatewayFilterFactory() 
        super(Config.class);
    

    @Override
    public GatewayFilter apply(Config config) 
        return (exchange, chain) -> 
            // Pre-processing
            if (config.preLogger) 
                logger.info("Pre GatewayFilter logging: "
                        + config.baseMessage);
            
            return chain.filter(exchange)
                    .then(Mono.fromRunnable(() -> 
                        // Post-processing
                        if (config.postLogger) 
                            logger.info("Post GatewayFilter logging: "
                                    + config.baseMessage);
                        
                    ));
        ;
    

    public static class Config 
        public String baseMessage;
        public boolean preLogger;
        public boolean postLogger;
    

一切正常,无需配置过滤器,但当我配置过滤器时,出现以下错误:

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name MyLogging
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name MyLogging

我在这里做错了什么?

【问题讨论】:

如果你在MyLoggingGatewayFilterFactory() 中放置一个断点,它会被命中吗? 不,不是应用程序无法启动 这就是问题所在。您的@Component 没有被扫描接收到。尝试在配置类中将MyLoggingGatewayFilterFactory 设为@Bean 【参考方案1】:

过滤器类是MyLoggingGatewayFilterFactory,而不是您在属性中设置的MyLogging

尝试在您的application.yml 文件中进行以下修改:

filters:
    - name: MyLoggingGatewayFilterFactory

【讨论】:

实际上,将过滤器名称设置为MyLogging也非常有效,因为过滤器类名称为MyLoggingGatewayFilterFactory,它满足解析这些过滤器的Spring命名约定......它只需要满足命名如&lt;filter-name&gt;GatewayFilterFactory【参考方案2】:

在 application.properties 文件中添加此依赖项。

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-circuitbreaker-reactor- 
     resilience4j</artifactId>
</dependency>

【讨论】:

以上是关于Spring Cloud Gateway - 找不到名称为 [Filter_Name] 的 GatewayFilterFactory的主要内容,如果未能解决你的问题,请参考以下文章

spring cloud gateway 如何工作

Spring Cloud Gateway集成

spring cloud gateway 的执行流程

spring cloud gateway 某些路由中跳过全局过滤器

spring cloud gateway 报错 Unable to find GatewayFilterFactory with name

Spring Cloud(18)——gateway