SpringCloud分佈式微服務b2b2c電子商務docker-hystrix-dashboard

Hystrix的主要優勢之一是它收集關於每一個HystrixCommand的一套指標。Hystrix儀表板以有效的方式顯示每一個斷路器的運行情況,經過Hystrix Dashboard咱們能夠在直觀地看到各Hystrix Command的斷路器是否打開,請求響應時間, 請求失敗率,請求超時個數等等數據。可是隻使用Hystrix Dashboard的話, 你只能看到單個應用內的服務信息, 這明顯不夠. 咱們須要一個工具能讓咱們彙總系統內多個服務的數據並顯示到Hystrix Dashboard上, 這個工具就是Turbine,瞭解springcloud架構能夠加求求:三五三六二四七二五九,這節咱們討論一下,怎麼用turbine+hystrix-dashboard監聽兩個消費者服務java

1、監聽模塊microservice-consumer-movie-feign-with-hystrix斷路器的運行情況
2、監聽模塊microservice-consumer-movie-ribbon-with-hystrix1斷路器的運行情況
2.一、建立模塊microservice-consumer-movie-ribbon-with-hystrix1git

項目結構以下:
SpringCloud分佈式微服務b2b2c電子商務docker-hystrix-dashboard
2.二、pom.xml文件github

<?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>microservice-spring-cloud</artifactId>
        <groupId>com.jacky</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-consumer-movie-ribbon-with-hystrix1</artifactId>
    <packaging>jar</packaging>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <!--設置在執行maven 的install時構建鏡像-->
                    <execution>
                        <id>build-image</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--安裝了docker的主機,而且打開了api remote接口設置-->
                    <dockerHost>http://192.168.6.130:5678</dockerHost>
                    <pushImage>true</pushImage><!--設置上傳鏡像到私有倉庫,須要docker設置指定私有倉庫地址-->
                    <!--鏡像名稱-->
                    <imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
                    <!--鏡像的基礎版本-->
                    <baseImage>java:openjdk-8-jdk-alpine</baseImage>
                    <!--鏡像啓動參數-->
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.三、配置文件application.ymlweb

spring:
  application:
    name: microservice-consumer-movie-ribbon-with-hystrix1
  sleuth:
    sampler:
      percentage: 1.0
  #zipkin:
    #base-url: http://localhost:7788
server:
  port: 8010
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#security:
  #oauth2:
   # resource:
    #  id: microservice-consumer-movie-ribbon-with-hystrix1
     # user-info-uri: http://localhost:9999/uaa/user
      #prefer-token-info: false

2.四、實體類User.javaspring

package com.jacky.cloud.entity;

import java.math.BigDecimal;

public class User {
  private Long id;

  private String username;

  private String name;

  private Short age;

  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Short getAge() {
    return this.age;
  }

  public void setAge(Short age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }

}

2.五、控制層MovieController.javadocker

hystrix.command.default和hystrix.threadpool.default中的default爲默認CommandKey

Command Properties
Execution相關的屬性的配置:
hystrix.command.default.execution.isolation.strategy 隔離策略,默認是Thread, 可選Thread|Semaphore

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令執行超時時間,默認1000ms

hystrix.command.default.execution.timeout.enabled 執行是否啓用超時,默認啓用true
hystrix.command.default.execution.isolation.thread.interruptOnTimeout 發生超時是是否中斷,默認true
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests 最大併發請求數,默認10,該參數當使用ExecutionIsolationStrategy.SEMAPHORE策略時纔有效。若是達到最大併發請求數,請求會被拒絕。理論上選擇semaphore size的原則和選擇thread size一致,但選用semaphore時每次執行的單元要比較小且執行速度快(ms級別),不然的話應該用thread。
semaphore應該佔整個容器(tomcat)的線程池的一小部分。
Fallback相關的屬性
這些參數能夠應用於Hystrix的THREAD和SEMAPHORE策略

hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 若是併發數達到該設置值,請求會被拒絕和拋出異常而且fallback不會被調用。默認10
hystrix.command.default.fallback.enabled 當執行失敗或者請求被拒絕,是否會嘗試調用hystrixCommand.getFallback() 。默認true
Circuit Breaker相關的屬性
hystrix.command.default.circuitBreaker.enabled 用來跟蹤circuit的健康性,若是未達標則讓request短路。默認true
hystrix.command.default.circuitBreaker.requestVolumeThreshold 一個rolling window內最小的請求數。若是設爲20,那麼當一個rolling window的時間內(好比說1個rolling window是10秒)收到19個請求,即便19個請求都失敗,也不會觸發circuit break。默認20
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds 觸發短路的時間值,當該值設爲5000時,則當觸發circuit break後的5000毫秒內都會拒絕request,也就是5000毫秒後纔會關閉circuit。默認5000
hystrix.command.default.circuitBreaker.errorThresholdPercentage錯誤比率閥值,若是錯誤率>=該值,circuit會被打開,並短路全部請求觸發fallback。默認50
hystrix.command.default.circuitBreaker.forceOpen 強制打開熔斷器,若是打開這個開關,那麼拒絕全部request,默認false
hystrix.command.default.circuitBreaker.forceClosed 強制關閉熔斷器 若是這個開關打開,circuit將一直關閉且忽略circuitBreaker.errorThresholdPercentage
Metrics相關參數
hystrix.command.default.metrics.rollingStats.timeInMilliseconds 設置統計的時間窗口值的,毫秒值,circuit break 的打開會根據1個rolling window的統計來計算。若rolling window被設爲10000毫秒,則rolling window會被分紅n個buckets,每一個bucket包含success,failure,timeout,rejection的次數的統計信息。默認10000
hystrix.command.default.metrics.rollingStats.numBuckets 設置一個rolling window被劃分的數量,若numBuckets=10,rolling window=10000,那麼一個bucket的時間即1秒。必須符合rolling window % numberBuckets == 0。默認10
hystrix.command.default.metrics.rollingPercentile.enabled 執行時是否enable指標的計算和跟蹤,默認true
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds 設置rolling percentile window的時間,默認60000
hystrix.command.default.metrics.rollingPercentile.numBuckets 設置rolling percentile window的numberBuckets。邏輯同上。默認6
hystrix.command.default.metrics.rollingPercentile.bucketSize 若是bucket size=100,window=10s,若這10s裏有500次執行,只有最後100次執行會被統計到bucket裏去。增長該值會增長內存開銷以及排序的開銷。默認100
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds 記錄health 快照(用來統計成功和錯誤綠)的間隔,默認500ms
Request Context 相關參數
hystrix.command.default.requestCache.enabled 默認true,須要重載getCacheKey(),返回null時不緩存
hystrix.command.default.requestLog.enabled 記錄日誌到HystrixRequestLog,默認true

Collapser Properties 相關參數
hystrix.collapser.default.maxRequestsInBatch 單次批處理的最大請求數,達到該數量觸發批處理,默認Integer.MAX_VALUE
hystrix.collapser.default.timerDelayInMilliseconds 觸發批處理的延遲,也能夠爲建立批處理的時間+該值,默認10
hystrix.collapser.default.requestCache.enabled 是否對HystrixCollapser.execute() and HystrixCollapser.queue()的cache,默認true

ThreadPool 相關參數
線程數默認值10適用於大部分狀況(有時能夠設置得更小),若是須要設置得更大,那有個基本得公式能夠follow:
requests per second at peak when healthy × 99th percentile latency in seconds + some breathing room
每秒最大支撐的請求數 (99%平均響應時間 + 緩存值)
好比:每秒能處理1000個請求,99%的請求響應時間是60ms,那麼公式是:
(0.060+0.012)

基本得原則時保持線程池儘量小,他主要是爲了釋放壓力,防止資源被阻塞。
當一切都是正常的時候,線程池通常僅會有1到2個線程激活來提供服務

hystrix.threadpool.default.coreSize 併發執行的最大線程數,默認10
hystrix.threadpool.default.maxQueueSize BlockingQueue的最大隊列數,當設爲-1,會使用SynchronousQueue,值爲正時使用LinkedBlcokingQueue。該設置只會在初始化時有效,以後不能修改threadpool的queue size,除非reinitialising thread executor。默認-1。
hystrix.threadpool.default.queueSizeRejectionThreshold 即便maxQueueSize沒有達到,達到queueSizeRejectionThreshold該值後,請求也會被拒絕。由於maxQueueSize不能被動態修改,這個參數將容許咱們動態設置該值。if maxQueueSize == -1,該字段將不起做用
hystrix.threadpool.default.keepAliveTimeMinutes 若是corePoolSize和maxPoolSize設成同樣(默認實現)該設置無效。若是經過plugin(https://github.com/Netflix/Hystrix/wiki/Plugins)使用自定義實現,該設置纔有用,默認1.
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds 線程池統計指標的時間,默認10000
hystrix.threadpool.default.metrics.rollingStats.numBuckets 將rolling window劃分爲n個buckets,默認10
相關文章
相關標籤/搜索