Introduction
In this tutorial, we’ll discuss configuring Http timeout in application. Http timeouts (response and connect) can be configured for all routes and overridden for each specific route.
In the context of HTTP timeouts:
- Connect Timeout: This is the maximum time the client will wait to establish a connection with the server (you can say another microservice for understanding). If the connection is not established within this time, the request fails with a timeout error.
- Response Timeout (also called Read Timeout): This is the maximum time the client will wait for a response after sending the request. If the server takes longer than this duration to respond, the request fails with a timeout error.
Global timeouts
To configure Global http timeouts:
connect-timeout
must be specified in milliseconds.response-timeout
must be specified as a java.time.Duration
global http timeouts example
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
spring: cloud: gateway: httpclient: connect-timeout: 1000 response-timeout: 5s
Per-route timeouts
To configure per-route timeouts:connect-timeout
must be specified in milliseconds.response-timeout
must be specified in milliseconds.
per-route http timeouts configuration via configuration
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200
- id: per_route_timeouts uri: https://example.org predicates: - name: Path args: pattern: /delay/{timeout} metadata: response-timeout: 200 connect-timeout: 200
per-route timeouts configuration using Java DSL
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){
return routeBuilder.routes()
.route("test1", r -> {
return r.host("*.somehost.org").and().path("/somepath")
.filters(f -> f.addRequestHeader("header1", "header-value-1"))
.uri("http://someuri")
.metadata(RESPONSE_TIMEOUT_ATTR, 200)
.metadata(CONNECT_TIMEOUT_ATTR, 200);
})
.build();
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){
return routeBuilder.routes()
.route("test1", r -> {
return r.host("*.somehost.org").and().path("/somepath")
.filters(f -> f.addRequestHeader("header1", "header-value-1"))
.uri("http://someuri")
.metadata(RESPONSE_TIMEOUT_ATTR, 200)
.metadata(CONNECT_TIMEOUT_ATTR, 200);
})
.build();
}
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder routeBuilder){ return routeBuilder.routes() .route("test1", r -> { return r.host("*.somehost.org").and().path("/somepath") .filters(f -> f.addRequestHeader("header1", "header-value-1")) .uri("http://someuri") .metadata(RESPONSE_TIMEOUT_ATTR, 200) .metadata(CONNECT_TIMEOUT_ATTR, 200); }) .build(); }
A per-route response-timeout
with a negative value will disable the global response-timeout
value.
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: -1
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: -1
- id: per_route_timeouts uri: https://example.org predicates: - name: Path args: pattern: /delay/{timeout} metadata: response-timeout: -1