熔断:用来避免微服务架构中雪崩现象,达到某个阈值条件之后自动出发熔断
原理:当监控到调用链路中某一个服务,出现异常(20个以上异常)自动出发熔断,在出发熔断之后对于该微服务调用不可用
RT:根据请求响应时间熔断
异常比例:根据请求调用过程中出现异常百分比进行熔断
异常数:根据请求调用过程中异常数进行熔断
平均响应时间
当 1s 内持续进入 N 个请求,对应时刻的平均响应时间(秒级)均超过阈值(count,以 ms 为单位),那么在接下的时间窗口(DegradeRule 中的 timeWindow,以 s 为单位)之内,对这个方法的调用都会自动地熔断(抛出 DegradeException)。注意 Sentinel 默认统计的 RT 上限是 4900 ms,超出此阈值的都会算作 4900 ms,若需要变更此上限可以通过启动配置项 -Dcsp.sentinel.statistic.max.rt=xxx 来配置。
异常比例
当资源的每秒请求量 >= N(可配置),并且每秒异常总数占通过量的比值超过阈值(DegradeRule 中的 count)之后,资源进入降级状态,即在接下的时间窗口(DegradeRule 中的 timeWindow,以 s 为单位)之内,对这个方法的调用都会自动地返回。异常比率的阈值范围是 [0.0, 1.0],代表 0% - 100%
异常数
当资源近 1 分钟的异常数目超过阈值之后会进行熔断。注意由于统计时间窗口是分钟级别的,若 timeWindow 小于 60s,则结束熔断状态后仍可能再进入熔断状态。
热点:经常访问的数据称之为热点
热点限流:也称之为热点参数限流,日后访问资源中携带了指定参数进行限流
@SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:
注意:使用热点参数限流时,不能使用资源路径,必须使用资源别名,sentinel提供资源别名注解 @SentinelResource(value = "别名")
package com.study.springcloudAlibaba.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @RequestMapping("/demo/{id}") //blockHandler = "" 使用sentinel进行不同规则控制时的默认处理方案 //fallback = "" 自定义业务出错时默认处理方案 //defaultFallback = "" 指定一个业务错误时默认方案 @SentinelResource(value = "aaa",blockHandler = "blockHandler",fallback = "fallCustomer",defaultFallback = "fall") //作用代表这是一个sentinel资源 public String demo(@PathVariable("id") int id){ if (id<0)throw new RuntimeException("id无效"); return "demo ok!!!id="+id; } public String blockHandler(int id, BlockException e){ if (e instanceof FlowException){ return "当前请求过于火爆,您已被流量控制!!!"; } if (e instanceof DegradeException){ return "当前请求过于火爆,您已被降级!!!"; } if (e instanceof ParamFlowException){ return "当前请求过于火爆,您已被热点参数限流!!!"; } return "服务器快爆了,请稍后再试!!!"; } public String fallCustomer(int id){ return "自定义处理----我们服务器出错了"+id; } public String fall(int id){ return "默认处理----我们服务器出错了"+id; } @RequestMapping("/test") public String test(){ return "test ok!!!"; } }
一、
二、
三、
四、
五、