SPRING CLOUD微服务实战笔记--服务容错保护:Spring Cloud Hystrix(二)

属性详解

四种不同优先级别的配置(优先级由低到高)

  • 全局默认值:
    该属性通过代码中定义的默认值来确定
  • 全局配置属性:
    在配置文件中定义全局属性值,在应用启动时或在与Spring Cloud Config和Spring Cloud Bus实现的动态刷新配置功能的配合下,实现静态覆盖或者动态调整
  • 实例默认值:
    实例代码中定义的默认值
  • 实例配置属性:
    通过配置文件来为指定的实例进行属性配置,或者通过动态刷新来动态调整

Command属性

主要有5种不同类型的属性配置:

execution配置

execution配置控制的是HystrixCommand.run()的执行

  • execution.isolation.strategy:该属性用来设置HystrixCommand.run()执行的隔离策略
    – THREAD:通过线程池隔离的策略,它在独立的线程上执行,并且它的并发限制受线程池中线程数量的限制
    – SEMAPHORE:通过信号量隔离的策略,它在调用线程上执行,并且它的并发限制受信号量计数的限制
  • execution.isolation.thread.timeoutInMilliseconds:该属性用来配置HystrixCommand执行的超时时间,单位为毫秒.当HystrixCommand执行时间超过该配置值之后,Hystrix会将该执行命令标记为TIMEOUT并进入服务降级处理逻辑
  • execution.timeout.enabled:该属性用来配置HystrixCommand.run()的执行是否启用超时时间.默认为true,如果设置为false,那么execution.isolation.thread.timeoutInMilliseconds属性的配置将不再起作用
  • execution.isolation.thread.interruptOnTimeout:该属性用来配置当HystrixCommand.run()执行超时的时候是否要将它中断
  • execution.isolation.thread.interruptOnCancel:该属性用来配置当HystrixCommand.run()执行被取消的时候是否要将它中断
  • execution.isolation.semaphore.maxConcurrentRequests:当HystrixCommand的隔离策略使用信号量的时候,该属性用来配置

fallback配置

这些属性用来控制HystrixCommand.getFallback()的执行.这些属性同时适用于线程池的信号量的隔离策略

  • fallback.isolation.semaphore.maxConcurrentRequests:该属性用来设置从调用线程中允许HystrixCommand.getFallback()方法执行的最大并发请求数.当达到最大并发请求数时,后续的请求将会被拒绝并抛出异常(因为它已经没有后续的fallback可以被调用了)
  • fallback.enabled:该属性用来设置服务降级策略是否启用,如果设置为false,那么当请求失败或者拒绝发生时,将不会调用HystrixCommand.getFallback()来执行服务降级逻辑

circuitBreaker配置

下面这些是断路器的属性配置,用来控制HystrixCircuitBreaker的行为

  • circuitBreaker.enabled:该属性用来确定当服务请求命令失败时,是否使用断路器来跟踪其健康指标和熔断请求
  • circuitBreaker.requestVolumeThreshold:该属性设置在滚动时间窗中,断路器熔断的最小请求数.
  • circuitBreaker.sleepWindowInMilliseconds:该属性用来设置当断路器打开之后的休眠时间窗.
    例如,默认该值为20的时候,如果滚动时间窗(默认10秒)内仅收到了19个请求,即使这19个请求都失败了,断路器也不会打开
  • circuitBreaker.sleepWindowInMilliseconds:该属性用来设置当断路器打开之后的休眠时间窗.休眠时间窗结束之后,会将断路器置为"半开"状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为"打开"状态,如果成功就设置为"关闭"状态
  • circuitBreaker.errorThresholdPercentage:该属性用来设置断路器打开的错误百分比条件.
  • circuitBreaker.forceOpen:如果将该属性设置为true,断路器将强制进入"打开"状态,它会拒绝所有请求.该属性优于circuitBreaker.forceClosed属性
  • circuitBreaker.forceClosed:如果将该属性设置为true,断路器将强制进入"关闭"状态,它会接收所有请求.如果circuitBreaker.forceOpen属性为true,该属性不会生效

metrics设置

下面的属性均与HystrixCommandHystrixObservableCommand执行中捕获的指标信息有关.

  • metrics.rollingStats.timeInMilliseconds:该属性用来设置滚动时间窗的长度,单位为毫秒.该时间用于断路器判断健康度时需要收集信息的持续时间
  • metrics.rollingStats.numBuckets:该属性用来设置滚动时间窗统计指标信息时划分"桶"的数量
  • metrics.rollingPercentile.enabled:该属性用来设置对命令执行的延迟是否使用百分位数来跟踪和计算,如果设置为false,那么所有的概要统计都将返回-1
  • metrics.rollingPercentile.timeInMilliseconds:该属性用来设置百分位统计的滚动窗口的持续时间,单位为毫秒
  • metrics.rollingPercentile.numBuckets:该属性用来设置百分位统计滚动窗口中使用"桶"的数量
  • metrics.rollingPercentile.bucketSize:该属性用来设置在执行过程中每个"桶"中保留的最大执行次数.如果在滚动时间窗内发生超过该设定值得执行次数,就从最初的位置开始重写
  • metrics.healthSnapShot.intervalInMilliseconds:该属性用来设置采集影响断路器状态的健康快照(请求的成功,错误百分比)的间隔等待时间

requestContext配置

下面这些属性涉及HystrixCommand使用的HystrixRequestContext的配置

  • requestCache.enabled:此属性用来配置是否开启请求缓存
  • requestLog.enabled:该属性用来设置HystrixCommand的执行和事件是否打印日志到HystrixRequestLog

collapser属性

该属性除了在代码中用set和配置文件配置之外,也可使用注解进行配置.可使用@HystrixCollapser中的collapserProperties属性来设置,比如:

@HystrixCollapser(batchMethod = "batch",collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds",value = "20")})

下面这些属性用来控制命令合并相关的行为

  • maxRequestsInBatch:该参数用来设置一次请求合并处理中允许的最大请求数.
  • timerDelayInMilliseconds:该参数用来设置批处理过程中每个命令延迟的时间,单位为毫秒
  • requestCache.enabled:该参数用来设置批处理过程中是否开启请求缓存

threadPool属性

该属性除了在代码中用set和属性文件配置之外,还可使用注解进行设置.可使用@HystrixCommand中的threadPoolProperties属性来设置,比如:

@HystrixCommand(fallbackMethod="helloFallback" , commandKey="helloKey",threadPoolProperties={@HystrixProperty(name="coreSize",value="20")}

下面这些属性用来控制Hystrix命令所属线程池的配置

  • coreSize:该参数用来设置执行命令线程池的核心线程数,该值也就是命令执行的最大并发量
  • maxQueueSize:该参数用来设置线程池的最大队列大小,当设置为-1时,线程池将使用SynchronousQueue实现的队列,否则将使用LinkedBlockingQueue实现的队列
  • queueSizeRejectionThreshold:该参数用来为队列设置拒绝阈值.通过该参数,即使队列没有达到最大值也能拒绝请求.该参数主要是对LinkedBlockingQueue队列的补充,因为LinkedBlockingQueue队列不能动态修改它的对象大小,而通过该属性就可以调整拒绝请求的队列大小了
  • metrics.rollingStats.timeInMilliseconds:该参数用来设置滚动时间窗的长度,单位为毫秒.该滚动时间窗的长度用于线程池的指标度量,它会被分成多个"桶"来统计指标
  • metrics.rollingStats.numBuckets:该参数用来设置滚动时间窗被划分成"桶"的数量.
    注:metrics.rollingStats.timeInMilliseconds参数的设置必须能够被metrics.rollingStats.numBuckets参数整除,不然将会抛出异常

Hystrix仪表盘

构建一个Hystrix Dashboard,需要四步:

  • 创建一个标准的Spring Boot工程,命名为hystrix-dashboard
  • 编辑pom.xml,内容如下:
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
	<version>1.4.6.RELEASE</version>
</dependency>
  • 为应用主类加上@EnableHystrixDashboard,启用Hystrix Dashboard功能
  • 根据实际情况修改application.properties配置文件
spring.application.name=hystrix-dashboard
server.port=2001

启动应用,看到如下页面:
Hystrix Dashboard
首页的两个参数,Delay和Title:

  • Delay:该参数用来控制服务器上轮询监控信息的延迟时间,可以通过配置该属性来降低客户端的网络和CPU消耗
  • Title:该参数对应了Hystrix Stream之后的内容,可以通过该配置展示更合适的标题
    HystrixDashboard支持三种不同的监控方式:
    1)默认的集群监控:通过URL http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控
    2)指定的集群监控:通过URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控.
    3)单体应用的监控:通过URL http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控
    1.监控单实例节点,需要两步:
    第一步,在服务消费方的pom.xml中新增依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
	<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

第二步,在服务消费方的主类中使用@EnableCircuitBreaker注解,开启断路器功能
重启服务消费方,在Hystrix Dashboard的首页输入http://localhost:9000/hystrix.stream,可以看到如下页面在这里插入图片描述

Turbine 集群监控

构建监控聚合服务

1.引入Turbine来聚合RIBBON-CONSUMER服务的监控信息,并输出给Hystrix Dashboard来进行展示,具体步骤如下:

  • 创建一个标准的Spring boot工程,命名为turbine
  • 编辑pom.xml,引入依赖
<parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-parent</artifactId>
		<version>Brixton.SR5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.didispace</groupId>
	<artifactId>hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hello</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-turbine -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-turbine</artifactId>
		</dependency>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
  • 创建主类,并使用@EnableTurbine注解开启Turbine
@EnableEurekaClient
@EnableTurbine
@SpringBootApplication
public class HelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloApplication.class, args);
	}
}
  • 在application.properties中加入Eureka和Turbine的配置
spring.application.name=turbine

server.port=8989
management.port=8990

eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

turbine.app-config=RIBBON-CONSUMER
turbine.cluster-name-expression="default"
turbine.combine-host-port=true

turbine.app-config:该参数指定了需要收集监控信息的服务名
turbine.cluster-name-expression:该参数指定了集群名称为default
turbine.combine-host-port:该参数设置为true,可以让同一主机上的服务通过主机名和端口号的组合来进行区分
启动eureka-server/HELLO-SERVER/RIBBON-CONSUMER/Turbine/Hystrix Dashboard,页面如下:
Turbine

与消息代理结合

实现基于消息代理的Turbine聚合服务
1)创建一个标准的Spring Boot工程,命名为turbine-amqp
2)编辑pom.xml,导入依赖

<parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-parent</artifactId>
		<version>Brixton.SR5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.didispace</groupId>
	<artifactId>hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hello</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-turbine-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

3)在应用类中使用@EnableTurbineStream注解来启用Turbine Stream的配置

@EnableTurbineStream
@EnableEurekaClient
@SpringBootApplication
public class HelloApplication {

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

}

4)配置application.properties文件

spring.application.name=turbine
server.port=8989
management.port=8990
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

5)服务消费者也要进行更改pom.xml,使其监控信息能够输出到RabbitMQ上.

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
	<version>1.4.6.RELEASE</version>
</dependency>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值