Matt Ke
Matt Ke

Reputation: 3739

How to configure request timeouts in Spring Cloud Gateway via code

I need to configure request timeouts in code for all routes. I know global timeouts can be configured via following properties in application.properties, but how can they be configured in code?

spring.cloud.gateway.httpclient.connect-timeout=1000
spring.cloud.gateway.httpclient.response-timeout=5s

I have looked at GatewayAutoConfiguration how timeouts are configured by default. HttpClientProperties holds both properties, however it cannot be overwritten.

@Bean
public HttpClientProperties httpClientProperties() {
    return new HttpClientProperties();
}

Can this be done in code?

Upvotes: 1

Views: 1911

Answers (1)

Matt Ke
Matt Ke

Reputation: 3739

I solved my problem. I created my own bean and used annotation @Primary to be able to create a separate bean with the same type. GatewayAutoConfiguration now uses my bean instead of the default bean.

@Bean
@Primary
public HttpClientProperties overwrittenHttpClientProperties() {
    HttpClientProperties p = new HttpClientProperties();
    p.setConnectTimeout(3000);
    p.setResponseTimeout(Duration.ofMillis(10000));
    return p;
}

Upvotes: 2

Related Questions