SpringCloud入門實戰之十一:斷路器監控(Hystrix Dashboard)

第四篇文章斷路器講述瞭如何使用斷路器,並簡單的介紹了下Hystrix Dashboard組件,
這篇文章更加詳細的介紹Hystrix Dashboard。


在微服務架構中爲例保證程序的可用性,防止程序出錯導致網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程序
的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是作爲斷路器狀態的一個組件,提供了數據監控和友好的圖形
化界面。


本篇範例工程由上一篇(SpringCloud入門實戰之十)源碼的工程改造而來

第一部分:改造eureka-server工程

重新定義application.yml配置文件內容:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

第二部分:重新創建service-hi工程

刪除service-hi工程重新創建它,目的是熟悉一下STS開發工具應該勾選那些選項自動生成我們需要的依賴:

勾選 Hystrix                    生成依賴 spring-cloud-starter-netflix-hystrix
勾選 Hystrix Dashboard 生成依賴 spring-cloud-starter-netflix-hystrix-dashboard
勾選 Eureka Discovery  生成依賴 spring-cloud-starter-netflix-eureka-client
勾選 Actuator                 生成依賴 spring-boot-starter-actuator
勾選 Web                       生成依賴 spring-boot-starter-web


在程序的入口ServiceHiApplication類,加上@EnableHystrix註解開啓斷路器,這個是必須的,並且需要在程序中聲
明斷路點HystrixCommand;加上@EnableHystrixDashboard註解,開啓HystrixDashboard

package com.contoso;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHiApplication {

	/**
	 * 訪問地址 http://localhost:8762/actuator/hystrix.stream
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	@Value("${server.port}")
	String port;

	@RequestMapping("/hi")
	@HystrixCommand(fallbackMethod = "hiError")
	public String message(@RequestParam(value = "name", defaultValue = "World") String name) {
		return "Hello " + name + " ,this message is from port:" + port;
	}

	public String hiError(String name) {
		return "Hello " + name + " ,sorry,error!";
	}

}

定義application.yml配置文件內容如下:

server:
  port: 8762

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

啓動工程: 依次運行eureka-server註冊中心服務和service-hi服務提供者

Hystrix Dashboard圖形展示

打開 http://localhost:8762/actuator/hystrix.stream 可以看到一些具體的數據:

ping: 

data: {"type":"HystrixCommand","name":"message","group":"ServiceHiApplication$$EnhancerBySpringCGLIB$$3fd63545","currentTime":1532281208812,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"ServiceHiApplication$$EnhancerBySpringCGLIB$$3fd63545"}

data: {"type":"HystrixThreadPool","name":"ServiceHiApplication$$EnhancerBySpringCGLIB$$3fd63545","currentTime":1532281208812,"currentActiveCount":0,"currentCompletedTaskCount":3,"currentCorePoolSize":10,"currentLargestPoolSize":3,"currentMaximumPoolSize":10,"currentPoolSize":3,"currentQueueSize":0,"currentTaskCount":3,"rollingCountThreadsExecuted":0,"rollingMaxActiveThreads":0,"rollingCountCommandRejections":0,"propertyValue_queueSizeRejectionThreshold":5,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"reportingHosts":1}

http://localhost:8762/hystrix

本篇blog源碼 spring-cloud-example11.zip

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章