【SpringCloud总结】10 Hystrix断路器

    xiaoxiao2023-10-30  152

    一、服务降级

    1. 简介

        整体资源快不够了,忍痛将某些服务先关掉,待渡过难关,再开启回来。

        服务降级处理是在客户端实现完成的,与服务端没有关系

    2. 修改 microservicecloud-api 工程,根据已经有的DeptClientService接口新建一个实现了FallbackFactory接口的类   DeptClientServiceFallbackFactory

    package com.atguigu.springcloud.service;   import java.util.List;   import org.springframework.stereotype.Component;   import com.atguigu.springcloud.entities.Dept;   import feign.hystrix.FallbackFactory;   @Component//不要忘记添加,不要忘记添加 public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {   @Override   public DeptClientService create(Throwable throwable)   {    return new DeptClientService() {      @Override      public Dept get(long id)      {        return new Dept().setDeptno(id)                .setDname("该ID:"+id+"没有没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭")                .setDb_source("no this database in MySQL");      }        @Override      public List<Dept> list()      {        return null;      }        @Override      public boolean add(Dept dept)      {        return false;      }    };   } }

    3. 修改microservicecloud-api工程,DeptClientService接口在注解@FeignClient中添加fallbackFactory属性值

    package com.atguigu.springcloud.service;   import java.util.List;   import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;   import com.atguigu.springcloud.entities.Dept;   @FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class) public interface DeptClientService {   @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)   public Dept get(@PathVariable("id") long id);     @RequestMapping(value = "/dept/list",method = RequestMethod.GET)   public List<Dept> list();     @RequestMapping(value = "/dept/add",method = RequestMethod.POST)   public boolean add(Dept dept); }

    4. microservicecloud-api工程,mvn clean install

    5. microservicecloud-consumer-dept-feign工程修改YML

    feign:    hystrix:      enabled: true

    6. 测试

    (1)3个eureka先启动

    (2)微服务microservicecloud-provider-dept-8001启动

    (3)microservicecloud-consumer-dept-feign启动

    (4)正常访问测试:http://localhost/consumer/dept/get/1

    (5)故意关闭微服务microservicecloud-provider-dept-8001

    (6)再次访问测试。虽然服务关掉了,但是再次访问时,不会出现error界面,而是给出友好的提示!提示的内容就是我们上面类中配置的!

    【提示】throwable参数可以获取异常的信息,然后根据调用的方法出现的异常,返回相应的提示信息!我们可以看到,服务降级把异常处理的所有方法,放到了一个类下,而不是想服务熔断那样,把异常处理的方法和正常的方法放在一个类下。个人建议,熔断采用服务降级的形式,这样比较清晰明确!

     

    二、服务监控hystrixDashboard

    1. 简介

           除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面

    2. 实例步骤

    (1)新建工程microservicecloud-consumer-hystrix-dashboard

    (2)pom文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>      <parent>    <groupId>com.atguigu.springcloud</groupId>    <artifactId>microservicecloud</artifactId>    <version>0.0.1-SNAPSHOT</version>   </parent>      <artifactId>microservicecloud-consumer-hystrix-dashboard</artifactId>    <dependencies>    <!-- 自己定义的api -->    <dependency>      <groupId>com.atguigu.springcloud</groupId>      <artifactId>microservicecloud-api</artifactId>      <version>${project.version}</version>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!-- 修改后立即生效,热部署 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>springloaded</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-devtools</artifactId>    </dependency>    <!-- Ribbon相关 -->    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-ribbon</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <!-- feign相关 -->    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-feign</artifactId>    </dependency>    <!-- hystrix和 hystrix-dashboard相关-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>    </dependency>    </dependencies> </project>  

       主要是添加下面的依赖

       <!-- hystrix和 hystrix-dashboard相关-->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>    </dependency> 

    (3)yml文件

    server:   port: 9001

    (4)主启动类

    package com.atguigu.springcloud;   import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;   @SpringBootApplication @EnableHystrixDashboard public class DeptConsumer_DashBoard_App {   public static void main(String[] args)   {    SpringApplication.run(DeptConsumer_DashBoard_App.class,args);   } }

    【提示】新注解@EnableHystrixDashboard

    (5)所有Provider微服务提供类都需要监控依赖配置

       <!-- actuator监控信息完善 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>

    (6)启动microservicecloud-consumer-hystrix-dashboard该微服务监控消费端

                   http://localhost:9001/hystrix

             我们可以看到hystrix的web界面

    (7)测试

             启动3个eureka集群

             启动microservicecloud-provider-dept-hystrix-8001

                 http://localhost:8001/hystrix.stream

    Caption

     

           启动消费者

           把http://localhost:8001/hystrix.stream放在上面web界面的第一个框里面,后面两个框是自定义的。

           多次刷新http://localhost:8001/dept/get/1,查看web界面

    Caption

           

    最新回复(0)