美文网首页
Hystrix熔断器

Hystrix熔断器

作者: o_O小薯条 | 来源:发表于2021-01-15 09:11 被阅读0次

Hystrix 概述

  • Hystrix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务。第三方库,防止出现级联失败(雪崩)
  • 雪崩:一个服务失败。导致整条链路的服务都失败的情形。
  • Hystrix主要功能
  • 隔离
  1. 线程池隔离
  2. 信号量隔离
  • 降级: 异常 超时
  • 熔断
  • 限流

Hystrix 降级

  • Hystrix降级:当服务发生异常或调用超时,返回默认数据

Hystrix降级 - 服务提供方

  1. 在服务提供方,引入Hystrix依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.定义降级方法

/**
*定义降级方法:
*1.方法的返回值需要和原方法一样
*2.方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){
      Goods goods = new Goods();
      goods.setTitle("降级了....");
      return goods;
}

3.使用@HystrixCommand注解配置降级方法

/**
*降级:
*1.出现异常
*2.服务调用超时
*        *默认1s超时
*/
@HystrixCommand(fallbackMethod = "findOne_fallback", commandProperties = {
       //设置Hystrix的超时时间,默认1s
      @HystruxProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
}) //fallbackMethod 指定降级后调用的方法

4.在启动类上开启Hystrix功能:@EnableCircuitBreaker

Hystrix降级 - 服务消费方

  1. fegin组件已经集成了hystrix组件。
  2. 定义fegin调用接口实现类,复写方法,即降级方法
package com.itheima.consumer.feign;

import com.itheima.consumer.domain.Goods;
import org.springframework.stereotype.Component;

/**
 * Feign 客户端的降级处理类
 * 1.定义类实现 Feign 客户端接口
 * 2.使用@Component注解将该类的Bean加入Spring的IOC容器
 */
@Component
public class GoodsFeignClientFallback implements  GoodsFeignClient {


    @Override
    public Goods findGoodsById(int id) {
        Goods goods = new Goods();
        goods.setTitle("又被降级了。。。");
        return goods;
    }
}
  1. @FeignClient注解中使用fallback属性设置降级处理类
@FeginClient(value = "FEIGN_PROVIDER", fallback = GoodsFeignClientFallback.class)
  1. yml配置开启 feign.hystrix.enabled = true默认是关着的

Hystrix 熔断

  • Hystrix熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阀值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。
  • 三种状态 : 打开,关闭, 半开


    熔断-1.png
  • 设置参数
@HystrixCommand(fallbackMethod = "findOne_fallback", commandProperties = {
       //设置Hystrix的超时时间,默认1s
      @HystruxProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
      //监控时间   默认5000毫秒
      @HystruxProperty(name = "circuitBreaker.sleepWindowlnMilliseconds", value = "5000"),
     //失败次数  默认20次
      @HystruxProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
      //失败率  默认50%
      @HystruxProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
}) 

Hystrix 熔断监控

  • Hystrix提供了 Hystrix-dashboard功能,用于实时监控微服务运行状态。


    熔断监控-1.png
  • 但是Hystrix-dashboard只能监控一个微服务。
  • Netflix还提供了Turbine,进行聚合监控。
    一. 搭建监控模块
    1. 创建监控模块

创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix-dashboard功能。

2. 引入Turbine聚合监控起步依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>hystrix-parent</artifactId>
        <groupId>com.itheima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hystrix-monitor</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. yml配置

spring:
  application:
    name: hystrix-monitor

server:
  port: 8769

turbine:
  combine-host-port: true
  app-config: hystrix-provider,hystrix-consumer #配置需要被监控的服务名称列表
  aggregator:
    cluster-config: default #instanceUrlSuffix: /actuator/hystrix.stream
  cluster-name-expression: "default"

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

4. 添加启动类

package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableEurekaClient

@EnableTurbine //开启Turbine 聚合监控功能
@EnableHystrixDashboard //开启HyStrix仪表盘监控功能
public class HystrixMonitorApp {
    public static void main(String[] args) {
        SpringApplication.run(HystrixMonitorApp.class, args);
    }
}

二. 修改被监控模块

需要分别修改hystrix-provider和provider-consumer模块:

1. 导入依赖:

<!--turnbin监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

3. 配置Bean

此处为了方便,将其配置在启动类里 添加注解 @EnableHystrixDashboard

ackage com.itheima.provider;

import com.netflix.hystrix.HystrixMetrics;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;

/**
 * 启动类
 */
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker //开启Hystrix功能

@EnableHystrixDashboard //开启Hystrix仪表盘监控功能
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class, args);
    }

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return  registrationBean;
    }
}

启动项目访问http://ip:8769/hystrix/ 进入Hystrix Dashboard界面

Dashboard-1.png
Dashboard-2.png

相关文章

  • SpringCloud 之Hystrix熔断器

    熔断器Hystrix 为什么要使用熔断器 什么是Hystrix Hystrix 中文意思就是豪猪 ,因其背上长满...

  • 本周学习(20211220-20211226)

    Hystrix提供的熔断器具有自我反馈,自我恢复的功能,Hystrix会根据调用接口的情况,让熔断器在closed...

  • springcloud使用(四) 熔断器Hystrix

    熔断器的概念和优点参考 springcloud(四):熔断器Hystrix, 讲的很详细 基于feign的Hyst...

  • Feign调用报错:failed and no fallback

    timed-out and no fallback 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该...

  • 熔断器 Hystrix

    熔断器Hystrix 版本 2.2.1.RELEASE 1. 介绍  Hystrix是一个延迟和容错工具,旨在隔离...

  • Hystrix的正确理解方式

    hystrix-logo-tagline-640.png 什么是熔断器 熔断器,原本是电路中在电器发生短路时的防止...

  • Hystrix熔断器执行机制

    本篇假设大家对Hystrix的执行过程及源码有一定的了解,这里介绍Hystrix的熔断器执行机制。 1.Hystr...

  • hystrix

    hystrix 三个设计原则 资源隔离利用线程池实现,每个依赖服务最多耗尽自己线程池的资源 熔断器熔断器 命令模式...

  • 熔断器---Hystrix

    Hystrix:熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能...

  • Hystrix(熔断器)

    概述 Hystrix是一个用于处理分布式系统之间调用出现的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会...

网友评论

      本文标题:Hystrix熔断器

      本文链接:https://www.haomeiwen.com/subject/tumvaktx.html