【SpringCloud】SpringCloud Alibaba Sentinel实现熔断与限流

【SpringCloud】SpringCloud Alibaba Sentinel实现熔断与限流,第1张

概述SpringCloud Alibaba Sentinel实现熔断与限流 限流与降级 限流 blockHandler 降级 fallback 降级需要运行时出现异常才会触发,而限流一旦触发,你连运行的机

SpringCloud Alibaba Sentinel实现熔断与限流

限流与降级

限流 blockHandler
降级 fallback
降级需要运行时出现异常才会触发,而限流一旦触发,你连运行的机会都没有,当然就不会降级。
也就是说,两者如果同时触发,那么一定是限流触发(降级连机会都没有)。

SentIEl官网

https://github.com/alibaba/Sentinel
中文:https://github.com/alibaba/Sentinel/wiki/介绍

是什么

一句话解释就是我们之前讲过的hystrix

去哪下

https://github.com/alibaba/Sentinel/releases

能干嘛

怎么玩

官方文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.HTML#_spring_cloud_alibaba_sentinel

服务中的各种问题

服务雪崩服务降级服务熔断服务限流安装SentIEl控制台sentinel组件由两部分构成

后台前台8080安装步骤下载

https://github.com/alibaba/Sentinel/releases

运行命令前提java8环境OK8080端口不能被占用命令

java -jar sentinel-dashboard-1.7.0.jar

访问sentinel管理界面

http://localhost:8080

登录账号密码均为sentinel

初始化演示功能启动Nacos8848成功

http://localhost:8848/nacos/#/login

Modulecloudalibaba-sentinel-service8401POM
<dependencIEs>    <!-- SpringCloud ailibaba nacos-->    <dependency>        <groupID>com.alibaba.cloud</groupID>        <artifactID>spring-cloud-starter-alibaba-nacos-discovery</artifactID>    </dependency>    <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->    <dependency>        <groupID>com.alibaba.csp</groupID>        <artifactID>sentinel-datasource-nacos</artifactID>    </dependency>    <!-- SpringCloud ailibaba sentinel-->    <dependency>        <groupID>com.alibaba.cloud</groupID>        <artifactID>spring-cloud-starter-alibaba-sentinel</artifactID>    </dependency>    <dependency>        <groupID>org.springframework.cloud</groupID>        <artifactID>spring-cloud-starter-openfeign</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-actuator</artifactID>    </dependency>    <!--热部署-->    <dependency>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-devtools</artifactID>        <scope>runtime</scope>        <optional>true</optional>    </dependency>    <dependency>        <groupID>org.projectlombok</groupID>        <artifactID>lombok</artifactID>        <optional>true</optional>    </dependency>    <dependency>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-starter-test</artifactID>        <scope>test</scope>    </dependency></dependencIEs>
YML
server:  port: 8401spring:  application:    name: cloudalibaba-sentinal-service  cloud:    nacos:      discovery:        #Nacos服务注册中心地址        server-addr: localhost:8848    sentinel:      transport:        #配置Sentin dashboard地址        dashboard: localhost:8080        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口        port: 8719management:  endpoints:    web:      exposure:        include: '*'
主启动
@EnablediscoveryClIEnt@SpringBootApplicationpublic class MainApp8401 {    public static voID main(String[] args) {        SpringApplication.run(MainApp8401.class,args);    }}
业务类FlowlimitController
public class FlowlimitController {    @GetMapPing("/testA")    public String testA() {        return "----testA";    }    @GetMapPing("/testB")    public String testB() {        return "----testB";    }}
启动Sentinel8080

java -jar sentinel-dashboard-1.7.0.jar

启动微服务8401启动8401微服务后台查看sentinel控制台空空如也,啥也没有Sentinel采用懒加载说明执行一次访问

http://localhost:8401/testA
http://localhost:8401/testB

效果

结论

sentinel8080正在监控微服务8401

流控规则基本介绍

进一步解释说明


流控模式直接(默认)直接->快速失败系统默认配置及说明

测试快速点击访问http://localhost:8401/testA结果:Blocked by Sentinel(flow limiting)思考???
直接调用默认报错信息,技术方面ok,but,是否应该有我们自己的后续处理
类似有个fallback的兜底方法关联

应用场景:比如支付时达到阈值,可以从源头上比如购买界面,进行限流
类比:下流洪灾,上流关水

是什么当关联的资源达到阈值时,就限流自己当与A关联的资源B达到阈值后,就限流自己B惹事,A挂了配置A

postman模拟并发密集访问testB

访问B成功

postman里新建多线程集合组

将访问地址添加进新线程组

RUN

大批量线程高并发访问B,导致A失效了

运行后发现testA挂了点击访问A结果:Blocked by Sentinel(flow limiting)链路多个请求调用同一个微服务家庭作业试试流控效果直接->快速失败(默认的流控处理)直接失败,抛出异常
Blocked by Sentinel(flow limiting)源码
com.alibaba.csp.sentinel.slots.block.controller.DefaultController预热说明

公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值

官网

默认coldFactor为3,即请求QPS从threshold/3开始,经预热时长逐渐升至设定的QPS阈值限流 冷启动
https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81---%E5%86%B7%E5%90%AF%E5%8A%A8源码

com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController

WarmUp配置

多次点击http://localhost:8401/testB

刚开始不行,后续慢慢OK

应用场景

如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,颍热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。

排队等待

匀速排队,阈值必须设置为QPS官网

源码

com.ailibaba.csp.sentinel.slots.block.controller.RatelimiterController

测试

降级规则官网

https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7

基本介绍

进一步说明Sentinel的断路器是没有半开状态的

Sentinel熔断隆级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联错误。

当资源被降级后,在接下来的降级时间窗口之内,对该资源的调用都自动熔断(默认行为是抛出DegradeException)。

@H_419_373@半开的状态系统自动去检测是否请求有异常,没有异常就关闭断路器恢复使用,有异常则继续打开断路器不可用,具体参考Hystrix复习Hystrix

降级策略实战RT是什么


测试代码

@H_45_403@

配置

jmeter压测结论

@H_613_419@

异常比例是什么


测试代码

配置

jmeter

结论

异常数是什么


异常数是按照分钟统计的测试代码

同异常比例

配置

jmeter热点key限流基本介绍是什么

官网

https://github.com/alibaba/Sentinel/wiki/%E7%83%AD%E7%82%B9%E5%8F%82%E6%95%B0%E9%99%90%E6%B5%81

承上启下复习start

SentinelResource

兜底方法
分为系统默认和客户自定义,两种

之前的case,限流出问题后,都是用sentine|系统默认的提示: Blocked by Sentinel (flow limiting)

我们能不能自定?类似hystrix,某个方诎问题了,就找对应的兜底降级方法?

结论
从HystrixCommand到@SentinelResource

代码

com.alibaba.csp.sentinel.slots.block.BlockException

配置配置

1@SentinelResource(value = "testHotKey")异常打到了前台用户界面看到,不友好2@SentinelResource(value = "testHotKey",blockHandler="dealHandler_testHotKey")方法testHotKey里面第一个参数只要QPS超过每秒一次,马上降级处理用了我们自己定义的测试× error
http://localhost:8401/testHotKey?p1=abc× error
http://localhost:8401/testHotKey?p1=abc&p2=33√ right
http://localhost:8401/testHotKey?p2=abc参数例外项上述案例演示了第一个参数p1,当QPS超过1秒1次点击后马上被限流特殊情况普通超过1秒钟一个后,达到阈值1后马上被限流我们期望p1参数当它是某个特殊值时,它的限流值和平时不一样特例:假如当p1的值等于5时,它的阈值可以达到200配置


添加
按钮不能忘

测试√ http://localhost:8401/testHotKey?p1=5× http://localhost:8401/testHotKey?p1=3当p1等于5的时候,阈值变为200当p1不等于5的时候,阈值就是平常的1前提条件

热点参数的注意点,参数必须是基本类型或者String

其他

手贱添加异常看看o(╥﹏╥)o
后面讲

@SentinelResource
处理的是Sentinel控制台配置的违规情况,有blockHandler方法配置的兜底处理;

RuntimeException
int age = 10/0,这个是java运行时报出的运行时异常RunTimeException,@SentinelResource不管

总结
@SentinelResource主管配置出错,运行出错该走异常走异常

系统规则是什么

https://github.com/alibaba/Sentinel/wiki/ 系统自适应限流

各项配置说明

配置全局QPS

不合适,使用危险,一竹竿打死一船人@SentinelResource按资源名称限流+后续处理启动nacos成功启动Sentinel成功Modulecloudalibaba-sentinel-service8401pom
<dependency><!-- 引用自己定义的API通用包,可以使用Payment支付Entity -->    <groupID>com.eiletxIE.springcloud</groupID>    <artifactID>cloud-API-commons</artifactID>    <version>${project.version}</version></dependency>
yml业务类RatelimitController
@RestControllerpublic class RatelimitController {    @GetMapPing("/byResource")    @SentinelResource(value = "byResource",blockHandler = "handleException")    public CommonResult byResource() {        return  new CommonResult(200,"按照资源名称限流测试",new Payment(2020L,"serial001"));    }    public CommonResult handleException(BlockException exception) {        return  new CommonResult(444,exception.getClass().getCanonicalname() + "\t 服务不可用");    }}
主启动配置流控规则配置步骤

图形配置和代码关系表示1秒钟内查询次数大于1,就跑到我们自定义的限流处,限流测试1秒钟点击1下,OK超过上述,疯狂点击,返回了自己定义的限流处理信息,限流发生

额外问题此时关闭服务8401看看Sentinel控制台,流控规则消失了?
临时?持久?按照URL地址限流+后续处理通过访问URL来限流,会返回Sentinel自带默认的限流处理信息业务类RatelimitController访问一次Sentinel控制台配置

测试疯狂点击http://localhost:8401/ratelimit/byUrl结果

上面兜底方案面临的问题系统默认的,没有体现我们自己的业务要求。依照现有条件,我们自定义的处理方法又和业务代码耦合在-块,不直观。每个业务方法都添加一个兜底的,那代码膨胀加剧。全局统一的处理方法没有体现。客户自定义限流处理逻辑创建CustomerBlockHandler类用于自定义限流处理逻辑自定义限流处理类

CustomerBlockHandler

RatelimitController启动微服务后再调用一次

http://localhost:8401/ratelimit/customerBlockHandler

Sentinel控制台配置测试后我们的自定义出来了进一步说明

更多注解说明

https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81

多说一句

Sentinel主要有三个核心APIsphU定义资源Tracer定义统计ContextUtil定义了上下文服务熔断功能sentinel整合ribbon+openFeign+fallbackRibbon系列启动nacos和sentinel提供者9003/9004新建cloudalibaba-provIDer-payment9003/9004POM
<dependencIEs>    <!-- SpringCloud ailibaba nacos-->    <dependency>        <groupID>com.alibaba.cloud</groupID>        <artifactID>spring-cloud-starter-alibaba-nacos-discovery</artifactID>    </dependency>    <!-- SpringCloud ailibaba sentinel-->    <dependency>        <groupID>com.alibaba.cloud</groupID>        <artifactID>spring-cloud-starter-alibaba-sentinel</artifactID>    </dependency>    <dependency><!-- 引用自己定义的API通用包,可以使用Payment支付Entity -->        <groupID>com.eiletxIE.springcloud</groupID>        <artifactID>cloud-API-commons</artifactID>        <version>${project.version}</version>    </dependency>    <dependency>        <groupID>org.springframework.cloud</groupID>        <artifactID>spring-cloud-starter-openfeign</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-actuator</artifactID>    </dependency>    <!--热部署-->    <dependency>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-devtools</artifactID>        <scope>runtime</scope>        <optional>true</optional>    </dependency>    <dependency>        <groupID>org.projectlombok</groupID>        <artifactID>lombok</artifactID>        <optional>true</optional>    </dependency>    <dependency>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-starter-test</artifactID>        <scope>test</scope>    </dependency></dependencIEs>
YML
server:  port: 9003spring:  application:    name: nacos-payment-provIDer  cloud:    nacos:      discovery:        server-addr: localhost:8848management:  endpoints:    web:      exposure:        include: '*'
主启动
@SpringBootApplication@EnablediscoveryClIEntpublic class PaymentMain9003 {    public static voID main(String[] args) {        SpringApplication.run(PaymentMain9003.class,args);    }}
业务类
@RestControllerpublic class PaymentController {    @Value("${server.port}")    private  String serverPort;    public static HashMap<Long,Payment > map = new HashMap<>();    static {        map.put(1L,new Payment(1L,"1111"));        map.put(1L,new Payment(2L,"2222"));        map.put(1L,new Payment(3L,"3333"));    }    @GetMapPing(value = "/paymentsql/{ID}")    public CommonResult<Payment> paymentsql(@PathVariable("ID") Long ID) {        Payment payment = map.get(ID);        CommonResult<Payment> result = new CommonResult<>(200,"from MysqL,serverPort: " + serverPort,payment);        return result;    }}
测试地址

http://localhost:9003/paymentSQ/

消费者84新建cloudalibaba-consumer-nacos-order84POM

与提供者pom一致

YML
server:  port: 84spring:  application:    name: nacos-order-consumer  cloud:    nacos:      discovery:        server-addr: localhost:8848    sentinel:      transport:        dashboard: localhost:8080        port: 8719#消费者将去访问的微服务名称server-url:  nacos-user-service: http://nacos-payment-provIDer
主启动
@EnablediscoveryClIEnt@SpringBootApplicationpublic class OrderMain84 {    public static voID main(String[] args) {        SpringApplication.run(OrderMain84.class,args);    }}
业务类ApplicationContextConfig
@Configurationpublic class ApplicationContextConfig {    @Bean    @LoadBalanced    public RestTemplate getRestTemplate() {        return new RestTemplate();    }}
CircleBreakerController
public class CircleBreakerController {    public static  final  String SERVICE_URL = "http://nacos-payment-provIDer";    @Resource    private RestTemplate restTemplate;    @RequestMapPing("/consumer/fallback/{ID}")    @SentinelResource(value = "fallback")    public CommonResult<Payment> fallback(@PathVariable Long ID) {        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentsql/" + ID,CommonResult.class,ID);        if(ID == 4){            throw new IllegalArgumentException("IllegalArgument,非法参数异常...");        }else if(result.getData() == null) {            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");        }        return  result;    }}
修改后请重启微服务热部署对java代码级生效及时对@SentinelResource注解内属性,有时效果不好@H_301_878@目的fallback管运行异常blockHandler管配置违规测试地址

http://localhost:84/consumer/fallback/3

没有任何配置

直接给用户error界面。,,不太友好

只配置fallback
  @RequestMapPing("/consumer/fallback/{ID}")//    @SentinelResource(value = "fallback")    @SentinelResource(value = "fallback",fallback = "handlerFallback")    public CommonResult<Payment> fallback(@PathVariable Long ID) {        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentsql/" + ID,该ID没有对应记录,空指针异常");        }        return  result;    }    public CommonResult handlerFallback(@PathVariable Long ID,Throwable e) {        Payment payment = new Payment(ID,"null");        return new CommonResult(444,"异常handlerFallback,exception内容: " + e.getMessage(),payment);    }

结果

只配置blockHandler

编码

  @RequestMapPing("/consumer/fallback/{ID}")//    @SentinelResource(value = "fallback")//    @SentinelResource(value = "fallback",fallback =    @SentinelResource(value = "fallback",blockHandler = "blockHandler")    public CommonResult<Payment> fallback(@PathVariable Long ID) {        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentsql/" + ID,该ID没有对应记录,空指针异常");        }        return  result;    } public CommonResult blockHandler(@PathVariable Long ID,BlockException e) {    Payment payment = new Payment(ID,"null");    return new CommonResult(444,"blockHandler-sentinel 限流,BlockException: " + e.getMessage(),payment);}

记得配置一个qps的控制

结果

同时配置fallback和blockHandler

异常忽略

Feign系列修改84模块84消费者调用提供者9003Feign组件一般是消费端POM
<dependency>    <groupID>org.springframework.cloud</groupID>    <artifactID>spring-cloud-starter-openfeign</artifactID></dependency>
YML业务类带@FeignClIEnt注解的业务接口fallback = PaymentFallbackService.classController主启动

添加@EnableFeignClIEnts启动Feign的功能

http://localhost:84/consumer/paymentsql/2测试84调用9003,此时故意关闭9003微服务提供者,看84消费自动降级,不会被耗死熔断框架比较
SentinelHystrixresilIEnce4j
隔离策略信号量隔离(并发线程数限流)线程池隔离/信号量隔离信号量隔离
熔断降级策略基于响应时间、异常比率、异常数基于异常比率基于异常比率、响应时间
实时统计实现滑动窗口(LeapArray)滑动窗口(基于RxJava)Ring Bit Buffer
动态规则配置支持多种数据源支持多种数据源有限支持
扩展性多个扩展点插件的形式接口的形式
基于注解的支持支持支持支持
限流基于QPS.支持基于调用关系的限流有限的支持Rate limiter

规则持久化是什么

一旦我们重启应用,sentinel规则消失,生产环境需要将配置规则进行持久化

怎么玩

将限流规则持久进Nacos保存,只要刷新8401某个rest地址,sentinel控制台的流控规则就能看得到,只要Nacos里面的配置不删除,针对8401上的流控规则持续有效

步骤修改cloudalibaba-sentinel-server8401POM
<!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--><dependency>    <groupID>com.alibaba.csp</groupID>    <artifactID>sentinel-datasource-nacos</artifactID></dependency>
YML
server:  port: 8401spring:  application:    name: cloudalibaba-sentinal-service  cloud:    nacos:      discovery:        #Nacos服务注册中心地址        server-addr: localhost:8848    sentinel:      transport:        #配置Sentin dashboard地址        dashboard: localhost:8080        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口        port: 8719      datasource:        ds1:          nacos:            server-addr: localhost:8848            dataID: cloudalibaba-sentinel-service            groupID: DEFAulT_GROUP            data-type: Json            rule-type: flowmanagement:  endpoints:    web:      exposure:        include: '*'  feign:  sentinel:    enabled: true #激活Sentinel 对Feign的支持
添加Nacos业务规则配置

内容解析
[    {        "resource": "/ratelimit/byUrl","limitApp": "default","grade": 1,"count": 1,"strategy": 0,"controlBehavior": 0,"clusterMode": false    }]

这一步的作用是每次消费者微服务启动时在nacos中定义sentinel的流控规则,从而做到持久化的效果

启动8401刷新sentinel发现业务规则变了

快速访问测试接口

http://localhost:8401/ratelimit/byUrl
默认

停止8401再看sentinel

重新启动8401再看sentinel咋一看还是没有了,稍等一会儿多次调用
http://localhost:8401/ratelimit/byUrl重新配置出现了,持久化验证通过 总结

以上是内存溢出为你收集整理的【SpringCloud】SpringCloud Alibaba Sentinel实现熔断与限流全部内容,希望文章能够帮你解决【SpringCloud】SpringCloud Alibaba Sentinel实现熔断与限流所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1215420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存