Preface
Spring Cloud is a complete framework for implementing microservices based on Spring Boot. It provides the components required for microservice development such as configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control bus, global locks, decision campaigns, distributed sessions and cluster state management. The most important thing is that if you use it with the spring boot framework, it will make you very convenient to develop cloud services with microservice architecture. Spring Cloud contains a lot of subframes, among which Spring Cloud Netflix is one of the frameworks developed by Netflix and later incorporated into the Spring Cloud family. Its main modules include: service discovery, circuit breakers and monitoring, intelligent routing, client load balancing, etc.
This article will introduce to you the relevant content of Spring Cloud's components timeout, and share it for your reference and learning. I won't say much below, let's take a look at the detailed introduction together.
Ribbon's timeout
Global settings:
ribbon:ReadTimeout: 60000ConnectTimeout: 60000
Local settings:
service-id:ribbon:ReadTimeout: 1000ConnectTimeout: 1000
Among them, service-id is the virtual host name used by Ribbon, which is generally the same as the service name registered on Eureka Server, that is, it is the same as spring.application.name .
Feign's timeout
Starting with Spring Cloud Edgware, Feign supports configuring timeouts with properties:
feign: client: config: feignName: connectTimeout: 5000 readTimeout: 5000
For the old version, you can write feign.Request.Options , refer to: org.springframework.cloud.netflix.feign.ribbon.FeignRibbonClientAutoConfiguration#feignRequestOptions writing method.
RestTemplate timeout
Some times, we may use RestTemplate, e.g.
@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}At this time, the timeout can be set in the following way:
@Bean@LoadBalancedpublic RestTemplate restTemplate() {SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();simpleClientHttpRequestFactory.setConnectTimeout(1000);simpleClientHttpRequestFactory.setReadTimeout(1000);return new RestTemplate(simpleClientHttpRequestFactory);}Zuul's timeout
Zuul's timeout is more complicated because Zuul integrates Ribbon and Hystrix. The following are two situations:
If Zuul's route uses Ribbon
Then: Zuul's timeout is related to Ribbon and Hystrix. At this time, Zuul's timeout can be configured similarly to the following:
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1000ribbon: ReadTimeout: 1000 ConnectTimeout: 1000
Code analysis: In this case, the filter used for Zuul forwarding is org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter , which integrates Hystrix and Ribbon.
If Zuul's route is not using Ribbon
For example: Zuul's routing configuration is as follows:
zuul: routes: user-route: # In this configuration method, user-route just gives the route a name and can be named at will. url: http://localhost:8000/ # Specified url path: /user/** # The path corresponding to url.
Then, the timeout of Zuul at this time is only related to the following two configurations:
zuul: host: socket-timeout-millis: 10000 connect-timeout-millis: 2000
Code analysis: The way to directly configure URL routing is not used for Ribbon or Hystrix. The filter used for Zuul forwarding is org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter . In this filter, Zuul uses Apache HttpClient to forward.
In real scenarios, sometimes the two routing methods may be used in combination, so it is recommended that you configure all the above attributes.
Hystrix timeout
hystrix: command: default: execution: timeout: enabled: true isolation: thread: timeoutInMilliseconds: 1000
As above, the default timeout for Hystrix is 1 second. The timeout mechanism is enabled by default. To turn off the timeout of Hystrix, set xxx.enabled to false.
Tips
If a component is used with Hystrix, it is generally recommended that Hystrix timeout > other components timeout, otherwise it may cause the retry feature to fail.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.