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 可以看到一些具体的数据:

扫描二维码关注公众号,回复: 3325156 查看本文章
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

猜你喜欢

转载自blog.csdn.net/zhengzizhi/article/details/81161145
今日推荐