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

custom HttpClient each route.

Open
#3,698 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Before I start, I will apologize for my terrible English and start.

I want to create and apply a custom HttpClient for each route. For example, the timeout setting is set to 10 seconds for route1 and the timeout setting is set to 30 seconds for route2.

Please refer to the code below as a solution to the function.
I defined a CustomNettyRoutingFilter that inherits NettyRoutingFilter and redefined the getHttpClient() function so that if there is a newly defined ttpClient in exchange.getAttributes(), it can be returned.

@Component
public class CustomNettyRoutingFilter extends NettyRoutingFilter {
    public CustomNettyRoutingFilter(HttpClient httpClient, ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider, HttpClientProperties properties) {
        super(httpClient, headersFiltersProvider, properties);
    }

    @Override
    protected HttpClient getHttpClient(Route route, ServerWebExchange exchange) {

        if (exchange.getAttributes(). containsKey("httpClient")) {
            return (HttpClient)exchange.getAttributes().get("httpClient");
        }

        return super.getHttpClient(route, exchange);
    }
}

and then used filters to create and inject the desired HTTP client for each route into exchange.getAttributes().
I want to know if the code I designed is on the right track.

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 with spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java and inspect how the route and ServerWebExchange are used. Evaluate the proposed CustomNettyRoutingFilter and exchange attribute approach against the gateway's existing filter behavior; done means a clear, documented conclusion about whether route-specific HttpClient selection is supported.

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.