• hystrix 熔断器


    源码: https://gitee.com/GXQ205153964/Hystrix-parent.git

    Hystrix概述:

    •  hystrix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败雪崩
    • 级联失败: 当C服务出现问题(硬盘烧了,网络断了)不可用了,B依旧会不停的调用C,B的资源耗尽,导致B也出现问题,同理A。这种现象被称为雪崩

    预防雪崩

     Hystix主要功能:

    问题:

    这里有一个线程池对应3个服务,当哪个服务被调用时,线程池会分配一个线程运行。当C发生故障后线程池的线程依旧会执行C服务,从而导致线程池线程用完,从而导致BD服务也无法使用。

     

    解决: 

    • 隔离:

    线程池隔离:把A分成3个线程池分别对应三个服务,C坏了后,BD不会收到影响。

    信号量隔离:对BCD加一个阀门,只允许一定的数量的人经行访问,超过就不能访问了,从而阻止C将所有的线程池资源耗尽。

    • 降级 (异常,超时等)自己写个降级方案,在服务调用方和提供方都要写,网络不通时。降级就是执行自己B计划,预案。
    • 熔断   当C服务出现错误频率较高时,Hystrix直接将C的所有服务全部拒绝,C修复后,熔断器会自动将C恢复成原来样子,正常运行。熔断机制,用于监控微服务调用情况,当失败的情况打到预定的阀值(5秒失败20次),会打开短路器,拒绝所有请求,知道服务回复正常为止。
    • 限流   限制多少人/服务访问 

    降级:

     降级——服务提供方

    (c降级方案)

    流程:

    1. 在服务提供方,引入hystrix依赖
    2. 定义降级方法
    3. 使用@HystrixCommand注解配置降级方法
    4. 在启动类上开启Hystrix功能:@EnableCircuitBreker

     基础代码构建

    SpringCloud EurekeServer Consumer和provider

    C降级方案搭建

     provider:

    pom.xml

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

    ProviderApp启动类(开启熔断器)

    1. //启动类
    2. @EnableEurekaClient
    3. @SpringBootApplication
    4. @EnableCircuitBreaker//开启Hystrix熔断器
    5. public class ProviderApp {
    6. public static void main(String[] args){
    7. SpringApplication.run(ProviderApp.class,args);
    8. }
    9. }

     GoodsController

    1. package com.gao.controller;
    2. import com.gao.domain.Goods;
    3. import com.gao.service.GoodsService;
    4. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    5. import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.beans.factory.annotation.Value;
    8. import org.springframework.web.bind.annotation.GetMapping;
    9. import org.springframework.web.bind.annotation.PathVariable;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. //服务提供方
    13. @RestController
    14. @RequestMapping("/goods")
    15. public class GoodsController {
    16. @Autowired
    17. private GoodsService goodsService;
    18. /**/
    19. //这里注入port可时刻监控哪个服务被调用
    20. @Value("${server.port}")
    21. private int port;
    22. /**
    23. *降级:
    24. * 出现异常
    25. * 服务调用超时
    26. */
    27. @GetMapping("/findOne/{id}")
    28. //指定降级后调用的方法
    29. @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={
    30. //修改几秒后超时
    31. @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
    32. })
    33. public Goods findOne(@PathVariable("id") int id){
    34. Goods goods = goodsService.findOne(id);
    35. //造个异常
    36. //int i =3/0;
    37. //超时
    38. try{
    39. //休眠两秒
    40. Thread.sleep(2000);
    41. }catch (InterruptedException e){
    42. e.printStackTrace();
    43. }
    44. //将端口号,设置到了商品标题上方便监控
    45. goods.setTitle(goods.getTitle()+" "+"port" + ":" + port);
    46. return goods;
    47. }
    48. /**
    49. * 定义降级方法
    50. *方法的返回值需要和原方法一样
    51. * 方法的参数需要和原方法一样
    52. */
    53. public Goods findOne_fallback(int id){
    54. Goods goods = new Goods();
    55. goods.setTitle("降级了~~~");
    56. return goods;
    57. }
    58. }

    降级——Hystrix降级-服务提供方

    1. 在服务提供方,引入hystrix依赖(consumer使用了feign,而feign里已经集成了Hystrix,不用再引入feign依赖了,如果没有过使用feign,按照provider写
    2. 定义降级方法
    3. 使用@HystrixCommand注解配置降级方法
    4. 在启动类上开启Hystrix功能:@EnableCircuitBreaker

     consumer使用了feign,而feign里已经集成了Hystrix,不用再引入feign依赖了

    A级降级方案搭建

     开启feign对hystrix的支持

    1. #开启feign对hystrix的支持
    2. feign:
    3. hystrix:
    4. enabled: true

    pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-actuatorartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.cloudgroupId>
    12. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframework.cloudgroupId>
    16. <artifactId>spring-cloud-starter-openfeignartifactId>
    17. dependency>
    18. dependencies>

    GoodsFeignClient

    1. package com.gao.feign;
    2. @FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)
    3. public interface GoodsFeignClient {
    4. @GetMapping("/goods/findOne/{id}")
    5. public Goods findGoodById(@PathVariable("id") int id);
    6. }

    GoodsFeignClientFallback

    1. package com.gao.feign;
    2. @Component
    3. public class GoodsFeignClientFallback implements GoodsFeignClient {
    4. @Override
    5. public Goods findGoodById(int id) {
    6. Goods goods = new Goods();
    7. goods.setTitle("又被降级了~~~");
    8. return goods;
    9. }
    10. }

    启动类上加

    @EnableFeignClients//启动feign

    测试结果

     当服务C坏掉后,启动了降级方案,返回给A是好的值,所以A不会启动降级方案。在A和C之间不能通信时(例如断网,或者直接关掉C),A会启动降级方案

     熔断

     

     在provider里的GoodsController里添加

    1. 当id=1时,出现异常id=2时正常
    2. //熔断
    3. //如果id==1,则出现异常,id !=1 则正常访问
    4. if(id == 1){
    5. //造个异常
    6. int i =3/0;
    7. }

    启动ProviderApp,5秒失败20次,刷新,当达到这个情况下,当id变为2时也会降级。说明启动了熔断,等五秒后会自动启动,id=2时又可以用了。

    1. package com.gao.controller;
    2. import com.gao.domain.Goods;
    3. import com.gao.service.GoodsService;
    4. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    5. import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.beans.factory.annotation.Value;
    8. import org.springframework.web.bind.annotation.GetMapping;
    9. import org.springframework.web.bind.annotation.PathVariable;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. //服务提供方
    13. @RestController
    14. @RequestMapping("/goods")
    15. public class GoodsController {
    16. @Autowired
    17. private GoodsService goodsService;
    18. /**/
    19. //这里注入port可时刻监控哪个服务被调用
    20. @Value("${server.port}")
    21. private int port;
    22. /**
    23. *降级:
    24. * 出现异常
    25. * 服务调用超时
    26. */
    27. @GetMapping("/findOne/{id}")
    28. //指定降级后调用的方法
    29. @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={
    30. //修改几秒后超时
    31. @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000"),
    32. //监控时间 默认5000 毫秒
    33. @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
    34. //失败次数。默认20次
    35. @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
    36. //失败率 默认50%
    37. @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")
    38. })
    39. public Goods findOne(@PathVariable("id") int id){
    40. Goods goods = goodsService.findOne(id);
    41. //熔断
    42. //如果id==1,则出现异常,id !=1 则正常访问
    43. if(id == 1){
    44. //造个异常
    45. int i =3/0;
    46. }
    47. //降级
    48. //造个异常
    49. int i =3/0;
    50. //超时
    51. /*
    52. try{
    53. //休眠两秒
    54. Thread.sleep(2000);
    55. }catch (InterruptedException e){
    56. e.printStackTrace();
    57. }
    58. */
    59. //将端口号,设置到了商品标题上方便监控
    60. goods.setTitle(goods.getTitle()+" "+"port" + ":" + port);
    61. return goods;
    62. }
    63. /**
    64. * 定义降级方法
    65. *方法的返回值需要和原方法一样
    66. * 方法的参数需要和原方法一样
    67. */
    68. public Goods findOne_fallback(int id){
    69. Goods goods = new Goods();
    70. goods.setTitle("降级了~~~");
    71. return goods;
    72. }
    73. }

    修改熔断基础属性

    //监控时间 默认5000 毫秒 @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"), //失败次数。默认20次 @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"), //失败率 默认50% @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")

     provider GoodsController

    1. package com.gao.controller;
    2. //服务提供方
    3. @RestController
    4. @RequestMapping("/goods")
    5. public class GoodsController {
    6. @Autowired
    7. private GoodsService goodsService;
    8. /**/
    9. //这里注入port可时刻监控哪个服务被调用
    10. @Value("${server.port}")
    11. private int port;
    12. /**
    13. *降级:
    14. * 出现异常
    15. * 服务调用超时
    16. */
    17. @GetMapping("/findOne/{id}")
    18. //指定降级后调用的方法
    19. @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={
    20. //修改几秒后超时
    21. @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000"),
    22. //监控时间 默认5000 毫秒
    23. @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
    24. //失败次数。默认20次
    25. @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
    26. //失败率 默认50%
    27. @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")
    28. })
    29. public Goods findOne(@PathVariable("id") int id){
    30. Goods goods = goodsService.findOne(id);
    31. //熔断
    32. //如果id==1,则出现异常,id !=1 则正常访问
    33. if(id == 1){
    34. //造个异常
    35. int i =3/0;
    36. }
    37. //降级
    38. //造个异常
    39. int i =3/0;
    40. //超时
    41. /*
    42. try{
    43. //休眠两秒
    44. Thread.sleep(2000);
    45. }catch (InterruptedException e){
    46. e.printStackTrace();
    47. }
    48. */
    49. //将端口号,设置到了商品标题上方便监控
    50. goods.setTitle(goods.getTitle()+" "+"port" + ":" + port);
    51. return goods;
    52. }
    53. /**
    54. * 定义降级方法
    55. *方法的返回值需要和原方法一样
    56. * 方法的参数需要和原方法一样
    57. */
    58. public Goods findOne_fallback(int id){
    59. Goods goods = new Goods();
    60. goods.setTitle("降级了~~~");
    61. return goods;
    62. }
    63. }

    熔断监控

    简介

     

    •  Hystrix提供了Hystrix-dashbopard功能,用于实时监控微服务运行状态
    • 但是Hystrix-dashboard只能监控一个微服务
    • Netflix还提供了Trurbine进行聚合监控

     搭建:

     搭建监控模块:

    创建监控模块:

    创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能

    引入Turbine聚合监控起步依赖:

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>spring_cloud_parentartifactId>
    6. <groupId>com.gaogroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>hystrix-monitorartifactId>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    14. <java.version>1.8java.version>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.cloudgroupId>
    19. <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.cloudgroupId>
    23. <artifactId>spring-cloud-starter-netflix-turbineartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-actuatorartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.cloudgroupId>
    31. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframework.bootgroupId>
    35. <artifactId>spring-boot-starter-testartifactId>
    36. <scope>testscope>
    37. dependency>
    38. dependencies>
    39. <build>
    40. <plugins>
    41. <plugin>
    42. <groupId>org.springframework.bootgroupId>
    43. <artifactId>spring-boot-maven-pluginartifactId>
    44. plugin>
    45. plugins>
    46. build>
    47. project>

    application.yml

    1. spring:
    2. application.name: hystrix-monitor
    3. server:
    4. port: 8769
    5. turbine:
    6. combine-host-port: true
    7. # 配置需要被监控的服务名称列表
    8. app-config: hystrix-provider,hystrix-consumer
    9. cluster-name-expression: "'default'"
    10. aggregator:
    11. cluster-config: default
    12. #instanceUrlSuffix: /actuator/hystrix.stream
    13. eureka:
    14. client:
    15. serviceUrl:
    16. defaultZone: http://localhost:8761/eureka/

    启动类:

    1. package com.itheima;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    6. import org.springframework.cloud.netflix.turbine.EnableTurbine;
    7. @SpringBootApplication
    8. @EnableEurekaClient
    9. @EnableTurbine //开启Turbine 很聚合监控功能
    10. @EnableHystrixDashboard //开启Hystrix仪表盘监控功能
    11. public class HystrixMonitorApp {
    12. public static void main(String[] args) {
    13. SpringApplication.run(HystrixMonitorApp.class, args);
    14. }
    15. }

    分别修改hystrix-provider和hystrix-consumer模块

    添加依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-actuatorartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.cloudgroupId>
    7. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    8. dependency>
    9. <dependency>
    10. <groupId>org.springframework.cloudgroupId>
    11. <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
    12. dependency>

    启动类里配置Bean再添加启动注解

    1. //启动类
    2. @EnableEurekaClient
    3. @SpringBootApplication
    4. @EnableCircuitBreaker//开启Hystrix熔断器
    5. @EnableHystrixDashboard//开启Hystrix仪表盘监控功能
    6. public class ProviderApp {
    7. public static void main(String[] args){
    8. SpringApplication.run(ProviderApp.class,args);
    9. }
    10. @Bean
    11. public ServletRegistrationBean getServlet() {
    12. HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    13. ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    14. registrationBean.setLoadOnStartup(1);
    15. registrationBean.addUrlMappings("/actuator/hystrix.stream");
    16. registrationBean.setName("HystrixMetricsStreamServlet");
    17. return registrationBean;
    18. }
    19. }

    consumer OrderController这里注入的goodsFeignClient会报红,可以忽略。

    1. //服务的调用方
    2. @RestController
    3. @RequestMapping("/order")
    4. public class OrderController {
    5. @Autowired
    6. private GoodsFeignClient goodsFeignClient;
    7. @GetMapping("/goods/{id}")
    8. public Goods findGoodsById(@PathVariable("id") int id){
    9. //3.调用方法
    10. //Goods goods = restTemplate.getForObject(url,Goods.class);
    11. Goods goods = goodsFeignClient.findGoodById(id);
    12. return goods;
    13. }
    14. }

    web界面输入Url地址:

    http://localhost:8769/turbine.stream                监控所有监管的服务

    http://localhost:9000/actuator/hystrix.stream  监控单个服务consumer

    http://localhost:8000/actuator/hystrix.stream  监控provider

     图像注释:

     实心圆:他有颜色大小区分,分别代表实例的监控程序和流量大小。他从上图所示健康度从绿色 黄色 橙色 红色递减。通过实心球的展示我们可以在大量的实例中快速的发现故障实例和高压力实例

    曲线: 用来记录两分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。 

  • 相关阅读:
    Object.preventExtensions vs Object.seal vs Object.freeze
    HarmonyOS NEXT应用开发案例——自定义TabBar
    Posgresql数据库项目sql实操1
    初探Hessian利用链为Dubbo-CVE占坑
    Rust所有权及引用
    C++ 内联函数的作用
    UE5 官方案例LyraStarter 全特性详解 5.LyraPlayerStart
    Qt实现生成唯一标识ID
    PEG包裹上转换荧光纳米颗粒
    论Orchestration和Choreography
  • 原文地址:https://blog.csdn.net/qq_51497041/article/details/126243229