深入浅出Spring Cloud整合hystrix

前言

Hystrix github

快速上手

第一步新增依赖:hystrix-core 或者 spring-cloud-starter-netflix-hystrix

<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core -->
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

第二步:

主启动类添加 @EnableHystrix
在这里插入图片描述
新增注解@HystrixCommand(defaultFallback = “fallback1”) 并指定降级方法。

    @HystrixCommand(defaultFallback = "fallback1")
    @GetMapping("b2")
    public String b2() {
        int a = 1 / 0;
        return "this is b2 ...";
    }

    public String fallback1() {
        return "this is fallback b...";
    }

可以看到成功返回降级方法内容
在这里插入图片描述

HystrixCommand

public @interface HystrixCommand {
  
    String groupKey() default "";
  
    String commandKey() default "";

    String threadPoolKey() default "";

    String fallbackMethod() default "";

    HystrixProperty[] commandProperties() default {};

    HystrixProperty[] threadPoolProperties() default {};

    Class<? extends Throwable>[] ignoreExceptions() default {};

    ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;

    HystrixException[] raiseHystrixExceptions() default {};

    String defaultFallback() default "";
}

指定等待时间

方法1s返回结果,超过500直接返回降级方法。

    @HystrixCommand(fallbackMethod = "fallback2", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    })
    @GetMapping("c")
    public String c() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "this is c ...";
    }


    public String fallback2() {
        return "this is fallback c...";
    }

hystrix-dashboard

github hystrix-dashboard

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>2.2.10.RELEASE</version>
        </dependency>

新增注解@EnableHystrixDashboard

在这里插入图片描述
访问路径 /hystrix

效果图
在这里插入图片描述
示例地址 https://localhost:8094/actuator/hystrix.stream

在这里插入图片描述

@EnableCircuitBreaker 和 @EnableHystrix 区别

在这里插入图片描述
在2.2.10版本中 EnableCircuitBreaker 被标记为过时的,且 EnableHystrix 和 EnableCircuitBreaker 作用是相同的,EnableHystrix 直接使用了 EnableCircuitBreaker 注解。

EnableCircuitBreaker

核心注解

@Import(EnableCircuitBreakerImportSelector.class)

在这里插入图片描述

知识点

节选自官网版本2.2.10.RELEASE

Spring Cloud Netflix

在这里插入图片描述

spring:
  cloud:
    circuitbreaker:
      hystrix:
        enabled: false

hystrix-dashboard

https://github.com/Netflix-Skunkworks/hystrix-dashboard

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值