合肥住房和建设厅网站首页,狠狠做新网站,seo管理软件,汕头模板网建站前期回顾#xff1a;
【Zero to One系列】springcloud微服务集成nacos#xff0c;形成分布式系统
【Zero to One系列】SpringCloud Gateway结合Nacos完成微服务的网关路由 1、hystrix依赖包
首先引入hystrix相关的依赖包#xff0c;版本方面自己和项目内相对应即可#… 前期回顾
【Zero to One系列】springcloud微服务集成nacos形成分布式系统
【Zero to One系列】SpringCloud Gateway结合Nacos完成微服务的网关路由 1、hystrix依赖包
首先引入hystrix相关的依赖包版本方面自己和项目内相对应即可我这这边是直接使用的默认版本。
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactId
/dependency
然后演示的微服务还是以我架构项目中的2个子模块为例user模块与send模块
user模块调用方
send模块被调用方 2、hystrix配置
2.1 feign集成hystrix熔断统一配置
首先写一个send模块的feign接口fallback实现类例如
package com.***.common.hystrix;import com.***.common.bean.ResultBean;
import com.***.common.enums.ResponseCodeEnum;
import com.***.common.feign.SendFeignClient;
import org.springframework.stereotype.Component;Component
public class SendFallBack implements SendFeignClient {Overridepublic ResultBean sendSMS() {//todo 可以在对应的方法内写入使用统一降级策略或是指定的降级策略处理return ResultBean.returnResult(ResponseCodeEnum.SUCCESS,发送服务暂时断开连接请稍后。。。);}
}
然后在send模块的feign接口的注解上直接配置对应的降级实现类如下
FeignClient(name ${sys.serviceName.send},path ${sys.serviceContext.send},fallback SendFallBack.class)
最后在user模块中配置上 feign.hystrix.enabledtrue 现在启动2个微服务我们测试一下效果 1、显示2个服务都正常的情况下调用结果如下 2、通过nacos的管理界面我们手动下线send模块服务或是手动停止send服务模拟服务宕机异常情况再看下请求效果 可以看到通过feign集成的hystrix配置的降级处理已经生效了。 2.2 指定接口自定义熔断配置
这里以send模块为例。。在启动类上先加上注解 EnableHystrix 然后写一个测试方法如下
HystrixCommand(fallbackMethod failResult,commandProperties {HystrixProperty(name execution.isolation.thread.timeoutInMilliseconds, value 1000),HystrixProperty(name fallback.enabled, value true)})
PostMapping(value /testHystrix)
public ResultBean testHystrix() throws InterruptedException {//线程睡眠2秒模拟接口响应慢的情况Thread.sleep(2000);return ResultBean.returnResult(ResponseCodeEnum.SUCCESS,send服务正常。。。);
}private ResultBean failResult(){return ResultBean.returnResult(ResponseCodeEnum.FAIL,send服务暂不可用。。。);
}
启动后我们测试一下 可以看到接口的执行已经进入到的降级的fallback方法中了。
这里我贴一下HystrixCommand详细配置说明可以参考使用
Command属性主要用来控制HystrixCommand命令的行为它主要分下面的类别1、Execution用来控制HystrixCommand.run()的执行
execution.isolation.strategy该属性用来设置HystrixCommand.run()执行的隔离策略。默认为THREAD。
execution.isolation.thread.timeoutInMilliseconds该属性用来配置HystrixCommand执行的超时时间单位为毫秒。
execution.timeout.enabled该属性用来配置HystrixCommand.run()的执行是否启用超时时间。默认为true。
execution.isolation.thread.interruptOnTimeout该属性用来配置当HystrixCommand.run()执行超时的时候是否要它中断。
execution.isolation.thread.interruptOnCancel该属性用来配置当HystrixCommand.run()执行取消时是否要它中断。
execution.isolation.semaphore.maxConcurrentRequests当HystrixCommand命令的隔离策略使用信号量时该属性用来配置信号量的大小。当最大并发请求达到该设置值时后续的请求将被拒绝。2、Fallback用来控制HystrixCommand.getFallback()的执行
fallback.isolation.semaphore.maxConcurrentRequests该属性用来设置从调用线程中允许HystrixCommand.getFallback()方法执行的最大并发请求数。当达到最大并发请求时后续的请求将会被拒绝并抛出异常。
fallback.enabled该属性用来设置服务降级策略是否启用默认是true。如果设置为false当请求失败或者拒绝发生时将不会调用HystrixCommand.getFallback()来执行服务降级逻辑。3、Circuit Breaker用来控制HystrixCircuitBreaker的行为。
circuitBreaker.enabled确定当服务请求命令失败时是否使用断路器来跟踪其健康指标和熔断请求。默认为true。
circuitBreaker.requestVolumeThreshold用来设置在滚动时间窗中断路器熔断的最小请求数。例如默认该值为20的时候如果滚动时间窗默认10秒内仅收到19个请求即使这19个请求都失败了断路器也不会打开。
circuitBreaker.sleepWindowInMilliseconds用来设置当断路器打开之后的休眠时间窗。休眠时间窗结束之后会将断路器设置为“半开”状态尝试熔断的请求命令如果依然时候就将断路器继续设置为“打开”状态如果成功就设置为“关闭”状态。
circuitBreaker.errorThresholdPercentage该属性用来设置断路器打开的错误百分比条件。默认值为50表示在滚动时间窗中在请求值超过requestVolumeThreshold阈值的前提下如果错误请求数百分比超过50就把断路器设置为“打开”状态否则就设置为“关闭”状态。
circuitBreaker.forceOpen该属性默认为false。如果该属性设置为true断路器将强制进入“打开”状态它会拒绝所有请求。该属性优于forceClosed属性。
circuitBreaker.forceClosed该属性默认为false。如果该属性设置为true断路器强制进入“关闭”状态它会接收所有请求。如果forceOpen属性为true该属性不生效。4、Metrics该属性与HystrixCommand和HystrixObservableCommand执行中捕获的指标相关。
metrics.rollingStats.timeInMilliseconds该属性用来设置滚动时间窗的长度单位为毫秒。该时间用于断路器判断健康度时需要收集信息的持续时间。断路器在收集指标信息时会根据设置的时间窗长度拆分成多个桶来累计各度量值每个桶记录了一段时间的采集指标。例如当为默认值10000毫秒时断路器默认将其分成10个桶每个桶记录1000毫秒内的指标信息。
metrics.rollingStats.numBuckets用来设置滚动时间窗统计指标信息时划分“桶”的数量。默认值为10。
metrics.rollingPercentile.enabled用来设置对命令执行延迟是否使用百分位数来跟踪和计算。默认为true如果设置为false那么所有的概要统计都将返回-1。
metrics.rollingPercentile.timeInMilliseconds用来设置百分位统计的滚动窗口的持续时间单位为毫秒。
metrics.rollingPercentile.numBuckets用来设置百分位统计滚动窗口中使用桶的数量。
metrics.rollingPercentile.bucketSize用来设置每个“桶”中保留的最大执行数。
metrics.healthSnapshot.intervalInMilliseconds用来设置采集影响断路器状态的健康快照的间隔等待时间。5、Request Context涉及HystrixCommand使用HystrixRequestContext的设置。
requestCache.enabled用来配置是否开启请求缓存。
requestLog.enabled用来设置HystrixCommand的执行和事件是否打印到日志的HystrixRequestLog中。3、hystrix的配置参数说明
1.HystrixCommandProperties用于HystrixCommand配置一个HystrixCommandKey对应一个HystrixCommandProperties实例。
2.HystrixThreadPoolProperties用于HystrixThreadPool配置一个HystrixThreadPoolKey对应一个HystrixThreadPoolProperties实例。
3.HystrixCollapserProperties用于HystrixCollapserCommand配置一个HystrixCollapserKey对应一个HystrixCollapserProperties实例。 类别 配置项 默认值 HystrixCommandProperties hystrix.threadpool.[commandkey].circuitBreaker.enabled true hystrix.threadpool.[commandkey].circuitBreaker.requestVolumeThreshold 20 hystrix.threadpool.[commandkey].circuitBreaker.sleepWindowInMilliseconds 5000 hystrix.threadpool.[commandkey].circuitBreaker.errorThresholdPercentage 50 hystrix.threadpool.[commandkey].circuitBreaker.forceOpen false hystrix.threadpool.[commandkey].circuitBreaker.forceClosed false hystrix.threadpool.[commandkey].execution.isolation.strategy Thread hystrix.threadpool.[commandkey].execution.isolation.thread.timeoutInMilliseconds 1000 hystrix.threadpool.[commandkey].execution.timeout.enabled true hystrix.threadpool.[commandkey].execution.isolation.thread.interruptOnTimeout true hystrix.threadpool.[commandkey].execution.isolation.thread.interruptOnFutureCancel false hystrix.threadpool.[commandkey].execution.isolation.semaphore.maxConcurrentRequests 10 hystrix.threadpool.[commandkey].fallback.isolation.semaphore.maxConcurrentRequests 10 hystrix.threadpool.[commandkey].fallback.enabled true hystrix.threadpool.[commandkey].metrics.rollingStats.timeInMilliseconds 10000 hystrix.threadpool.[commandkey].metrics.rollingStats.numBuckets 10 hystrix.threadpool.[commandkey].metrics.rollingPercentile.enabled true hystrix.threadpool.[commandkey].metrics.rollingPercentile.timeInMilliseconds 60000 hystrix.threadpool.[commandkey].metrics.rollingPercentile.numBuckets 6 hystrix.threadpool.[commandkey].metrics.rollingPercentile.bucketSize 100 hystrix.threadpool.[commandkey].metrics.healthSnapshot.intervalInMilliseconds 500 hystrix.threadpool.[commandkey].requestCache.enabled true hystrix.threadpool.[commandkey].requestLog.enabled true hystrix.threadpool.[commandkey].threadPoolKeyOverride HystrixThreadPoolProperties hystrix.threadpool.[threadPoolkey].coreSize 10 hystrix.threadpool.[threadPoolkey].allowMaximumSizeToDivergeFromCoreSize false hystrix.threadpool.[threadPoolkey].maximumSize 10 hystrix.threadpool.[threadPoolkey].keepAliveTimeMinutes 1 hystrix.threadpool.[threadPoolkey].maxQueueSize -1 hystrix.threadpool.[threadPoolkey].queueSizeRejectionThreshold 5 hystrix.threadpool.[threadPoolkey].metrics.rollingStats.timeInMilliseconds 10000 hystrix.threadpool.[threadPoolkey].metrics.rollingStats.numBuckets 10 HystrixCollapserProperties hystrix.collapser.[collapserCommandkey].maxRequestsInBatch Integer.MAX_VALUE hystrix.collapser.[collapserCommandkey].timerDelayInMilliseconds 10 hystrix.collapser.[collapserCommandkey].requestCache.enabled true hystrix.collapser.[collapserCommandkey].metrics.rollingStats.timeInMilliseconds 10000 hystrix.collapser.[collapserCommandkey].metrics.rollingStats.numBuckets 10 hystrix.collapser.[collapserCommandkey].metrics.rollingPercentile.enabled true hystrix.collapser.[collapserCommandkey].metrics.rollingPercentile.timeInMilliseconds 60000 hystrix.collapser.[collapserCommandkey].metrics.rollingPercentile.numBuckets 6 hystrix.collapser.[collapserCommandkey].metrics.rollingPercentile.bucketSize 100 内部每个属性由一个ChainHystrixProperty表示ChainHystrixProperty是一个串联的HystrixDynamicProperty持续获取串中的属性值直到获得不为null值为止。ChainHystrixProperty串联的HystrixDynamicProperty默认通过插件获取的HystrixDynamicProperties获取最后我们会讲到插件。 HystrixDynamicProperty表示动态配置数据如果配置源发送变化通过该对象获取配置也会相应变化。hystrix中有种实现类
1.通过archaius实现获取配置项通过HystrixDynamicPropertiesArchaius创建该类HystrixDynamicProperty。
2.通过system实现获取配置项通过HystrixDynamicPropertiesSystemProperties创建该类HystrixDynamicProperty。
还有yaml配置
hystrix:command:#全局默认配置default:execution:timeout:#是否给方法执行设置超时时间默认为true。一般我们不要改。enabled: trueisolation:#配置请求隔离的方式这里是默认的线程池方式。还有一种信号量的方式semaphore使用比较少。strategy: threadPoolsemaphore:maxConcurrentRequests: 1000thread:#方式执行的超时时间默认为1000毫秒在实际场景中需要根据情况设置timeoutInMilliseconds: 60000#发生超时时是否中断方法的执行默认值为true。不要改。interruptOnTimeout: true#是否在方法执行被取消时中断方法默认值为false。没有实际意义默认就好interruptOnCancel: false#熔断器相关配置##并发执行的最大线程数默认10coreSize: 200#说明是否允许线程池扩展到最大线程池数量默认为false。allowMaximumSizeToDivergeFromCoreSize: true#说明线程池中线程的最大数量默认值是10。此配置项单独配置时并不会生效需要启用allowMaximumSizeToDivergeFromCoreSizemaximumSize: 200#说明1作业队列的最大值默认值为-1。表示队列会使用SynchronousQueue此时值为0Hystrix不会向队列内存放作业。#说明2如果此值设置为一个正int型队列会使用一个固定size的LinkedBlockingQueue此时在核心线程池都忙碌的情况下会将作业暂时存放在此队列内但是超出此队列的请求依然会被拒绝maxQueueSize: 20000#设置队列拒绝请求的阀值默认为5。queueSizeRejectionThreshold: 30000circuitBreaker:#说明是否启动熔断器默认为true。我们使用Hystrix的目的就是为了熔断器不要改否则就不要引入Hystrix。enabled: true#说明1启用熔断器功能窗口时间内的最小请求数假设我们设置的窗口时间为10秒#说明2那么如果此时默认值为20的话那么即便10秒内有19个请求都失败也不会打开熔断器。#说明3此配置项需要根据接口的QPS进行计算值太小会有误打开熔断器的可能而如果值太大超出了时间窗口内的总请求数则熔断永远也不会被触发#说明4建议设置一般为QPS*窗口描述*60%requestVolumeThreshold: 3000#说明1熔断器被打开后所有的请求都会被快速失败掉但是何时恢复服务是一个问题。熔断器打开后Hystrix会在经过一段时间后就放行一条请求#说明2如果请求能够执行成功则说明此时服务可能已经恢复了正常那么熔断器会关闭相反执行失败则认为服务仍然不可用熔断器保持打开。#说明3所以此配置的作用是指定熔断器打开后多长时间内允许一次请求尝试执行官方默认配置为5秒。sleepWindowInMilliseconds: 5000#说明1:该配置是指在通过滑动窗口获取到当前时间段内Hystrix方法执行失败的几率后根据此配置来判断是否需要打开熔断器#说明2:这里官方的默认配置为50即窗口时间内超过50%的请求失败后就会打开熔断器将后续请求快速失败掉errorThresholdPercentage: 70#说明是否强制启用熔断器默认false没有什么场景需要这么配置忽略forceOpen: false#说明是否强制关闭熔断器默认false没有什么场景需要这么配置忽略forceClosed: false
对于hystrix的实现原理感兴趣的可以看下我往期的博文
【SpringCloud微服务系列】Hystrix熔断器底层原理 至此springcloud微服务集成hystrix熔断器的操作流程就记录完啦。后续还会有分布式链路追踪的实现集成感兴趣的话请帮忙一键三连谢谢~~~