spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.53k stars 3.33k forks source link

Adding Routes Dynamically to gateway #1259

Open arpitkh12 opened 5 years ago

arpitkh12 commented 5 years ago

Need to add service registered with Eureka server as routes dynamically to spring cloud gateway. Have got it working using

spring.cloud.gateway.discovery.locator.lowerCaseServiceId: true
spring.cloud.gateway.discovery.locator.enabled: true
spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/api/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.filters[0].name: StripPrefix
spring.cloud.gateway.discovery.locator.filters[0].args[_genkey_0]: 1

Can someone help me in converting this to Java Code? Need to do this to use readBody() which we cannot do in yaml files as per https://github.com/spring-cloud/spring-cloud-gateway/issues/690

arpitkh12 commented 5 years ago

@ryanjbaxter - Do you have any updates on this. I have been researching through the gateway documents but found no solution.

spencergibb commented 5 years ago

There's not a way to use read body via yaml

arpitkh12 commented 5 years ago

@spencergibb - I know we cannot use read body via yaml. So is their a way to convert the above yaml to Java DSL ? Alternatively what is the other way to read request body in global filters without ReadBodyPredicateFactory.

ryanjbaxter commented 5 years ago

It is kind of ugly at the moment.

You would have to provide your own DiscoveryClientRouteDefinitionLocator bean and mark it as @Primary. In that bean you would have to write some code to add the read body predicate.

We should make this easier. Maybe by providing a list of predicate and filter beans we would automatically include?

spencergibb commented 5 years ago

We could probably make readBody() work with a SpEL expression that would work via yaml.

arpitkh12 commented 5 years ago

Any other way we can read the request body in GlobalFilter ?

arpitkh12 commented 5 years ago

My requirement is to make a call to an audit microservice which requires the request body ? So using rest template to make the call before proceeding to actual call but need to pass requestbody to it along with some other parameters.

arpitkh12 commented 5 years ago

It is kind of ugly at the moment.

You would have to provide your own DiscoveryClientRouteDefinitionLocator bean and mark it as @Primary. In that bean you would have to write some code to add the read body predicate.

We should make this easier. Maybe by providing a list of predicate and filter beans we would automatically include?

Can you please provide an example on how to implement custom DiscoveryClientRouteDefinitionLocator bean with the read body predicate.