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 includes 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 timeouts, 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: 60000 ConnectTimeout: 60000
Local settings:
service-id: ribbon: ReadTimeout: 1000 ConnectTimeout: 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:Consistent.
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 one,refer to:
#feignRequestOptions
Just write it.
RestTemplate timeout
Some times we may use RestTemplate, e.g.
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
At this time, the timeout can be set in the following way:
@Bean @LoadBalanced public RestTemplate restTemplate() { SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); (1000); (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: 1000 ribbon: ReadTimeout: 1000 ConnectTimeout: 1000
Code analysis: In this case, the filter used for Zuul forwarding is, In this filter, Hystrix and Ribbon are integrated.
If Zuul's route is not using Ribbon
For example: Zuul's routing configuration is as follows:
zuul: routes: user-route: # In this configuration, user-route just gives the route a name and can be named at will. url: http://localhost:8000/ # specified url path: /user/** # urlCorresponding path。
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 method of directly configuring URL routing cannot be used, nor can it use Hystrix. The filter used for Zuul forwarding is, In this filter, Zuul uses Apache HttpClient for forwarding.
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, you canSet to false.
Tips
If a component is used with Hystrix, it is generally recommended that Hystrix timeout > timeout of other components, 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.