Component timeout summary in Spring Cloud

  • 2020-11-26 18:49:32
  • OfStack

preface

Spring Cloud is a microservice implementation framework based on Spring Boot. It provides configuration management, service discovery, circuit breaker, intelligent routing, microagent, control bus, global locking, decision campaign, distributed session, and cluster state management components for microservice development. On top of that, using the spring boot framework 1 will make it very easy for you to develop cloud services for microservice architectures. Spring Cloud contains many sub-frameworks, among which Spring Cloud Netflix is one set of frameworks developed by Netflix and later incorporated into Spring Cloud family. It mainly provides modules including: service discovery, circuit breaker and monitoring, intelligent routing, client load balancing and so on.

This article will introduce Spring Cloud components timeout related content, share for your reference and study, the following words do not say much, let's start with a detailed introduction.

Ribbon timeout

Global Settings:


ribbon:
ReadTimeout: 60000
ConnectTimeout: 60000

Local Settings:


service-id:
ribbon:
ReadTimeout: 1000
ConnectTimeout: 1000

Among them, ES32en-ES33en is the virtual host name used by Ribbon, 1 general and the service name registered on Eureka Server 1 sent, namely: and spring.application.name 1.

Feign timeout

Starting with Spring Cloud Edgware, Feign supports configuring timeouts using properties:


feign:
 client:
 config:
  feignName:
  connectTimeout: 5000
  readTimeout: 5000

For the old version, you can write one feign.Request.Options Reference: org.springframework.cloud.netflix.feign.ribbon.FeignRibbonClientAutoConfiguration#feignRequestOptions You can just write it this way.

RestTemplate timeout

1 For some time, we may have used RestTemplate, for example


@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}

At this point, the timeout can be set as follows:


@Bean
@LoadBalanced
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
simpleClientHttpRequestFactory.setConnectTimeout(1000);
simpleClientHttpRequestFactory.setReadTimeout(1000);
return new RestTemplate(simpleClientHttpRequestFactory);
}

Zuul timeout

The timeout of Zuul is complicated because Zuul integrates Ribbon and Hystrix. The following two cases are discussed:

If Zuul is routed using Ribbon

Then the timeout of Zuul is related to Ribbon and Hystrix, and the timeout of Zuul can be configured as follows:


hystrix:
 command:
 default:
  execution:
  isolation:
   thread:
   timeoutInMilliseconds: 1000
ribbon:
 ReadTimeout: 1000
 ConnectTimeout: 1000

Code resolution: In this case, the filter used by Zuul forwarding is org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter In this filter, Hystrix and Ribbon are integrated.

If Zuul is not routed using Ribbon

For example, the routing configuration for Zuul is as follows:


zuul:
 routes:
 user-route:     #  In this configuration, user-route It's just routing 1 You can call it anything you want. 
  url: http://localhost:8000/ #  The specified url
  path: /user/**    # url The corresponding path. 

The timeout of Zuul at this point, then, is only related to the following two configurations:


zuul:
 host:
 socket-timeout-millis: 10000
 connect-timeout-millis: 2000

Code analysis: directly configure the URL route without using Ribbon or Hystrix. The filter used by Zuul forwarding is org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter In this filter, Zuul forwards using Apache HttpClient.

In a real-world scenario, it is sometimes possible to use both routes together, so it is recommended that you configure all of the above properties.

Hystrix timeout


hystrix:
 command:
 default:
  execution:
  timeout:
   enabled: true
  isolation:
   thread:
   timeoutInMilliseconds: 1000

As mentioned, the default timeout for Hystrix is 1 second. Timeout is enabled by default. If you want to turn off the timeout for Hystrix, you can use the xxx.enabled Set to false.

Tips

In general, Hystrix timeouts are recommended if components are used with Hystrix > Timeout for other components, otherwise it may invalidate the retry feature.

conclusion


Related articles: