记完注册中心和服务间通信调用,该看看微服务间通信的异常情况处理了(微服务宕了咋办,微服务故障(通信阻塞啥的)导致雪崩咋办),耐非出了一个hystrix(容错器),防服务雪崩神器(丑话说在前头,它现在也不维护了!~),组件概念:

3.PNG

这块涉及的概念很多,老师的笔记又总结的很全,我真想啪啪啪直接贴截图,想了想还是算了,还是手冲吧😂

hystrix 用来保护微服务系统,实现服务降级 、服务熔断,防止服务雪崩。那么要先说说服务雪崩。

服务雪崩

4.PNG

一个名词:级联服务故障

服务熔断

5.PNG

熔断是在服务链路过程出现问题的处理机制,本质也是降级的一种(把有问题的服务关一阵,没问题后再释放)

服务降级

6.PNG

服务降级是出于系统整体负荷产生的处理机制,关闭边缘服务(我好像确实有印象双11当夜确认收货点不了)

服务熔断和服务降级异同点总结
# 1.共同点
- 目的很一致,都是从可用性可靠性着想,为防止系统的整体缓慢甚至崩溃,采用的技术手段;
- 最终表现类似,对于两者来说,最终让用户体验到的是某些功能暂时不可达或不可用;
- 粒度一般都是服务级别,当然,业界也有不少更细粒度的做法,比如做到数据持久层(允许查询,不允许增删改);
- 自治性要求很高,熔断模式一般都是服务基于策略的自动触发,降级虽说可人工干预,但在微服务架构下,完全靠人显然不可能,开关预置、配置中心都是必要手段;sentinel

# 2.异同点
- 触发原因不太一样,服务熔断一般是某个服务(下游服务)故障引起,而服务降级一般是从整体负荷考虑;
- 管理目标的层次不太一样,熔断其实是一个框架级的处理,每个微服务都需要(无层级之分),而降级一般需要对业务有层级之分(比如降级一般是从最外围服务边缘服务开始)

# 3.总结
- 熔断必会触发降级,所以熔断也是降级一种,区别在于熔断是对调用链路的保护,而降级是对系统过载的一种保护处理
服务熔断的实现

熟悉的感觉,引依赖、启动类加注解

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

@EnableCircuitBreaker    //开启hystrix服务熔断

来一个demo:

package com.jin.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jinyunlong
 * @date 2021/7/15 11:25
 * @profession ICBC锅炉房保安
 */
@RestController
public class DemoController {
    @GetMapping("/demo")   //?id=
    @HystrixCommand(fallbackMethod = "demoFallBack")  //熔断之后处理 fallbackMethod 书写快速失败方法名  这是对服务失败率和失败次数的处理(此时服务还正常),还应该加一个整个服务不可用后的处理
    public String demo(@RequestParam Integer id){
        System.out.println("demo ok");
        if(id<=0){
            throw new RuntimeException("无效id");
        }
        return "demo ok";
    }

    public String demoFallBack(Integer id){
        return "当前活动过于火爆,服务已经被熔断了。";
    }

}

口述一下这个demo的处理机制:首先id为正数就走正常流程,为负数的话,因为加了@HystrixCommand熔断注解,所以送负数就会抛出异常从而进入熔断的方法,返回火爆那一段。然后,如果在10s内失败了很多次(有阈值),那么下一次的请求就算正常,也会进入熔断方法,这时该微服务处于熔断器开启状态。然后根据熔断的处理机制,每隔5s再向这个微服务发请求(此时熔断器处于半开状态),正常的话,还原回正常(熔断器关闭状态),失败的话,熔断器继续处于开启状态。

7.PNG

可以说相当口是心非了,还是贴图了,不过既然是好东西,那就得借鉴!

在openFeign中使用Hystrix

需要引依赖(不引也行,因为openFeign中带了Hystrix),然后在配置文件中开启hystrix支持,启动类添加注解

@EnableCircuitBreaker
@EnableFeignClients     //开启OpenFeign调用

 <!--即使openfeign有了,也最好再引一下,虽然可能有重复依赖的问题,但是我只引入openfeign启动时提示hystrix -->
        <!--Caused by: java.lang.ClassNotFoundException:com.netflix.hystrix.contrib.javanica.aop.aspectj.Hystrix-->
        <!--引入hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

#开启openfeign在调用服务过程中 开启hystrix支持 默认:就是没有开启
feign.hystrix.enabled=true

然后声明接口,然后如果有服务直接宕了(服务不可用的情况),直接写接口实现类就行

package com.jin.controller;

import com.jin.feignclients.HystrixClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jinyunlong
 * @date 2021/7/16 9:24
 * @profession ICBC锅炉房保安
 */
@RestController
public class TestDemoController {

    //注入openfeign 客户端对象
    @Qualifier("com.jin.feignclients.HystrixClient")
    @Autowired
    private HystrixClient hystrixClient;


    @GetMapping("/test")
    public String test(){
        System.out.println("test ok");
        String demoresult = hystrixClient.demo(-7);
        System.out.println("demo result:"+demoresult);
        return "test ok";
    }
}

package com.jin.feignclients;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author jinyunlong
 * @date 2021/7/16 9:32
 * @profession ICBC锅炉房保安
 */
@FeignClient(value = "HYSTRIX",fallback = HystrixClientFallBack.class)  //fallback;  这个属性用来指定当前服务不可用时,默认的备选处理
public interface HystrixClient {
    @GetMapping("/demo")
    public String demo(@RequestParam Integer id);

}

package com.jin.feignclients;

import org.springframework.stereotype.Component;

/**
 * @author jinyunlong
 * @date 2021/7/16 9:47
 * @profession ICBC锅炉房保安
 */
//自定义HystrixClient 默认备选处理
@Component   //spring.xml
public class HystrixClientFallBack implements HystrixClient{
    @Override
    public String demo(Integer id) {
        return "当前服务不可用,请稍后再试:"+id;
    }
}

Hystrix Dashboard(仪表盘)

锦上添花的一个东西吧,可视化服务接口请求次数,熔断器状态等,这个最简单,引依赖、启动类加注解就可以用了。

<!--引入hystrix dashboard 依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

@EnableHystrixDashboard //开启监控面板

有几个坑点:

1、要在需要进行熔断监控的微服务中加入配置类:

package com.jin.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jinyunlong
 * @date 2021/7/16 16:23
 * @profession ICBC锅炉房保安
 */
@Configuration
public class BeansConfig {

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

2、jQuery问题解决

1.PNG

都解决后访问http://ip:port/hystrix即可访问主界面 ,由于我在配置类里写的是/hystrix.stream,所以只需要填写服务监控了路径为http://ip:port/hystrix.stream即可开始监控。

(不太好模拟测试)用postman慢一点发个20来次失败请求,然后发送1次成功请求也会返回失败内容,然后5秒内再次发送成功请求会返回成功,并且可以实时查看熔断器的open、close状态(没有半开)

正如服务注册中心对各种微服务做可视化监控一样,熔断仪表盘的作用就是对熔断服务做可视化监控

总结Hystrix

毕竟是耐非家的东西,他也不维护了哈哈哈(同样,仪表盘也被放弃)!不过官方说1.5.8足够稳定了。不过出于对未来考虑,应该会去使用阿里的流量卫兵sentinel

首先由微服务的雪崩引出服务熔断和服务降级(熔断也是降级的一种)。熔断中又有链路熔断(openFeign接口转到熔断微服务实现)和微服务不可用(直接在请求client中用openFeign的接口实现类实现)这两种熔断,最后可用仪表盘去监控熔断的微服务。

前几个组件的总结

主要使用了eureka、consul、ribbon+restTemplate、openFeign、Hystrix、Hystrix Dashboard。还有微服务、单应用的一些联系和相关概念,直接贴图:

30.组件快速回顾.png


标题:再战SpringCloud(5)
作者:jyl
地址:http://jinyunlong.xyz/articles/2021/07/15/1626341197640.html