Dynamic Request Routing via Spring Cloud Gateway

See original GitHub issue

I have recently started working on Spring Cloud Gateway. I added the following maven dependency in my project:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

But I couldn’t find what I was expecting to be included in a basic API gateway. I am trying to achieve the followings:

  1. Simple Request Routing I want my gateway to to receive a request and execute another url defined in Uri. But, I couldn’t find a way to receive GET request in gateway and execute POST request and vice versa. For example, in the example below, how may I execute Uri as POST?
    .route(r ->
                    r.path("/controller/action/{uhbgy}").and().method(HttpMethod.GET)
                      .uri("https://ip:port/group/2/${id}")
               )
  1. Reading request/query parameters from header/body from source request (path) and pass it to the destination request (Uri)?
  2. Changing routing rules at run time without any need to restart the application. Like I could define Uri to run against particular path in some file and may change it at run time.

I think 1st and 2nd point is very basic and should be supported. Can anyone please confirm that either SCG supports these things or not? Or either I missed in the documentation.

@spencergibb Can you please have a look into these points. Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
botorabicommented, Jun 12, 2019

Just one late comment about dynamically update the routes, you can do it during runtime by using the event publisher ApplicationEventPublisher. After changing your routes all you need is calling the method refreshRoutes() on the following component.

` @Component public class GatewayRoutesRefresher implements ApplicationEventPublisherAware {

private ApplicationEventPublisher publisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    publisher = applicationEventPublisher;
}

public void refreshRoutes() {
    publisher.publishEvent(new RefreshRoutesEvent(this));
}

} `

For a more complete reference on dynamically setup and modification of routes take a look on following open source project: https://github.com/botorabi/HomieCenter In particular this folder should be of your interest: https://github.com/botorabi/HomieCenter/tree/master/src/main/java/net/vrfun/homiecenter/reverseproxy

1reaction
spencergibbcommented, Oct 22, 2018

The path in the uri does not know about {id}. Please use the SetPath filter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Cloud Gateway Dynamic Routes from Database
After some time reading about spring cloud gateway, it is possible to configure routes through RouteLocatorBuilder class or through application.properties ...
Read more >
Dynamic Configuration of Spring Cloud Gateway Routing
The configuration of Spring Cloud Gateway is controlled by a series of "RouteDefinitionLocator" interfaces, which are as follows:
Read more >
Exploring the New Spring Cloud Gateway - Baeldung
3. Dynamic Routing. Just like Zuul, Spring Cloud Gateway provides means for routing requests to different services.
Read more >
Spring Cloud Gateway Dynamic Routing - Stack Overflow
And in the idResolvingGatewatFilter I'm making the changes (and the log statement look right-one...it just doesn't GO there! public Mono<Void> ...
Read more >
Spring Cloud - Gateway - Tutorialspoint
Dynamic Routing with Gateway · Route − These are the building blocks of the gateway which contain URL to which request is to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found