天天看點

hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

hystrix 對請求的降級和熔斷,可以産生監控資訊,hystrix dashboard可以實時的進行監控

sp07-hystrix項目添加actuator,并暴露 hystrix監控端點

actuator 是 spring boot 提供的服務監控工具,提供了各種監控資訊的監控端點

management.endpoints.web.exposure.include

配置選項,

可以指定端點名,來暴露監控端點

如果要暴露所有端點,可以用 “*”

hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

1.pom.xml添加actuator依賴

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

2.調整 application.yml 配置,并暴露

hystrix.stream

監控端點

server:
  port: 3001
spring:
  application:
    name: hystrix-server
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
ribbon:
  MaxAutoRetriesNextServer: 2
  MaxAutoRetried: 1
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 500
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream
           

3.通路 actuator 路徑,檢視監控端點

  • http://localhost:3001/actuator

Hystrix dashboard 儀表盤

hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

1.建立項目

hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置
hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

2.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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.tedu</groupId>
    <artifactId>sp08-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp08-hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <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-hystrix-dashboard</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

           

3.application.yml

spring:
  application:
    name: hystrix-dashboard
    
server:
  port: 4001

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
           

4.主程式添加

@EnableHystrixDashboard

@EnableDiscoveryClient

@EnableDiscoveryClient
@EnableHystrixDashboard
@SpringBootApplication
public class Sp08HystrixDashboardApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp08HystrixDashboardApplication.class, args);
	}

}
           

5.啟動并測試

通路 hystrix dashboard

  • http://localhost:4001/hystrix
    hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

填入 hystrix 的監控端點,開啟監控

  • http://localhost:3001/actuator/hystrix.stream
    hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置
  • 通過 hystrix 通路服務多次,觀察監控資訊

    http://localhost:3001/item-service/35

    hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

hystrix 熔斷

整個鍊路達到一定的門檻值,預設情況下,10秒内産生超過20次請求,則符合第一個條件。

滿足第一個條件的情況下,如果請求的錯誤百分比大于門檻值,則會打開斷路器,預設為50%。

Hystrix的邏輯,先判斷是否滿足第一個條件,再判斷第二個條件,如果兩個條件都滿足,則會開啟斷路器

斷路器打開 5 秒後,會處于半開狀态,會嘗試轉發請求,如果仍然失敗,保持打開狀态,如果成功,則關閉斷路器

  • 斷路器狀态為 Open,所有請求會被短路,直接降級執行 fallback 方法
    hystrix dashboard 斷路器儀表盤sp07-hystrix項目添加actuator,并暴露 hystrix監控端點Hystrix dashboard 儀表盤hystrix 熔斷hystrix 配置

hystrix 配置

https://github.com/Netflix/Hystrix/wiki/Configuration

  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

    請求逾時時間,逾時後觸發失敗降級
  • hystrix.command.default.circuitBreaker.requestVolumeThreshold

    10秒内請求數量,預設20,如果沒有達到該數量,即使請求全部失敗,也不會觸發斷路器打開
  • hystrix.command.default.circuitBreaker.errorThresholdPercentage

    失敗請求百分比,達到該比例則觸發斷路器打開
  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds

    斷路器打開多長時間後,再次允許嘗試通路(半開),仍失敗則繼續保持打開狀态,如成功通路則關閉斷路器,預設 5000