首页  

Feign Ribbon Hystrix     所属分类 springcloud 浏览量 761
Fegin   声明式调用 http 客户端
Ribbon  客户端负载均衡       
Hystrix 断路器         

Fegin
为接口添加注解 Feign注解 或 JAX-RS注解
支持热插拔的编码器和解码器
SpringCloud为Feign添加 SpringMVC 注解支持,
并整合 Ribbon 和 Eureka 实现负载均衡


api请求 > Fegin > Hystrix > ribbon > feignHttpClient > server

ribbon > eureka (获取服务列表)

Fegin 可关闭断路保护

Feign 支持多种Http客户端
Apache-HttpClient   OkHttp  HttpURLConnection

feign.httpclient.enabled=true
# 默认false
feign.hystrix.enabled=true
feign.client.config.default.connectTimeout=5000 
feign.client.config.default.readTimeout=5000  


ribbon.eager-load.enabled=true
#同一实例最大自动重试次数,默认1,不包括首次
ribbon.MaxAutoRetries=1
# 要重试的下一个实例的最大数量,默认1,不包括第一次被调用的实例
ribbon.MaxAutoRetriesNextServer=1
# 是否所有的操作都重试,默认true
ribbon.OkToRetryOnAllOperations=true
# 从注册中心刷新服务器列表信息时间间隔,默认2000毫秒 
ribbon.ServerListRefreshInterval=2000
# 连接超时 毫秒
ribbon.ConnectTimeout=3000
# 读取超时 毫秒
ribbon.ReadTimeout=3000
  

# 默认true,false 不引入Hystrix
hystrix.circuitBreaker.enabled=true
# 启用熔断器功能窗口时间内的最小请求数 
hystrix.requestVolumeThreshold=20     
# 熔断器打开后多长时间内允许一次请求尝试执行
hystrix.sleepWindowInMilliseconds=5000  
# 窗口时间内超过50%的请求失败后就会打开熔断器将后续请求快速失败掉
hystrix.errorThresholdPercentage=50    

hystrix.propagate.request-attribute.enabled=true
hystrix.command.default.execution.timeout.enabled=true
# 隔离策略 线程池 threadPool   信号量 semaphore
hystrix.command.default.execution.isolation.strategy=threadPool
# 方式执行超时 默认 1000毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
# 发生超时时是否中断方法执行
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 
hystrix.command.default.execution.isolation.thread.interruptOnCancel=false



一般 Ribbon 超时时间 应小于Hystrix 超时时间
否则会有warn信息
The Hystrix timeout of 1000ms   for the command operation is set lower than the combination of the Ribbon   read and connect timeout, 2000ms.

Hystrix超时时间 = Ribbon的重试次数(含首次) * (ribbon.ReadTimeout + ribbon.ConnectTimeout)
  
Ribbon重试次数(含首次) = 1 + ribbon.MaxAutoRetries  +  ribbon.MaxAutoRetriesNextServer  +  (ribbon.MaxAutoRetries * ribbon.MaxAutoRetriesNextServer)

如果不启用Hystrix,Feign的超时时间就是Ribbon的超时时间,Feign自身的配置会被覆盖

上一篇     下一篇
写代码的几个好习惯

redis-cli 使用

SpringCloud全家桶简介

eureka工作原理简介

Eureka源码要点

Hystrix实例