当前位置: 首页 > news >正文

不会做网站现代化专业群建设网站

不会做网站,现代化专业群建设网站,seo与sem的区别,网站开发代码无中文本文属于sentinel学习笔记系列。网上看到吴就业老师的专栏#xff0c;写的好值得推荐#xff0c;我整理的有所删减#xff0c;推荐看原文。 https://blog.csdn.net/baidu_28523317/category_10400605.html sentinel 实现限流降级、熔断降级、黑白名单限流降级、系统自适应…本文属于sentinel学习笔记系列。网上看到吴就业老师的专栏写的好值得推荐我整理的有所删减推荐看原文。 https://blog.csdn.net/baidu_28523317/category_10400605.html sentinel 实现限流降级、熔断降级、黑白名单限流降级、系统自适应限流降级以及热点参数限流降级都是由 ProcessorSlot、Checker、Rule、RuleManager 组合完成。ProcessorSlot 作为调用链路的切入点负责调用 Checker 检查当前请求是否可以放行Checker 则根据资源名称从 RuleManager 中拿到为该资源配置的 Rule规则取 ClusterNode 统计的实时指标数据与规则对比如果达到规则的阈值则抛出 Block 异常抛出 Block 异常意味着请求被拒绝也就实现了限流或熔断。 可以总结为以下三个步骤 在 ProcessorSlot#entry 方法中调用 Checker#check 方法并将 DefaultNode 传递给 Checker。Checker 从 DefaultNode 拿到 ClusterNode并根据资源名称从 RuleManager 获取为该资源配置的规则。Checker 从 ClusterNode 中获取当前时间窗口的某项指标数据QPS、avgRt 等与规则的阈值对比如果达到规则的阈值则抛出 Block 异常也有可能将 check 交给 Rule 去实现。 限流规则与规则配置加载器 rule 规则是围绕资源配置的接口Rule 只定义获取资源。 public interface Rule {/*** Get target resource of this rule.** return target resource of this rule*/String getResource();} public abstract class AbstractRule implements Rule {/*** rule id. */private Long id;/*** Resource name. 资源名*/private String resource;/*** 流控对应的调用来源*/private String limitApp; Rule、AbstractRule 与其它实现类的关系如下图所示  FlowRule 是限流规则配置类FlowRule 继承 AbstractRule 并实现 Rule 接口。FlowRule 源码如下  public class FlowRule extends AbstractRule {public FlowRule() {super();setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);}public FlowRule(String resourceName) {super();setResource(resourceName);setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);}/*** The threshold type of flow control (0: thread count, 1: QPS).* 限流阈值类型*/private int grade RuleConstant.FLOW_GRADE_QPS;/*** Flow control threshold count. 限流阈值 配置的是qps类型则代表qps的值配置的是线程数类型则代表线程数*/private double count;/*** Flow control strategy based on invocation chain.* 流控限流策略* {link RuleConstant#STRATEGY_DIRECT} for direct flow control (by origin);* {link RuleConstant#STRATEGY_RELATE} for relevant flow control (with relevant resource);* {link RuleConstant#STRATEGY_CHAIN} for chain flow control (by entrance resource).*/private int strategy RuleConstant.STRATEGY_DIRECT;/*** Reference resource in flow control with relevant resource or context.* 关联流控的资源*/private String refResource;/*** Rate limiter control behavior. 流控效果控制* 0. default(reject directly), 1. warm up, 2. rate limiter, 3. warm up rate limiter*/private int controlBehavior RuleConstant.CONTROL_BEHAVIOR_DEFAULT;//对应流控效果为Warm Up情况下冷启动时长预热时长单位秒private int warmUpPeriodSec 10;/*** Max queueing time in rate limiter behavior.* 对应流控效果为排队等待情况下出现的超时时间*/private int maxQueueingTimeMs 500;// 对应新增流控规则页面的是否集群private boolean clusterMode;/*** Flow rule config for cluster mode.集群流控的相关配置*/private ClusterFlowConfig clusterConfig;/*** The traffic shaping (throttling) controller. 流量整形的实现不同流控效果有不同算法*/private TrafficShapingController controller; 字段属性有些多可以对比sentinel 限流保护-笔记-CSDN博客 跟官网文档来理解 RuleManager Sentinel 中用来管理规则配置的类都以规则类的名称Manger 命名用来加载限流规则配置以及缓存限流规则配置的类为 FlowRuleManager其部分源码如下 public class FlowRuleManager {// 缓存限流规则private static volatile MapString, ListFlowRule flowRules new HashMap();// PropertyListener 监听器private static final FlowPropertyListener LISTENER new FlowPropertyListener();//SentinelProperty ,默认的 DynamicSentinelPropertyprivate static SentinelPropertyListFlowRule currentProperty new DynamicSentinelPropertyListFlowRule();/** the corePool size of SCHEDULER must be set at 1, so the two task ({link #startMetricTimerListener()} can run orderly by the SCHEDULER **/SuppressWarnings(PMD.ThreadPoolCreationRule)private static final ScheduledExecutorService SCHEDULER Executors.newScheduledThreadPool(1,new NamedThreadFactory(sentinel-metrics-record-task, true));static {//给默认的 SentinelProperty 注册监听器FlowPropertyListenercurrentProperty.addListener(LISTENER);startMetricTimerListener();}。。。public static ListFlowRule getRules() {ListFlowRule rules new ArrayListFlowRule();for (Map.EntryString, ListFlowRule entry : flowRules.entrySet()) {rules.addAll(entry.getValue());}return rules;}//更新规则public static void loadRules(ListFlowRule rules) {currentProperty.updateValue(rules);}我们之前demo对用FlowRuleManager.loadRules()来更新规则生效注意1.8.6版本这里面 SentinelProperty SentinelProperty 是 Sentinel 提供的一个接口可注册到 Sentinel 提供的各种规则的 Manager例如 FlowRuleManager并且可以给 SentinelProperty 添加监听器在配置改变时你可以调用 SentinelProperty#updateValue 方法由它负责调用监听器去更新规则而不需要调用 FlowRuleManager#loadRules 方法。这块先先不展开梳理后面结合nacos再看。 限流处理器插槽FlowSlot FlowSlot 是实现限流功能的切入点它作为 ProcessorSlot 插入到 ProcessorSlotChain 链表中在 entry 方法中调用 Checker 去判断是否需要拒绝当前请求如果需要拒绝请求则抛出 Block 异常。FlowSlot 的源码如下 Spi(order Constants.ORDER_FLOW_SLOT) public class FlowSlot extends AbstractLinkedProcessorSlotDefaultNode {private final FlowRuleChecker checker;public FlowSlot() {this(new FlowRuleChecker());}/*** Package-private for test.** param checker flow rule checker* since 1.6.1*/FlowSlot(FlowRuleChecker checker) {AssertUtil.notNull(checker, flow checker should not be null);this.checker checker;}Overridepublic void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,boolean prioritized, Object... args) throws Throwable {checkFlow(resourceWrapper, context, node, count, prioritized);fireEntry(context, resourceWrapper, node, count, prioritized, args);}//校验是否限流void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int count, boolean prioritized)throws BlockException {checker.checkFlow(ruleProvider, resource, context, node, count, prioritized);}Overridepublic void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {fireExit(context, resourceWrapper, count, args);}// 规则生产者private final FunctionString, CollectionFlowRule ruleProvider new FunctionString, CollectionFlowRule() {Override // 参数为资源名称public CollectionFlowRule apply(String resource) {// Flow rule map should not be null.MapString, ListFlowRule flowRules FlowRuleManager.getFlowRuleMap();return flowRules.get(resource);}}; } 限流规则检查器FlowRuleChecker FlowRuleChecker 负责判断是否需要拒绝当前请求方法很多先看看调用checkFlow public class FlowRuleChecker {public void checkFlow(FunctionString, CollectionFlowRule ruleProvider, ResourceWrapper resource,Context context, DefaultNode node, int count, boolean prioritized) throws BlockException {if (ruleProvider null || resource null) {return;}// 获取匹配的规则CollectionFlowRule rules ruleProvider.apply(resource.getName());if (rules ! null) {//遍历规则for (FlowRule rule : rules) { // 检查规则能否通过if (!canPassCheck(rule, context, node, count, prioritized)) {throw new FlowException(rule.getLimitApp(), rule);}}}} 注意遍历限流规则只要有一个限流规则达到限流阈值即可抛出 FlowException使用 FlowException 目的是标志当前请求因为达到限流阈值被拒绝FlowException 是 BlockException 的子类 canPassCheck 方法返回 true 说明允许请求通过反之则不允许通过。canPassCheck 方法源码如下 public boolean canPassCheck(/*NonNull*/ FlowRule rule, Context context, DefaultNode node, int acquireCount,boolean prioritized) {String limitApp rule.getLimitApp();//当前限流规则只对哪个调用来源生效,一般不为null,默认为“default”不限定调用来源if (limitApp null) {return true;}// 集群模式下的规则检测if (rule.isClusterMode()) {return passClusterCheck(rule, context, node, acquireCount, prioritized);}//单机模式下规则检测return passLocalCheck(rule, context, node, acquireCount, prioritized);} 先不讨论集群限流的情况看看单机的passLocalCheck private static boolean passLocalCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount,boolean prioritized) {//根据调用来源和“调用关系限流策略”选择 DefaultNodeNode selectedNode selectNodeByRequesterAndStrategy(rule, context, node);if (selectedNode null) {return true;}return rule.getRater().canPass(selectedNode, acquireCount, prioritized);} selectNodeByRequesterAndStrategy返回ClusterBuilderSlot阶段生成的ClusterNode getRater 返回TrafficShapingController在默认模式下返回流控效果策略DefaultController。DefaultController#canPass 完成canPassCheck。 下面分别看看这两个方法。 流控节点选择 selectNodeByRequesterAndStrategy 方法的实现有多种情况。原码如下 public class FlowRuleChecker {static Node selectNodeByRequesterAndStrategy(/*NonNull*/ FlowRule rule, Context context, DefaultNode node) {// The limit app should not be empty.限流规则针对哪个来源生效String limitApp rule.getLimitApp();// 基于调用关系的限流策略int strategy rule.getStrategy();// 远程来源String origin context.getOrigin();if (limitApp.equals(origin) filterOrigin(origin)) {if (strategy RuleConstant.STRATEGY_DIRECT) {// 1 Matches limit origin, return origin statistic node.return context.getOriginNode();}//2return selectReferenceNode(rule, context, node);} else if (RuleConstant.LIMIT_APP_DEFAULT.equals(limitApp)) {if (strategy RuleConstant.STRATEGY_DIRECT) {//3 Return the cluster node.return node.getClusterNode();}//4return selectReferenceNode(rule, context, node);} else if (RuleConstant.LIMIT_APP_OTHER.equals(limitApp) FlowRuleManager.isOtherOrigin(origin, rule.getResource())) {if (strategy RuleConstant.STRATEGY_DIRECT) {// 5return context.getOriginNode();}//6return selectReferenceNode(rule, context, node);}return null;}static Node selectReferenceNode(FlowRule rule, Context context, DefaultNode node) {String refResource rule.getRefResource();int strategy rule.getStrategy();if (StringUtil.isEmpty(refResource)) {return null;}if (strategy RuleConstant.STRATEGY_RELATE) {return ClusterBuilderSlot.getClusterNode(refResource);}if (strategy RuleConstant.STRATEGY_CHAIN) {if (!refResource.equals(context.getName())) {return null;}return node;}// No node.return null;} 如果当前限流规则的 limitApp 为 default则说明该限流规则对任何调用来源都生效针对所有调用来源限流否则只针对指定调用来源限流。 1 如果调用来源与当前限流规则的 limitApp 相等且 strategy 为 STRATEGY_DIRECT则使用调用来源的 StatisticNode实现针对调用来源限流。 2 前置条件与1相同依然是针对来源限流。selectReferenceNode strategy 为 STRATEGY_RELATE使用引用资源的 ClusterNodestrategy 为 STRATEGY_CHAIN使用当前资源的 DefauleNode。 3当 limitApp 为 default 时针对所有来源限流。如果 strategy 为 STRATEGY_DIRECT则使用当前资源的 ClusterNode。 4 前置条件与3相同依然是针对所有来源限流。selectReferenceNode 5 如果 limitApp 为 other且该资源的所有限流规则都没有针对当前的调用来源限流。如果 strategy 为 STRATEGY_DIRECT则使用 origin 的 StatisticNode。 6  前置条件与5一样。selectReferenceNode 从 selectNodeByRequesterAndStrategy 方法可以看出Sentinel 之所以针对每个资源统计访问来源的指标数据也是为了实现对丰富的限流策略的支持.比如针对调用来源限流可限制并发量较高的来源服务的请求而对并发量低的来源服务的请求可不限流或者是对一些并没有那么重要的来源服务限流。 TrafficShapingController Sentinel 支持对超出限流阈值的流量采取效果控制器控制这些流量流量效果控制支持直接拒绝、Warm Up冷启动、匀速排队。对应 FlowRule 中的 controlBehavior 字段。在调用 FlowRuleManager#loadRules 方法时FlowRuleManager 会将限流规则配置的 controlBehavior 转为对应的 TrafficShapingController。 public interface TrafficShapingController {/*** Check whether given resource entry can pass with provided count.* 判断当前请求是否能通过* param node resource node* param acquireCount count to acquire* param prioritized whether the request is prioritized* return true if the resource entry can pass; false if it should be blocked*/boolean canPass(Node node, int acquireCount, boolean prioritized);/*** Check whether given resource entry can pass with provided count.* 判断当前请求是否能通过* param node resource node* param acquireCount count to acquire* return true if the resource entry can pass; false if it should be blocked*/boolean canPass(Node node, int acquireCount); }DefaultController DefaultController 是默认使用的流量效果控制器直接拒绝超出阈值的请求。当 QPS 超过限流规则配置的阈值新的请求就会被立即拒绝抛出 FlowException。 Overridepublic boolean canPass(Node node, int acquireCount, boolean prioritized) {// 获取当前已使用的tokenqps 算每秒被放行的请求数threads 统计的当前并行占用的线程数int curCount avgUsedTokens(node);//当前已使用token 获取的token 大于token数量的场景if (curCount acquireCount count) {//qps 且prioritized 参数的值为 true(有优先级的请求可以占用未来时间窗口的统计指标)if (prioritized grade RuleConstant.FLOW_GRADE_QPS) {long currentTime;long waitInMs;currentTime TimeUtil.currentTimeMillis();//当前请求需要等待的时间单位毫秒waitInMs node.tryOccupyNext(currentTime, acquireCount, count);if (waitInMs OccupyTimeoutProperty.getOccupyTimeout()) {//将休眠之后对应的时间窗口的 pass(通过)这项指标数据的值加上 acquireCountnode.addWaitingRequest(currentTime waitInMs, acquireCount);// 添加占用未来的 pass 指标的数量node.addOccupiedPass(acquireCount);// 休眠等待当前线程阻塞sleep(waitInMs);// PriorityWaitException indicates that the request will pass after waiting for {link waitInMs}.//休眠结束后抛出 PriorityWait 异常表示当前请求是等待了 waitInMs 之后通过的throw new PriorityWaitException(waitInMs);}}return false;}return true;}private int avgUsedTokens(Node node) {if (node null) {return DEFAULT_AVG_USED_TOKENS;}return grade RuleConstant.FLOW_GRADE_THREAD ? node.curThreadNum() : (int)(node.passQps());} 一般情况下prioritized 参数的值为 false所以这个 canPass 方法实现的流量效果就是直接拒绝。 限于篇幅其他限流RateLimiterController、WarmUpController、WarmUpRateLimiterController 待整理。
http://www.hkea.cn/news/14323115/

相关文章:

  • 陕西省住房与城乡建设部网站网站开发的几种语言
  • 万和城网站3免费网站建站
  • 泰州企业自助建站系统做网站公司能赚钱吗
  • 口碑好的坪山网站建设做网站的的价格
  • 做网站插背景图片如何变大wordpress登入页面
  • 网站注册需要什么网站框架怎么做
  • 邢台网站设计厂家发帖网站百度收率高的
  • 北京公司建网站要多少费用信阳网站推广公司
  • 深圳网站建设推广建设一个网站app需要多少钱
  • 南安市网站建设怎么自己做网站空间
  • 毕业设计做网站怎么样微信小程序商城制作
  • 白酒招商网站大全百度搜索风云排行榜
  • 在模板网站建站好吗电子商务的网站建设过程
  • 网站域名的所有权怎么用域名做邮箱网站
  • 重庆最新网站备案如何给网站配置域名
  • 哈尔滨自助建站软件鲁谷网站建设
  • 湖南长沙网站建地方美食网站开发意义
  • 网站搭建费用明细吃什么补肾最快最好
  • 网站管理员可控的关键节点后台网站建设招聘
  • 网站系统建设费用营销网络电话软件
  • 用二级域名做的网站算新站吗免费动漫软件app下载大全
  • html5登录界面完整代码游戏seo推广
  • asp网站想学做宝宝食谱上什么网站
  • 菏泽市城乡和建设局网站怎么去管理好一个团队
  • 网站跳出的广告是怎么做的凡客诚品购物流程设计
  • 做网站文字要求坪山网站建设方案
  • 湘潭做网站 i磐石网络微信群推广软件
  • 海尔商务网站建设wordpress用户10亿
  • 网站建设的建议例子wordpress 多站点错误
  • 网站排名张家港seo技术培训泰州