3.2.3 Hystris - 服务容错机制

服务相互依赖

在这里插入图片描述

服务异常

在这里插入图片描述

系统雪崩

在这里插入图片描述

构建弹性应用的思路

在这里插入图片描述

Hystrix集成示例

在这里插入图片描述

Hystrix的核心知识

在这里插入图片描述

资源隔离

在这里插入图片描述

Hystrix的初始化流程

在这里插入图片描述

Hystrix核心执行流程

在这里插入图片描述

Hystrix与springcloud集成

在这里插入图片描述

Hystrix可视化监视

在这里插入图片描述

Hystrix使用

  1. 创建一个类继承HystrixCommand,重写fallback方法,详细。
package com.study.springcloudhystrix.command;

import com.netflix.hystrix.*;
import org.springframework.web.client.RestTemplate;

public class CustomerCommand extends HystrixCommand<Object> {

  private RestTemplate restTemplate;
    public CustomerCommand(RestTemplate restTemplate) {

        super(
       Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("study-hystrix"))
               .andCommandKey(HystrixCommandKey.Factory.asKey("CustomerController"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("studyThreadPool"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                              .withExecutionTimeoutInMilliseconds(100)
                                               .withCircuitBreakerSleepWindowInMilliseconds(5000))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                                                 .withCoreSize(1)
                                                 .withMaxQueueSize(2))
        );
        this.restTemplate = restTemplate;
    }

    @Override
    protected Object run() throws Exception {
     //核心实现,调用我们期望调用的方法.
        System.out.println("当前线程是: "+Thread.currentThread().getName());
      Object result = restTemplate.getForObject("http://helloserver",String.class,"");
      return  result;
    }

    @Override
    protected Object getFallback() {
        //核心方法,降级之后会来实现这个
        System.out.println("降级啦。。。");
        return "降级了,返回降级";
    }
}
  1. 配置文件config.properties的方式
# Hystrix 默认加载的配置文件 - 限流、 熔断示例

# 线程池大小
hystrix.threadpool.default.coreSize=1
# 缓冲区大小, 如果为-1,则不缓冲,直接进行降级 fallback
hystrix.threadpool.default.maxQueueSize=200
# 缓冲区大小超限的阈值,超限就直接降级
hystrix.threadpool.default.queueSizeRejectionThreshold=2

# 执行策略
# 资源隔离模式,默认thread。 还有一种叫信号量
hystrix.command.default.execution.isolation.strategy=THREAD
# 是否打开超时
hystrix.command.default.execution.timeout.enabled=true
# 超时时间,默认1000毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
# 超时时中断线程
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 取消时候中断线程
hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=false
# 信号量模式下,最大并发量
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=2

# 降级策略
# 是否开启服务降级
hystrix.command.default.fallback.enabled=true
# fallback执行并发量
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=100

# 熔断策略
# 启用/禁用熔断机制
hystrix.command.default.circuitBreaker.enabled=true
# 强制开启熔断
hystrix.command.default.circuitBreaker.forceOpen=false
# 强制关闭熔断
hystrix.command.default.circuitBreaker.forceClosed=false
# 前提条件,一定时间内发起一定数量的请求。  也就是5秒钟内(这个5秒对应下面的滚动窗口长度)至少请求3次,熔断器才发挥起作用。总数  默认20
hystrix.command.default.circuitBreaker.requestVolumeThreshold=3
# 错误百分比。达到或超过这个百分比,熔断器打开。  比如:5秒内有100请求,60个请求超时或者失败,就会自动开启熔断
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
# 10秒后,进入半打开状态(熔断开启,间隔一段时间后,会让一部分的命令去请求服务提供者,如果结果依旧是失败,则又会进入熔断状态,如果成功,就关闭熔断)。 默认5秒
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=10000


# 度量策略
# 5秒为一次统计周期,术语描述:滚动窗口的长度为5秒
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=5000
# 统计周期内 度量桶的数量,必须被timeInMilliseconds整除。作用:
hystrix.command.default.metrics.rollingStats.numBuckets=10
# 是否收集执行时间,并计算各个时间段的百分比
hystrix.command.default.metrics.rollingPercentile.enabled=true
# 设置执行时间统计周期为多久,用来计算百分比
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000
# 执行时间统计周期内,每个度量桶最多统计多少条记录。设置为50,有100次请求,则只会统计最近的50次
hystrix.command.default.metrics.rollingPercentile.bucketSize=100
# 数据取样时间间隔
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500

# 设置是否缓存请求,request-scope内缓存
hystrix.command.default.requestCache.enabled=false
# 设置HystrixCommand执行和事件是否打印到HystrixRequestLog中
hystrix.command.default.requestLog.enabled=false


######DnUser-ThreadPool特定配置

# hystrix.threadpool.DnUser-ThreadPool.coreSize=20
# hystrix.threadpool.DnUser-ThreadPool.maxQueueSize=1000
# 超过就报错
# hystrix.threadpool.DnUser-ThreadPool.queueSizeRejectionThreshold=800
  1. 基于注解的方式

在启动入口添加注解 @EnableCircuitBreaker

@HystrixCommand(fallbackMethod = "callTimeoutFallback",
      threadPoolProperties = {
             @HystrixProperty(name="coreSize",value = "1"),
              @HystrixProperty(name="queueSizeRejectionThreshold",value = "1")
      },
     commandProperties = {
             @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "100")
     })
     @GetMapping("index2")
     public Object getIndex2(){
        return restTemplate.getForObject("http://helloserver",String.class,"");
     }

      public Object callTimeoutFallback(){
          return  "请求index2降级";
       }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章