woshikid / blog

Apache License 2.0
8 stars 1 forks source link

Hystrix学习笔记 #171

Open woshikid opened 2 years ago

woshikid commented 2 years ago

POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

默认配置

hystrix:
  command:
    default:
      execution:
        timeout.enabled: true
        isolation:
          strategy: THREAD # 默认线程池隔离
          thread:
            timeoutInMilliseconds: 1000 # 默认超时时间
          semaphore:
            maxConcurrentRequests: 10 # 默认并发执行数
      circuitBreaker:
        enabled: true
        requestVolumeThreshold: 20 # 熔断最小请求数(10秒内)
        sleepWindowInMilliseconds: 5000 # 熔断时间(到时间后半开尝试1个请求)
        errorThresholdPercentage: 50 # 熔断错误率
      fallback:
        enabled: true
        isolation.semaphore.maxConcurrentRequests: 10 # 默认降级并发数
  threadpool:
    default:
      coreSize: 10 # 默认线程池大小
      maxQueueSize: -1 # 默认使用SynchronousQueue
  collapser:
    default:
      timerDelayInMilliseconds: 10 # 合并请求的时间窗口
      maxRequestsInBatch: 0x7fffffff # 单批最大请求数

启用Hystrix

@EnableHystrix // @EnableCircuitBreaker

请求降级/熔断/隔离/超时

@DefaultProperties(defaultFallback = "fallback", ignoreExceptions = BusinessException.class) // 类 // defaultFallback方法不能有参数
@HystrixCommand // 方法

@HystrixCommand(fallbackMethod = "fallback") // fallback方法参数与返回值类型需与原方法一致(可追加一个Throwable参数)
@HystrixCommand(fallbackMethod = "fallback",
    commandProperties = {
        @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"), // 使用信号量隔离
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"), // 设置超时时间
        //@HystrixProperty(name = "circuitBreaker.forceOpen", value = "true") // 设置强制熔断
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "1") // 设置熔断最小请求数(10秒内)
    }
)

请求合并

@HystrixCollapser(batchMethod = "testBatch", scope = Scope.GLOBAL,
    collapserProperties = {
        @HystrixProperty(name = "timerDelayInMilliseconds", value = "1000"), // 设置合并请求的时间窗口
        @HystrixProperty(name = "maxRequestsInBatch", value = "100") // 设置单批最大请求数
    }
)
public String test(String arg) { // 支持返回Future<String>
    return null; // 不会执行方法体
}

@HystrixCommand(fallbackMethod = "testFallback")
public List<String> testBatch(List<String> args) {
    return List.of("1", "2", "3");
}

public List<String> testFallback(List<String> args, Throwable e) {
    return args.stream().map(v -> "").toList();
}

请求缓存(仅在同一请求中有效)

// 初始化缓存
HystrixRequestContext.initializeContext();

// 使用缓存
@CacheResult // 需配合@HystrixCommand
@CacheResult(cacheKeyMethod = "cacheKey") // 或使用@CacheKey

// 清除缓存
@CacheRemove(commandKey = "test") // 需配合@HystrixCommand
//HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("test"), HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(arg));

Hystrix Dashboard

POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

启用Hystrix Dashboard

@EnableHystrixDashboard

暴露端点

management.endpoints.web.exposure.include: "*"

设置白名单

hystrix.dashboard.proxy-stream-allow-list: "*"

地址:http://localhost:8080/hystrix stream:http://localhost:8080/actuator/hystrix.stream

Turbine(依赖注册中心)

POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

启用Turbine

@EnableTurbine

按app查看

turbine:
  app-config: test,test2 # 需要监控的app
  aggregator.cluster-config: TEST,TEST2 # 大写app名

stream:http://localhost:8080/turbine.stream?cluster=TEST

聚合查看

turbine:
  app-config: test,test2 # 需要监控的app
  cluster-name-expression: "'default'"

stream:http://localhost:8080/turbine.stream