spring-cloud-gateway
spring-cloud-gateway copied to clipboard
Enhance AOT-Support (Issue#3171)
Changes:
- replace registerBeanDefinition callback with a factoryMethod
- split out a RouterFunctionHolderFactory from the Registrar class and provide its bean in the AutoConfiguration
- set refresh scope on the RouterFunctionHolder bean only if RefreshScope bean definition registered and add warning
- added first bunch of runtime hints, that supports most handlers, predicates and filters as external route config (see comment below)
Remarks: fixes #3171
- I have signed the Contributor License Agreement
- The major code changes come from a mere extraction of a new
RouterFunctionHolderFactory
class from the Registrar (similar to what Josh did in his hacking session on UTube recently) in order to make this a bean and thus accessible for factory methods. My first solution was to just create the wholeGatewayMvcPropertiesBeanDefinitionRegistrar
class as bean in addition to its@Import
-role asImportBeanDefinitionRegistrar
. But I did not like this too much, as it puts "too much hats" on this class. However, if the amount of code changes scares you off now, this partial solution (not including the Runtime Hints) with just like 6 LOC changes is still available as branch https://github.com/wisskirchenj/spring-cloud-gateway/commit/a718c273453960078e17b451fbcb6c7243cabeda - The refresh scope on the RouterFunctionHolder (for cloud config support I guess) is only set, if there is a RefreshScope bean definition in the registry. This is exactly the case iff the property
spring.cloud.refresh.enabled
is not explicitly set to false. So in order to not crash in AOT, the user has to explicitly set this - but it seems to be in accordance with Spring Cloud Config, where there even is an explicit warning in the docs to do that for AOT (might be useful to add in gateway mvc docs too..). Still I think, the newbie to gateway-mvc will likely not set / know that. That is why I gave an instructive warning in the Registrar class.
now added and imported RuntimeHintsRegistrar to support externalized configs as the one I tested with below (properties-style for readability :-)).
RuntimeHints are definitely not complete yet though - e.g. I did not include Reflection Binds on RetryFilterFunctions
as this would have been Conditional on dependencies - but it should be a good start for a lot of native image use cases, I hope...
The application.properties settings, that run smoothly as native image in my local test setup:
spring.cloud.refresh.enabled=false
spring.cloud.gateway.mvc.routes[0].id=recipe
spring.cloud.gateway.mvc.routes[0].uri=http://localhost:8080/
spring.cloud.gateway.mvc.routes[0].predicates[0]=Path=/recipe/**
spring.cloud.gateway.mvc.routes[0].filters[0]=TokenRelay=
spring.cloud.gateway.mvc.routes[0].filters[1]=PrefixPath=/api
spring.cloud.gateway.mvc.routes[1].id=code
spring.cloud.gateway.mvc.routes[1].uri=http://localhost:8090/
spring.cloud.gateway.mvc.routes[1].predicates[0]=Path=/code/**
spring.cloud.gateway.mvc.routes[1].filters[0]=TokenRelay=
spring.cloud.gateway.mvc.routes[2].id=quiz
spring.cloud.gateway.mvc.routes[2].uri=http://localhost:9090/
spring.cloud.gateway.mvc.routes[2].predicates[0]=Path=/quiz/**
spring.cloud.gateway.mvc.routes[2].filters[0]=TokenRelay=
spring.cloud.gateway.mvc.routes[2].filters[1]=PrefixPath=/juergen/api
@spencergibb May I politely request your review on this?
@ryanjbaxter @OlgaMaciaszek Just saw Spencer leaving to Cockroach Labs 😢 That may explain there was no progress in this PR. Do you want to proceed with it? Still it will make Gateway MVC (mostly) native image compatible...
I think you want to check who was the author of that tweet, it doesn’t appear to be @spencergibb 😉
We all have a lot of work on our plates we will try to get to it soon
@ryanjbaxter: Thanks for the reply.
I think you want to check who was the author of that tweet, it doesn’t appear to be @spencergibb 😉
No :-) I authored it myself. But it was in exchange and on request of @spencergibb.
We all have a lot of work on our plates we will try to get to it soon
Sorry - I patiently waited for 2 months. I was just worried, it might get lost, though it can add value.
No I mean the tweet about working for Cockroach was not from Spencer, he just retweeted it.
I promise we will get to it eventually, we appreciate the PR!
Not going anywhere
I'm waiting for this PR getting release
@maradanasai Please watch https://github.com/spring-cloud/spring-cloud-release/milestones this will be part of 2023.0.1 in mid march
I want @OlgaMaciaszek to look, and she will be back next week. The release has been rescheduled for later this month, 2024-03-26.
@spencergibb @OlgaMaciaszek
I just resolved the merge conflicts by transferring adapted methods (mainly making operationArgs
now Map<String, Object>
) inside GatewayMvcPropertiesBeanDefinitionRegistrar::routerFunctionHolderSupplier
(and subordinated methods) to the RouterFunctionHolderFactory
in my PR-branch.
maven verify passes fine.
Re-running build. Still want @OlgaMaciaszek to look
Still want @OlgaMaciaszek to look
Yes, sure!
Hello @wisskirchenj, the hints implementation looks good, but have added some minor comments. Please address.
@OlgaMaciaszek Thank you for your review. I have incorporated all your comments - except the one with the package modifier on Registrar class, as with the present locations of Registrar and AutoConfiguration classes public
is needed.