GitHub repository
- GitHub repository to reproduce the issue: spring-cloud-feign-hystrix-timeout-problem
- StackOverflow question: Spring Cloud Feign client with Hystrix circuit-breaker timeout defaults in 2 seconds
The application use neither Zuul nor Eureka. The main class is annotated with the following annotations
@EnableFeignClients
@EnableCircuitBreaker
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Used dependencies on top of Spring Boot 2.3.1.RELEASE and Spring Cloud Hoxton.SR6:
- org.springframework.boot:spring-boot-starter
- org.springframework.boot:spring-boot-starter-web
- org.springframework.cloud:spring-cloud-starter-openfeign
- org.springframework.cloud:spring-cloud-starter-netflix-hystrix
Used plugin on top of of Spring Boot 2.3.1.RELEASE:
- org.springframework.boot:spring-boot-maven-plugin
Problem
The configured Feign client and Hystrix on unavailable endpoint falls correctly to fallback, however too early, after 2 seconds although the configuration (application.yml) says different:
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
hystrix:
enabled: true
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 60000
How to reproduce
- Make sure nothing runs on
localhost:8080(this application) andlocalhost:9090( used as a "unavailable service") - Run the Spring Boot application
org.example.Application. It starts on the default port8080. - Call the following CURL:
curl --request GET --url http://localhost:8080/feign/api/dto
The result might be the following log which clearly shows that the fallback was triggered too way early in 2 seconds however it is configured to 5.
Calling feign clientis logged right after calling the CURL invokingMyRestController::get.Fallbackis logged right after the request falls into fallbackMyFallbackFactory::create.
2020-08-10 19:19:04.254 INFO 15368 --- [nio-8080-exec-2] org.example.MyRestController : Calling feign client
2020-08-10 19:19:06.276 INFO 15368 --- [trix-my-feign-2] org.example.MyFallbackFactory : Fallback
2020-08-10 19:19:06.277 INFO 15368 --- [trix-my-feign-2] org.example.MyFallbackFactory : Fallback caught FeignException, no status
2020-08-10 19:19:06.287 ERROR 15368 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.netflix.hystrix.exception.HystrixRuntimeException: MyFeignClient#getDto() failed and fallback failed.] with root cause
java.net.ConnectException: Connection refused: connect
at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method) ~[na:na]
at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
...
Comment From: OlgaMaciaszek
I have checked your demo. As the accepted answer in SO suggests, the call fails on a connection refused (simply, there is nothing running at that port) as opposed to a timeout (that would be caused by the service running on that port but not being responsive). So, as it does not fail on a timeout, the timeout settings are not used here.
Comment From: Nikolas-Charalambidis
Thanks for the comment. The explanation of the accepted answer is sufficient and implied the documented behavior of Openfeign with Hystrix is not clear in this case and would be fine to describe it a bit for further users.
As long as this is not clear from the documentation:
- https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#spring-cloud-feign-overriding-defaults
- https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-overriding-defaults
... I suggest the following:
- Document the real behavior of the
feign.client.config.default.connectTimeoutproperty that is not related to the connecting to the server itself and the difference fromfeign.client.config.default.readTimeout. - Explain the origin of 2 seconds timeout.
Also the quesiton is: Do you take care after Spring documentation or do you at least participate on its creation and maintenance?
Comment From: OlgaMaciaszek
@Nikolas-Charalambidis would you like to submit a PR?
Comment From: Nikolas-Charalambidis
@OlgaMaciaszek Yeah, I'd like to. As long as I am new to this repository would you navigate me where to start looking for the relevant documentation sources?
Comment From: OlgaMaciaszek
Sure @Nikolas-Charalambidis This is the file that you should change. Look for the Feign Hystrix Support version. Please do the changes against the 2.2.x branch and not master.