当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Cloud中各组件超时总结

Spring Cloud中各组件超时总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

spring cloud是基于spring boot的一整套实现微服务的框架。他提供了微服务开发所需的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等组件。最重要的是,跟spring boot框架一起使用的话,会让你开发微服务架构的云服务非常好的方便。 spring cloud包含了非常多的子框架,其中,spring cloud netflix是其中一套框架,由netflix开发后来又并入spring cloud大家庭,它主要提供的模块包括:服务发现、断路器和监控、智能路由、客户端负载均衡等。

本文将给大家介绍spring cloud各组件超时的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

ribbon的超时

全局设置:

ribbon:
readtimeout: 60000
connecttimeout: 60000

局部设置:

service-id:
ribbon:
readtimeout: 1000
connecttimeout: 1000

其中,service-id 是ribbon所使用的虚拟主机名,一般和eureka server上注册的服务名称一致,即:与spring.application.name 一致。

feign的超时

从spring cloud edgware开始,feign支持使用属性配置超时:

feign:
 client:
 config:
  feignname:
  connecttimeout: 5000
  readtimeout: 5000

对于老版本,可以写个feign.request.options ,参考:org.springframework.cloud.netflix.feign.ribbon.feignribbonclientautoconfiguration#feignrequestoptions 的写法即可。

resttemplate的超时

一些时,我们可能使用了resttemplate,例如

@bean
@loadbalanced
public resttemplate resttemplate() {
return new resttemplate();
}

此时,超时可使用如下方式设置:

@bean
@loadbalanced
public resttemplate resttemplate() {
simpleclienthttprequestfactory simpleclienthttprequestfactory = new simpleclienthttprequestfactory();
simpleclienthttprequestfactory.setconnecttimeout(1000);
simpleclienthttprequestfactory.setreadtimeout(1000);
return new resttemplate(simpleclienthttprequestfactory);
}

zuul的超时

zuul的超时比较复杂,因为zuul整合了ribbon、hystrix。下面分两种情况讨论:

如果zuul的路由使用了ribbon

那么:zuul的超时则与ribbon、hystrix相关,此时zuul的超时可以配置类似如下:

hystrix:
 command:
 default:
  execution:
  isolation:
   thread:
   timeoutinmilliseconds: 1000
ribbon:
 readtimeout: 1000
 connecttimeout: 1000

代码解析:此种情况下,zuul转发所使用的过滤器是org.springframework.cloud.netflix.zuul.filters.route.ribbonroutingfilter ,在这个过滤器中,整合了hystrix以及ribbon。

如果zuul的路由未使用ribbon

例如:zuul的路由配置如下:

zuul:
 routes:
 user-route:     # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
  url: http://localhost:8000/ # 指定的url
  path: /user/**    # url对应的路径。

那么,此时zuul的超时只与如下两个配置有关:

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

代码解析:直接配置url路由的方式,用不上ribbon,也用不上hystrix,zuul转发所使用的过滤器是org.springframework.cloud.netflix.zuul.filters.route.simplehostroutingfilter ,在这个过滤器中,zuul使用apache httpclient进行转发。

在现实场景中,有时候可能两种路由方式配合使用,因此,建议大家配置以上所有属性。

hystrix的超时

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

如上,hystrix的默认超时时间是1秒。默认开启超时机制。如需关闭hystrix的超时,可将xxx.enabled设置为false。

tips

如有组件跟hystrix配合使用,一般来讲,建议hystrix的超时 > 其他组件的超时,否则将可能导致重试特性失效。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网