1 year ago
#362019
Gourab Guha
Spring Cloud Gateway's RouteDefinitionRepository is getting called multiple times during startup and refresh
I need to read the Gateway’s Route Definitions from an external service, which I wanted to achieve by creating a custom RouteDefinitionRepository class, so I started a small POC on RouteDefinitionRepository.
In this POC, while creating an implementation of RouteDefinitionRepository I have overridden the getRouteDefinations method and returned a Flux<RouteDefinition> from it which is statically built (so far not integrated with the external service). I marked this class as @Component and surprisingly I see the getRouteDefinations method is invoked three times.
My expectation is during startup and during refresh this method will get invoked only once.
Here is the snippet of my custom Route Definition class.
@Component
@Slf4j
public class CustomRouteDefinationRepository implements RouteDefinitionRepository {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
log.info("Inside getRouteDefinitions >>>");
List<ApiRouteTO> list = new ArrayList<ApiRouteTO>();
ApiRouteTO routeTO_1 = new ApiRouteTO();
routeTO_1.setApiRouteId("1");
routeTO_1.setName("route_1");
routeTO_1.setUrl("http://localhost:8080/hello");
try {
routeTO_1.setPredicate(objectMapper.readValue(predicate_1, JsonNode.class));
} catch (Exception ex) {
log.error("Error", ex);
}
list.add(routeTO_1);
List<RouteDefinition> rdList = list.stream().map(this::getRd).collect(Collectors.toList());
return Flux.fromIterable(rdList);
}
Also to check whether Refresh Event is happening I created a @EventListener and its getting called once during startup and once during refresh.
@EventListener(RefreshRoutesEvent.class)
public void handleEvents() {
log.info(" $$$$$$$$$$$ Inside Refresh Event $$$$$$$$$$$");
}
My main class (including the event listener)
@ComponentScan("com.poc.route.gw")
@SpringBootApplication
@Slf4j
public class GwApplication {
public static void main(String[] args) {
SpringApplication.run(GwApplication.class, args);
}
@EventListener(RefreshRoutesEvent.class)
public void handleEvents() {
log.info(" $$$$$$$$$$$ Inside Refresh Event $$$$$$$$$$$");
}
}
Following is the log captured during startup
egouguh@IN-00215733 MINGW64 /c/workspace/poc/springgwpoc/gw/target (main)
$ java -jar gw-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.5)
2022-04-01 09:21:58.553 INFO 33400 --- [ main] com.poc.route.gw.GwApplication : Starting GwApplication v0.0.1-SNAPSHOT using Java 11 on IN-00215733 with PID 33400 (C:\workspace\poc\springgwpoc\gw\target\gw-0.0.1-SNAPSHOT.jar started by egouguh in C:\workspace\poc\springgwpoc\gw\target)
2022-04-01 09:21:58.564 INFO 33400 --- [ main] com.poc.route.gw.GwApplication : No active profile set, falling back to 1 default profile: "default"
2022-04-01 09:22:01.692 INFO 33400 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=1162eef5-9ddd-3db8-b935-0f1e65f3ea5e
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2022-04-01 09:22:08.396 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBody]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [XForwardedRemoteAddr]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2022-04-01 09:22:08.397 INFO 33400 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2022-04-01 09:22:08.805 INFO 33400 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2022-04-01 09:22:15.618 INFO 33400 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 8081
2022-04-01 09:22:21.042 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : Inside getRouteDefinitions >>>
2022-04-01 09:22:21.183 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : route route_1 has been added / refreshed
2022-04-01 09:22:21.217 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : Inside getRouteDefinitions >>>
2022-04-01 09:22:21.219 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : route route_1 has been added / refreshed
2022-04-01 09:22:21.321 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : Inside getRouteDefinitions >>>
2022-04-01 09:22:21.322 INFO 33400 --- [ main] .p.r.g.r.CustomRouteDefinationRepository : route route_1 has been added / refreshed
2022-04-01 09:22:21.324 INFO 33400 --- [ main] com.poc.route.gw.GwApplication : $$$$$$$$$$$ Inside Refresh Event $$$$$$$$$$$
2022-04-01 09:22:21.338 INFO 33400 --- [ main] com.poc.route.gw.GwApplication : Started GwApplication in 26.287 seconds (JVM running for 27.906)
I am running it under Java 11 with Spring Boot version 2.6.5 and Spring cloud version is 2021.0.1
The code is in the following git repo and is publicly accessible: https://github.com/gourabepsilon/springgwpoc.git
spring
spring-cloud
spring-cloud-gateway
0 Answers
Your Answer