Spring Cloud及微服务(四):服务容错保护Spring Cloud Hystrix

版权声明:YETA https://blog.csdn.net/qq_28958301/article/details/88718415

出现背景

微服务架构中,每个服务都是一个单独的进程,有可能因为网络的原因或是服务自身出现问题出现调用故障或延迟,会导致对外服务也出现延迟,若此时调用方的请求不断增加,最后就会导致服务的瘫痪。

断路器模式

当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

  • 依赖

  • 方法

  • 启动类

同步请求和异步请求

  • user-service

  • 同步执行

  • 同步执行结果

  • 异步执行

  • 异步执行结果

响应式执行方式

服务降级

如”断路器模式-方法“所示。

使用注解定义服务降级逻辑时,必须将@HystrixCommand与fallback实现函数定义在同一个类中,并且fallbackMethod的值必须与实现fallback方法的名字相同,对于访问修饰符没有特定要求。

写操作、批处理或离线计算可以不实现降级逻辑。

异常处理

除了HystrixBadRequestException之外,其他异常均会被Hystrix认为命令执行失败并触发服务降级的处理逻辑。

命令名称、分组以及线程池划分

通过设置命名组,Hystrix会根据组来组织和统计命令的告警、仪表盘等信息。除了根据组能实现统计之外,Hystrix命令默认的线程划分也是根据命令分组来实现的。默认情况下,Hystrix会让相同组名的命令使用同一个线程池 ,所以需要在创建Hystrix命令时为其指定命令组名来实现默认的线程池划分。

请求缓存

开启请求缓存的好处:

  • 减少重复的请求数,降低依赖服务的并发度;
  • 在同一用户请求的上下文中,相同依赖服务的返回数据始终保持一致
  • 请求缓存在run()和construct()执行之前生效,所以可以有效减少不必要的线程开销。

请求缓存功能鸡肋,参考

https://blog.csdn.net/lvyuan1234/article/details/76691112

https://blog.csdn.net/lixiaxin200319/article/details/81131188

请求合并

请求合并功能鸡肋参考

https://blog.csdn.net/lixiaxin200319/article/details/81188019

https://www.jianshu.com/p/63f82a142342

https://stackoverflow.com/questions/49223609/hystrixcollapser-null-pointer-exception-on-collapsed-method

https://blog.csdn.net/xiao_jun_0820/article/details/78423985

属性详解

#设置HystrixCommand.run()执行的隔离策略,THREAD:通过线程池隔离的策略、SEMAPHORE:通过信号量隔离的策略
hystrix.command.default.execution.isolation.strategy=THREAD
#断路器的超时时间,需要大于Ribbon的超时时间,不然不会触发重试
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
#配置HystrixCommand.run()的执行是否启用超时时间,如果设置为false,那么上一个配置将不再起作用
hystrix.command.default.execution.timeout.enabled=true
#配置当HystrixCommand.run()执行超时的时候是否要将它中断
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
#配置当HystrixCommand.run()执行被取消的时候是否要将它中断
hystrix.command.default.execution.isolation.thread.interruptOnCancel=true
#当HystrixCommand的隔离策略使用信号量的时候,配置信号量的大小
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=10
#设置从调用线程中允许HystrixCommand.getFallback()方法执行的最大并发请求书
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=10
#设置服务降级策略是否启用
hystrix.command.default.fallback.enabled=true
#确定当服务请求命令失败时,是否使用断路器来跟踪其健康指标和熔断请求
hystrix.command.default.circuitBreaker.enabled=true
#设置在滚动时间窗中,断路器熔断的最小请求数
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
#设置当断路器打开之后的休眠时间窗
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
#设置断路器打开的错误百分比条件
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
#如果设置为true,断路器将强制进入“打开”状态,拒绝所有请求
hystrix.command.default.circuitBreaker.forceOpen=false
#如果设置为true,断路器将强制进入“关闭”状态,接收所有请求,优先级低于上一个配置
hystrix.command.default.circuitBreaker.forceClosed=false
#设置滚动时间窗的长度
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
#设置滚动时间窗统计指标信息时划分“桶”的数量
hystrix.command.default.metrics.rollingStats.numBuckets=10
#设置对命令执行的延迟是否使用百分位数来跟踪和计算
hystrix.command.default.metrics.rollingPercentile.enabled=true
#设置百分位统计的滚动窗口的持续时间
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000
#设置百分位统计滚动窗口中使用“桶”的数量
hystrix.command.default.metrics.rollingPercentile.numBuckets=6
#设置在执行过程中每个“桶”中保留的最大执行次数
hystrix.command.default.metrics.rollingPercentile.bucketSize=100
#设置采集影响断路器状态的健康快照的间隔等待时间
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500
#配置是否开启请求缓存
hystrix.command.default.requestCache.enabled=true
#设置HystrixCommand的执行和事件是否打印日志到HystrixRequestLog中
hystrix.command.default.requestLog.enabled=true
#设置一次请求合并批处理中允许的最大请求数
hystrix.collapser.default.maxRequestsInBatch=Integer.MAX_VALUE
#设置批处理过程中每个命令延迟的时间
hystrix.collapser.default.timerDelayInMilliseconds=10
#设置批处理过程中是否开启请求缓存
hystrix.collapser.default.requestCache.enabled=true
#设置执行命令线程池的核心线程数
hystrix.threadpool.default.coreSize=10
#设置线程池的最大队列大小,当设置为-1时,将使用SynchronousQueue实现的队列,否则将使用LinkedBlockingQueue实现的队列
hystrix.threadpool.default.maxQueueSize=-1
#为队列设置拒绝阈值
hystrix.threadpool.default.queueSizeRejectionThreshold=5
#设置滚动时间窗的长度
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000
#设置滚动时间窗被划分成“桶”的数量
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10

猜你喜欢

转载自blog.csdn.net/qq_28958301/article/details/88718415