Spring Cloud08: 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代码如下:


        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
        2.0.2.RELEASE

        org.springframework.cloud
        spring-cloud-starter-openfeign
        2.0.2.RELEASE

        org.springframework.boot
        spring-boot-starter-actuator
        2.0.7.RELEASE

        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix
        2.0.2.RELEASE

        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix-dashboard
        2.0.2.RELEASE

2.创建配置文件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'

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;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
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;

@Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
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();
}
</student>

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 {

    @Autowired
    private IFeignService feignservice;

    @GetMapping("/findAll")
    public Collection<student> findAll(){
        return feignservice.findAll();
    }

    @GetMapping("/index")
    public String index() {
        return feignservice.index();
    }

}
</student>

7.依次启动注册中心,两个服务提供者provider和Hystrix的服务,可访问注册中心看到如下效果:

Spring Cloud08: Hystrix 容错机制与数据监控

8.访问地址 http://localhost:8060/actuator/hystrix.stream 可查看到数据监控页面一直在刷新访问数据。数据为空是因为我们没有访问具体的接口,页面如下:

Spring Cloud08: Hystrix 容错机制与数据监控

9.当我们访问 http://localhost:8060/hystrix/index 调用接口时,看到如下页面:

Spring Cloud08: Hystrix 容错机制与数据监控

10.我们再次返回http://localhost:8060/actuator/hystrix.stream可查看到有数据出现,如下图:

Spring Cloud08: Hystrix 容错机制与数据监控

11.但是这个页面的数据不够直观,这时候,我们就要访问 http://localhost:8060/hystrix 就能出现如下界面:

Spring Cloud08: Hystrix 容错机制与数据监控

12.在监测接口地址连填写入数据监测的地址http://localhost:8060/actuator/hystrix.stream后,在title随便输入个名字即可进入到数据监测可视化界面,如下图:

Spring Cloud08: Hystrix 容错机制与数据监控

Original: https://www.cnblogs.com/kylinxxx/p/14510933.html
Author: 猫坚果NutCat
Title: Spring Cloud08: Hystrix 容错机制与数据监控

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/591168/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球