Hystrix断路器和服务降级

Hystrix断路器

是一种保护机制。Hystrix也是Netflix公司的一款组件。
在这里插入图片描述

Hystrix的几个重要概念

(1)服务降级:
服务器忙碌或者网络拥堵时,不让客户端等待并立刻返回一个友好提示,
相当于当程序运行异常时 给一个友善的回应 给程序降一级
(2)服务熔断
相当于一层保险 ,当服务访问量到最大时 相当于一个最后底线

(3)服务限流
到了一定程度 线程有序排队 限制进入

Hystrix案例

在这里插入图片描述

导入pom

  <dependencies>
        <!-- hystrix -->

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

        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>com.jiahui</groupId>
            <artifactId>jiahui-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

编写配置文件老一套

server:
  port: 8001
spring:
  application:
    name: cloud-payment-service  #服务名称
eureka:
  client:
    # 注册进 Eureka 的服务中心
    register-with-eureka: true
    # 检索 服务中心 的其它服务
    fetch-registry: true
    service-url:
      # 设置与 Eureka Server 交互的地址
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

启动类

@SpringBootApplication
@DiscoveryClient
public class HystrixPaymentApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixPaymentApplication.class,args);
    }
}

编写业务代码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

服务降级

导入pom

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

我导入的时候一直报一个找不到依赖包 的错 后来我加入了版本号才好了
版本不一样可能就会报错
当你写完测试并发时 会报一个错
在这里插入图片描述

这个时候就用到了服务降级

  /**
     超时访问的方法
     */
    @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
            //设置峰值,超过 3 秒,就会调用兜底方法,这个时间也可以由feign控制
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
            
    })
     public String paymentInfo_Timeout(Integer id){
      //  int interTime = 5;
        int i = 10/0;
//        try {
//            TimeUnit.SECONDS.sleep(interTime);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
      //  return "线程池"+Thread.currentThread().getName()+ "  ,paymentInfo_Timeout,id:" + id + "耗时" + interTime + "秒钟";
    return "hello";
    }

    // 降级的逻辑
    public String timeoutHandler(Integer id){
        return "服务异常,请重试......";
    }

为了保险 启动类加上熔断

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients  //开启feign客户端
@EnableCircuitBreaker//开启服务熔断
public class HystrixOrderApplication80 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixOrderApplication80.class,args);
    }
}

启动测试
在这里插入图片描述
我们发现咱们项目有内部错误 或者是服务不可用了 都可以使用降级来处理
看着友善

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值