springCloud入门使用Hystrix

分布式系统面临的问题

复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败
服务雪崩
多个微服务时间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他微服务,这就是所谓的扇出,如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的雪崩效应。
对于高流浪的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障,这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖失败,不能取消整个应用程序或系统

服务降级

假设对方系统不可用了,你需要给我一个兜底的解决方案(向调用方返回一个符合预期的,可处理的备选响应)如:返回提示语,服务器忙,请稍后再试。
服务降级的情况
1.程序运行时异常
2.超时
3.服务熔断触发降级
4.线程池/信号量打满也会导致服务降级

服务熔断

类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示,通俗的来讲就是保险丝
服务熔断类型
1.熔断打开:
请求不在进行调用当前服务,内部设置时钟一般为MTTR(平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态
2.熔断关闭:
熔断关闭不会对服务进行熔断
3.熔断半开
部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断

服务限流

秒杀高并发等操作,严禁一窝蜂过来拥挤,大家排队,一秒钟n个,有序进行

服务降级实际使用

1.服务提供者端服务降级
1.1pom

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

1.2主启动类上加注解

@EnableCircuitBreaker //服务降级

1.3服务层

    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000") //表示这个线程的超时时间设定为3000毫秒
    }) //fallbackMethod:当被注解的方法出问题,就找到值中的方法
    public String paymentInfo_TimeOut(Integer id){
        int timeNum = 3;
        try {

            TimeUnit.SECONDS.sleep(timeNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        int age = 10/0;
        return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+" 耗时"+"秒钟";
    }

    public String paymentInfo_TimeOutHandler(Integer id){
        return "出错,线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOutHandler,id: "+id;
    }

当注解了@HystrixCommand 的方法抛出异常,就直接找到 fallbackMethod 值中的方法名的方法执行
2.服务消费者方服务降级
2.1pom
这里调用接口用到openfeign包,里面包含了hystrix

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.2修改application.yml
openfeign 开启 hystrix

feign:
  hystrix:
    enabled: true

2.3主启动类

@EnableFeignClients
@EnableHystrix

2.4controller
使用方法与上面一样

    @GetMapping(value="/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //表示这个线程的超时时间设定为3000毫秒
//    }) //fallbackMethod:当被注解的方法出问题,就找到值中的方法
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentInfo_TimeOut(id);
        return s;
    }
    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
        return "我是消费者,程序出错了";
    }

3.服务降级如何统一处理
3.1在类的上面加注解

@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")

3.2修改timeout方法
把原注解改成 @HystrixCommand 方法代码抛出异常就会跑到类注解的payment_Global_FallbackMethod方法里面去

    @GetMapping(value="/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //表示这个线程的超时时间设定为3000毫秒
//    }) //fallbackMethod:当被注解的方法出问题,就找到值中的方法
    @HystrixCommand //发生异常到异常处理方法
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentInfo_TimeOut(id);
        return s;
    }

    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
        return "我是消费者,程序出错了";
    }

    //下面是全局fallback方法
    public String payment_Global_FallbackMethod(){
        return "异常处理信息,请稍后再试";
    }

服务熔断实际使用

当满足一定阈值的时候(默认10秒内超过20个请求次数)
当失败率达到一定的时候(默认10秒内超过50%的请求失败)
当到达以上阈值,断路器将会开启
当开启的时候,所有请求都不会转发
一段时间之后(默认是5秒),这个时候断路器是半开状态,会让其中一个请求进行转发,如果成功,断路器会关闭,若失败继续开启

    //============服务熔断
    @HystrixCommand(fallbackMethod = "paymetCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name="circuitBreaker.enabled",value="true"),//是否开启断路器
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"),//请求次数
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"),//时间窗口期
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="60"),//失败率达到多少后跳闸
    })
    public String paymetCircuitBreaker(@PathVariable("id") Integer id){
        if (id < 0){
            throw new RuntimeException("*********id,不能负数");
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"调用成功,流水号"+serialNumber;
    }
    public String paymetCircuitBreaker_fallback(@PathVariable("id") Integer id){
        return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: "+id;
    }```
## 搭建图形化Dashboard
**1.建model**
**2.导入pom**

```yaml
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

3.启动类

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

4.被监控的模块需要

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

5.输入被监控端口
输入被监控的端口号
111
6.查看监控数据
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值