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



我有一个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,而不是您在属性中设置的MyLogging

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

filters:
- name: MyLoggingGatewayFilterFactory

仔细检查您的Spring组件扫描是否为您的特定过滤器类拾取了@Component注释。默认情况下,Spring只会在@SpringBootApplication的包下拾取那些,因此,如果过滤器在一个单独的基本包下,则可能需要明确地告诉组件扫描包含该包。

例如

@SpringBootApplication
@ComponentScan({"com.myapp", "com.myfilters"})
public class MyApp {

根据spring文档,自定义过滤器类不需要组件。

https://cloud.spring.io/spring-cloud-gateway/reference/html/#writing-自定义网关过滤器工厂

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

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

相关内容

  • 没有找到相关文章

最新更新