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
项目结构以下:
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