I'm not getting an output from my global filter (or any of my filters). I can verify that the bean for each filter is created at runtime, but I'm not sure why they aren't executed.
Dependencies:
spring-boot-starter-parent 2.2.6.RELEASE
spring-cloud-starter-gateway 2.2.4
This is a global filter that I've created for testing purposes:
@Component
public class TestFilter implements GlobalFilter, Ordered{
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("First Pre Global Filter");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
log.info("Last Post Global Filter");
}));
}
@Override
public int getOrder() {
return -1;
}
}
Routes config:
spring:
cloud:
gateway:
routes:
- id: microservice
uri: lb://microservice
predicates:
- Path=/microservice/**
...
I've also tried setting up another spring cloud gateway with the filter above and it works, so there's definitely nothing wrong with the filter. Any idea what might be the cause of my issue? Or how do I troubleshoot it? Thanks in advance.
CodePudding user response:
Have you tried it this way :
@Component
@Order(-1)
public class TestFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("First Pre Global Filter");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
log.info("Last Post Global Filter");
}));
}
}
CodePudding user response:
The issue is with my WebClient
. I did not access the microservice through the gateway but instead, it directly accesses the microservice. Hence, the filters did not run as the request did not go through the routes.
Original:
webClient = webClientBuilder.baseUrl("http://microservice").build();
Fixed:
webClient = webClientBuilder.baseUrl("http://localhost:8080/microservice").build();