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

Make route.uri aware of uri variables

Open
#1,232 7 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

Hello guys,
Im trying to use less code for RouteBuilder, but cant see any options for my problem.
So here is the code:
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(
p -> p.path("/api/register/**")
.filters(f ->
f.rewritePath(
"/api/register/(?.*)",
"/${segment}")
)
.uri("lb://erp-register-service")
).build();

The question is: Can I somehow take name of the microservice from the path and
use it in the url(with regex or something , like the rewritePath)??
My idea :
return builder.routes()
.route(
p -> p.path("/api/ ?nameOfMicroservice/**")
.filters(f ->
f.rewritePath(
"/api/register/(?.*)",
"/${segment}")
)
.uri("lb://erp- ?nameOfMicroservice -service")
).build();
Check "?nameOfMicroservice" in path , to go down somehow in the uri, so i can redirect to any microservice
with just 1 RouteBuilder :))

Thanks.

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 tracing the RouteLocatorBuilder and RouteBuilder APIs used in the example, especially path matching, rewritePath, and uri handling. Define how a microservice name captured from the route path should reach the load-balanced URI, then verify the behavior with the gateway's existing route tests if available.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.