代码编织梦想

一、概述

容错机制是指的是在一个分布式系统中,每个微服务之间是相互调用的,并且他们之间相互依赖,而实际的运行情况中,可能会因为各种原因导致某个微服务不可用,那么依赖于这个微服务的其他微服务就可能出现响应时间过长或者请求失败的情况,出现这种情况比较多就可能导致整个系统卡顿甚至奔溃。那么如何解决这个问题呢,就可以通过 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>
  1. 创建配置文件 application.yml,配置代码如下:
server:
  port: 8060
spring:
  application:
    name: hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true
management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'
  1. 创建启动类,代码如下:
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;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(HystrixApplication.class, args);
	}

}

注解说明

  • @EnableCircuitBreaker:声明启用数据监控

  • @EnableHystrixDashboard:声明启用可视化的数据监控

  1. 创建实体类,方便接收 findAll 的数据转化实体,不接收数据转实体可不用实例类,比如 index 方法。具体代码如下:
package com.zing.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
public class Student {
	private long id;
	private String name;
	private int age;
}
  1. 创建 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();
}
  1. 创建 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 {
	
	@Autowired
	private IFeignService feignservice;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
		return feignservice.findAll();
	}
	
	@GetMapping("/index")
	public String index() {
		return feignservice.index();
	}
	
}
  1. 依次启动注册中心,两个服务提供者 provider 和 Hystrix 的服务,可访问注册中心看到如下效果:
    在这里插入图片描述
  2. 访问地址 http://localhost:8060/actuator/hystrix.stream 可查看到数据监控页面一直在刷新访问数据。数据为空是因为我们没有访问具体的接口,页面如下:
    在这里插入图片描述
  3. 当我们访问 http://localhost:8060/hystrix/index 调用接口时,看到如下页面:
    在这里插入图片描述
  4. 我们再次返回 http://localhost:8060/actuator/hystrix.stream 可查看到有数据出现,如下图:
    在这里插入图片描述
  5. 但是这个页面的数据不够直观,这时候,我们就要访问 http://localhost:8060/hystrix 就能出现如下界面:
    在这里插入图片描述
  6. 在监测接口地址连填写入数据监测的地址 http://localhost:8060/actuator/hystrix.stream 后,在 title 随便输入个名字即可进入到数据监测可视化界面,如下图:
    在这里插入图片描述
    至此,Hystrix 整个数据监控过程演示就已经完成。那么下一篇我们学习 Spring Cloud 的配置中心,请期待下一篇 Spring Cloud(九):Config 本地配置中心

开发工程师一只,也在不断的学习阶段,平时的小经验不定期分享。 希望看我写的文字的人,可以少走弯路 祝工作学习顺利。
博主经验有限,若有不足,欢迎交流,共同改进~ 愿与同在CSDN的你共同进步。

作者 | 甜蜜的小红薯
出品 | 小红薯

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36836370/article/details/130901237