2020-03-26 熔断管理

一、

 hystrix 

二、

 (1)、搭建hystrix-turbine项目,创建springboot启动项目后,pom.xml的配置信息

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ljtao3</groupId>
    <artifactId>hystrix-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-turbine</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

(2)、配置多个微服务的hystrix监控,在application.yml的配置文件中开启turbine并进行相关配置

server:
  port: 9200

spring:
  application:
    name: HYSTRIX-TURBINE

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/


turbine:
  #要监控的微服务列表,多个用,分隔
  appConfig: CONSUMER-USER
  clusterNameExpression: "'default'"

eureka相关配置,指定注册中心地址。

turbine配置:指定需要监控的微服务列表。

ruibine会自动的从注册中心中获取需要监控的微服务,并聚合所有微服务中的/hystrix.stream数据

 (3)配置启动类:

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class HystrixTurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbineApplication.class, args);
    }
}

作为一个独立的监控项目,需要配置启动类,开启HystrixDashboard监控平台,并激活Turbine。

测试:先打开 http://localhost:9200/hystrix

将 要监控的项目  http://localhost:9102/actuator/hystrix.stream 监控信息,放入到页面的输入框,点击下面的按钮,进入查看页面

 

运行被监控的方法,http://localhost:9102/user/userInfo4?id=2,在页面可以看到这些信息。

关于熔断器的状态介绍。

                                                 

可以在消费者项目配置

#在feign中开启对hystrix的支持
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
  hystrix:
    enabled: true
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #默认的连接超时时间1秒,若一秒没有返回数据,自动的触发降级逻辑。上面的feign超时时间也要一起设置。
      circuitBreaker:
        requestVolumeThreshold: 5 #触发熔断的最小请求次数,默认20 /10秒
        sleepWindowInMilliseconds : 10000   #熔断多少秒后去尝试请求 默认5 打开状态的时间
        errorThresholdPercentage: 50 #熔断触发的失败请求最小占比,默认50%
#  threadpool:
#    default:
#      coreSize: 10  # 全局配置

进行测试。

原文地址:https://www.cnblogs.com/mathlin/p/12577726.html