我的Spring Cloud(八):Hystrix 容错机制与数据监控
一、概述
容错机制是指的是在一个分布式系统中,每个微服务之间是相互调用的,并且他们之间相互依赖,而实际的运行情况中,可能会因为各种原因导致某个微服务不可用,那么依赖于这个微服务的其他微服务就可能出现响应时间过长或者请求失败的情况,出现这种情况比较多就可能导致整个系统卡顿甚至奔溃。那么如何解决这个问题呢,就可以通过Hystix的容错机制来处理。
容错机制是指在不改变各个微服务的调用关系的前提下,针对错误情况进行预先处理。Hystrix的主要作用就是当服务提供者发生故障无法访问的时候,向服务消费者返回一个fallback的降级处理,从而保证系统能顺利运行。
上一篇已经结合Feign讲解了Hystrix的容错机制,容错功能指的是当服务的调用发生问题的时候,我们做一个降级处理,用另外一部分代码进行处理。可以有效的防止程序因为响应时间过长而造成整个系统崩溃,并且还能给用户提供更为友好的交互。但是Hystrix除了熔断机制外,还提供了对请求的数据监控,本篇着重讲解Hystix的数据监控。Hystrix数据监控需要结合Spring Boot Actuator来使用,Actuator提供了对服务的健康监控、数据统计,可以通过hystrix.stream节点获取监控请求数据,提供了可视化的监控界面。
二、设计原则
1. 服务隔离机制:防止某个服务提供者出现故障而影响整个系统的运行。
2. 服务降级机制:当服务提供者出现故障时,向服务消费者返回一个fallback降级处理。
3. 熔断机制:当服务消费者请求失败率达到某个特定的数值时,会迅速的启动熔断机制,并且对错误进行修复。
4. 提供实时的监控和报警功能:保证熔断机制的运行。
5. 提供实时的配置修改功能:保证熔断机制的运行。
三、实战!
1.创建Module,pom.xml代码如下:
<!-- Eureka客户端依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.0.2.RELEASE</version></dependency><!-- 加入feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.2.RELEASE</version></dependency><!-- 加入actuator健康监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.0.7.RELEASE</version></dependency><!-- 添加hysteix熔断器 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.0.2.RELEASE</version></dependency><!-- 添加可视化监控组件 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId><version>2.0.2.RELEASE</version></dependency>
2.创建配置文件application.yml,配置代码如下:
server:port: 8060spring:application:name: hystrixeureka:client::defaultZone: http://localhost:8761/eureka/feign:hystrix:enabled: truemanagement:endpoints:web:exposure:include: 'hystrix.stream'
3.创建启动类,代码如下:
package com.zing;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.openfeign.EnableFeignClients;public class HystrixApplication {public static void main(String[] args) throws Exception {SpringApplication.run(HystrixApplication.class, args);}}
注解说明
* @EnableCircuitBreaker:声明启用数据监控
* @EnableHystrixDashboard:声明启用可视化的数据监控
4.创建实体类,方便接收findAll的数据转化实体,不接收数据转实体可不用实例类,比如index方法。具体代码如下:
package com.zing.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;//生成Getter,Setter,equals,canEqual,hasCode,toString等方法//添加一个构造函数,该构造函数含有所有已声明字段属性参数//创建一个无参构造函数public class Student {private long id;private String name;private int age;}
5.创建Feign声明式接口,代码如下:
package com.zing.service;import java.util.Collection;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import com.zing.entity.Student;@FeignClient(value = "provider")public interface IFeignService {@GetMapping("/student/findAll")public Collection<Student> findAll();@GetMapping("/student/index")public String index();}
6.创建Controller代码如下;
package com.zing.controller;import java.util.Collection;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.zing.entity.Student;import com.zing.service.IFeignService;@RestController@RequestMapping("/hystrix")public class HystrixHandler {@Autowiredprivate IFeignService feignservice;@GetMapping("/findAll")public Collection<Student> findAll(){return feignservice.findAll();}@GetMapping("/index")public String index() {return feignservice.index();}}
7.依次启动注册中心,两个服务提供者provider和Hystrix的服务,可访问注册中心看到如下效果:
9.当我们访问 http://localhost:8060/hystrix/index 调用接口时,看到如下页面:
10.我们再次返回http://localhost:8060/actuator/hystrix.stream可查看到有数据出现,如下图:
11.但是这个页面的数据不够直观,这时候,我们就要访问 http://localhost:8060/hystrix 就能出现如下界面:
至此,Hystrix整个数据监控过程演示就已经完成。那么下一篇我们学习Spring Cloud的配置中心,请期待下一篇《我的Spring Cloud(九):Config 本地配置中心》
