spring-cloud / spring-cloud/spring-cloud-gateway

Support dynamic URI building for the upstream service

Open
#1,462 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Is your feature request related to a problem? Please describe.
I would like to be able to build upstream service URI based on components of the request. For instance given a request with the path /api/{serviceName} I would like to build the upstream URI using the serviceName piece like so:

Map ports = Map.of("service1", 8080, "service2", 8081);
URI upstreamUri = "http://host:" + ports.get(serviceName) + "/";

Describe the solution you'd like
For the fluent routes (RouteLocatorBuilder) one solution might be to introduce another method e.g. uriBuilder which will have access to request details:

@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
  return builder.routes()
    .route("dynamic-route", predicate -> predicate.path(/api/**))
    .uriBuilder(request -> {
      String serviceName = "todo"; //extract service name from the reqeust's path
      return "http://host:" + ports.get(serviceName) + "/"
    })
    .build();
}

Describe alternatives you've considered
I couldn't find any alternatives. The closest I could find was the RewritePath GatewayFilter but it allows only for rewriting the path part of the URI.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the fluent RouteLocatorBuilder API and the RewritePath GatewayFilter, which the issue identifies as the closest existing entry points. Determine how request details could reach upstream URI construction and define done as supporting request-dependent upstream URI components while preserving existing route behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend, networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.