spring cloud hystrix

蔚落 2021-11-29 09:00 307阅读 0赞

hystrix起服务器熔断作用。

具有那几个作用

  • 服务降级
  • 依赖隔离
  • 服务熔断
  • 监控等功能

服务降级
优先核心服务,非核心服务不可用或弱用
通过HystrixCommand注解指定
fallbackMethod(回退函数)具体实现降级逻辑

使用

1.先引入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. </dependency>

2.在启动类上加上注解

  1. @EnableCircuitBreaker

3.启动超时配置

  1. //超时配置
  2. @HystrixCommand(commandProperties = {
  3. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
  4. })

4.在类上指定,熔断后的回调函数

  1. @DefaultProperties(defaultFallback = "defaultFallback")

编写方法

  1. private String defaultFallback() {
  2. return "默认提示:太拥挤了, 请稍后再试~~";
  3. }

5.或者设置熔断

  1. @HystrixCommand(commandProperties = {
  2. @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //设置熔断
  3. @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //请求数达到后才计算
  4. @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠时间窗
  5. @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"), //错误率 })

6.使用

  1. hystrix-dashboard监控

1.引入

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  4. </dependency>

2.配置文件配置一下

  1. management:
  2. context-path: /

3.直接访问localhost:8080/hystrix即可到达界面

填入,项目配置和项目名即可。

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring cloud hystrix

    hystrix起服务器熔断作用。 具有那几个作用 服务降级 依赖隔离 服务熔断 监控等功能 服务降级 优先核心服务,非核心服务不可用或弱用