zoukankan      html  css  js  c++  java
  • 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代码如下:

        <!-- 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: 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();
    }
    

    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();
    	}
    	
    }
    

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

    img

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

    img

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

    img

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

    img

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

    img

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

    img

  • 相关阅读:
    pyCharm最新2018激活码
    pycharm fiddler requests.exceptions.SSLError
    耐克的毛毛虫
    ROS-RouterOS hAP ac2+usb 4G上网卡+小米新推的无线上网卡是绝配
    TOM带你玩充电 篇三:15款5号电池横评及选购建议——南孚金霸王小米宜家耐时品胜一个都逃不了
    关于DELL服务器如果采购散件,进行服务器升级的相关说明
    开通微信零钱通的方法微信免手续费提现
    近期给朋友推荐的笔记本型号
    江苏中石化加油卡积分的几种类别
    Selenium geckodriver异常
  • 原文地址:https://www.cnblogs.com/kylinxxx/p/14510933.html
Copyright © 2011-2022 走看看