网站设计师培训,微商城网站建设市场,昌吉市住房和城乡建设局网站,设计logo网站免费南蒲四特Sentinel译为“哨兵”#xff0c;顾名思义#xff0c;面对您后台的大量服务/微服务#xff0c;前置一个哨兵#xff0c;但面对大量请求时#xff0c;让后台服务有序被调用#xff0c;但某些服务的不可用时#xff0c;采用服务熔断降级等措施#xff0c;让系统仍能平稳运…
Sentinel译为“哨兵”顾名思义面对您后台的大量服务/微服务前置一个哨兵但面对大量请求时让后台服务有序被调用但某些服务的不可用时采用服务熔断降级等措施让系统仍能平稳运行不至于造成系统雪崩典型应用场景
MQ中消息在某些时间段比如订单交易的高峰期秒杀期等消息并发量非常大时通过Sentinel起到“削峰填谷”的作用某个业务服务非常复杂需要调用大量微服务其中某服务不可用时不影响整体业务运行如提交某个订单需要调用诸如验证库存验证优惠金额预算配额工作流ERP支付验证手机号等其中验证手机号服务不可用时采用降级的方式让其通过不影响整个提交订单的业务上述订单业务提交时依赖的下游应用控制线程数请求上下文超过阈值时新的请求立即拒绝即针对流控可基于QPS或线程数在某些业务场景下都会有用如下就是一个qps瞬时拉大时通过流量缓慢增加避免系统被压垮的情况image
随着微服务的流行服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征: 丰富的应用场景Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景例如秒杀即突发流量控制在系统容量可以承受的范围、消息削峰填谷、实时熔断下游不可用应用等。 完备的实时监控Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据甚至 500 台以下规模的集群的汇总运行情况。 广泛的开源生态Sentinel 提供开箱即用的与其它开源框架/库的整合模块例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。 完善的 SPI 扩展点Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点快速的定制逻辑。例如定制规则管理、适配数据源等。
Sentinel分为两部分 核心库Java 客户端不依赖任何框架/库能够运行于所有 Java 运行时环境同时对 Dubbo / Spring Cloud 等框架也有较好的支持。 控制台Dashboard基于 Spring Boot 开发打包后可以直接运行不需要额外的 Tomcat 等应用容器该模块目前基于SpringBoot运行
部署
在使用Sentinel之前我们首先需部署Sentinel Dashborad下载最新版的Sentinel Dashborad通过以下命令运行 java -Dserver.port8088 -jar sentinel-dashboard-1.3.0.jar通过server.port执行程序运行端口号通过http://localhost:8088打开控制台如下 image
至此部署完成非常简单
项目中使用Sentinel
Sentinel针对各个主流框架都提供了适配包括ServletDubboSpringBoot/SpringCloudgRPCRocketMQ等本文以SpringBoot2举例通过笔者测试发现SpringBoot 1.x支持不好自定义流控规则不可用首先我们需要在SpringBoot2的配置文件中指定Sentinel连接的控制台地址和项目名即application.yml文件如下 project:name: 在控制台显示的项目名
spring:cloud:sentinel:transport:dashboard: 192.168.1.154:8088在项目中加入依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactIdversion0.2.0.RELEASE/version
/dependency启动SpringBoot2后可以在簇点链路页面看到项目的各个服务首次访问服务时会被出现在列表中这些服务在Sentinel中被称为资源接着你就可以针对这些资源进行流控降级热点授权等操作。
流量控制与规则扩展
在控制台可通过流控规则菜单定义某资源的流控规则不过这个定义只在内存中但控制台重启后随之消失所以我们一般在项目中通过配置文件来定义流控规则编写一个流控数据源如下 package com.sumscope.study.springboot2.service;import java.net.URLDecoder;
import java.util.List;import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.property.PropertyListener;
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class FileDataSourceInit implements InitFunc{private ConverterString, ListFlowRule flowRuleListParser source - JSON.parseObject(source,new TypeReferenceListFlowRule() {});private ConverterString, ListDegradeRule degradeRuleListParser source - JSON.parseObject(source,new TypeReferenceListDegradeRule() {});private ConverterString, ListSystemRule systemRuleListParser source - JSON.parseObject(source,new TypeReferenceListSystemRule() {});Overridepublic void init() throws Exception {ClassLoader classLoader getClass().getClassLoader();String flowRulePath URLDecoder.decode(classLoader.getResource(FlowRule.json).getFile(), UTF-8);String degradeRulePath URLDecoder.decode(classLoader.getResource(DegradeRule.json).getFile(), UTF-8);String systemRulePath URLDecoder.decode(classLoader.getResource(SystemRule.json).getFile(), UTF-8);// Data source for FlowRuleFileRefreshableDataSourceListFlowRule flowRuleDataSource new FileRefreshableDataSource(flowRulePath, flowRuleListParser);FlowRuleManager.register2Property(flowRuleDataSource.getProperty());// Data source for DegradeRuleFileRefreshableDataSourceListDegradeRule degradeRuleDataSource new FileRefreshableDataSource(degradeRulePath, degradeRuleListParser);DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());// Data source for SystemRuleFileRefreshableDataSourceListSystemRule systemRuleDataSource new FileRefreshableDataSource(systemRulePath, systemRuleListParser);SystemRuleManager.register2Property(systemRuleDataSource.getProperty());}
}然后在项目的resources下新增目录META-INF\services\com.alibaba.csp.sentinel.init.InitFunc并填写以上类的完成类名如com.sumscope.study.springboot2.service.FileDataSourceInit即可这样在您的classpath目录下通过DegradeRule.json,FlowRule.json,SystemRule.json分别来定义降级规则流控规则和系统规则比如我们定义一个流控规则让test资源QPS为1即1秒钟最多调用1次如下 [{resource: test,controlBehavior: 0,count: 1,grade: 1,limitApp: default,strategy: 0}
]在Java代码中通过SentinelResource(test)来定义服务对应的资源名如果不指数URI即为资源名。 熔断降级
降级规则定义如下 image
可基于RT平均响应时间或异常比例两种方式来定义其中RT单位为毫秒。
指定RT时当资源的平均响应时间超过阈值DegradeRule 中的 count以 ms 为单位之后资源进入准降级状态。接下来如果持续进入 5 个请求它们的 RT 都持续超过这个阈值那么在接下的时间窗口DegradeRule 中的 timeWindow以 s 为单位之内对这个方法的调用都会自动地返回。
指定异常时当资源的每秒异常总数占通过总数的比值超过阈值DegradeRule 中的 count之后资源进入降级状态即在接下的时间窗口DegradeRule 中的 timeWindow以 s 为单位之内对这个方法的调用都会自动地返回。
热点参数限流
也可对经常访问的数据进行限流如某个商品或某个用户等如下图 image
Sentinel 利用 LRU 策略结合底层的滑动窗口机制来实现热点参数统计。LRU 策略可以统计单位时间内最近最常访问的热点参数而滑动窗口机制可以帮助统计每个参数的 QPS热点参数限流目前只支持QPS模式。
黑白名单
可通过定义策略黑名单或白名单限定资源的调用方是否让其通过以下是代码定义白名单规则 AuthorityRule rule new AuthorityRule();
rule.setResource(test);
rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
rule.setLimitApp(appA,appB);
AuthorityRuleManager.loadRules(Collections.singletonList(rule));实时监控
在控制台我们可以实时看到每个资源的qps情况如下图 image 注其中p_qps是每秒通过的请求数b_qps是每秒拒绝的请求数