首页 > 其他分享 >sentinel-ProcessorSlot

sentinel-ProcessorSlot

时间:2024-10-24 11:47:40浏览次数:1  
标签:return chain resourceWrapper context sentinel entry null ProcessorSlot

官方文档

https://sentinelguard.io/zh-cn/docs/basic-implementation.html

初始化时机

   // 1.5.0 版本开始可以直接利用 try-with-resources 特性 自动回收调用  entry.exit() 配合注解使用 @SentinelResource("HelloWorld")
                        try (Entry entry = SphU.entry(
                            "HelloWorld",
                            EntryType.IN)) {
                            // 被保护的逻辑
                            System.out.println(Thread.currentThread().getId() + "hello world" + (++i));

                        } catch (BlockException ex) {
                            i++;
                        }

 

com.alibaba.csp.sentinel.SphU#entry(java.lang.String, com.alibaba.csp.sentinel.EntryType)

    public static Entry entry(String name, EntryType trafficType) throws BlockException {
        return Env.sph.entry(name, trafficType, 1, OBJECTS0);
    }

com.alibaba.csp.sentinel.CtSph#entry(java.lang.String, com.alibaba.csp.sentinel.EntryType, int, java.lang.Object...)

   @Override
    public Entry entry(String name, EntryType type, int count, Object... args) throws BlockException {
        StringResourceWrapper resource = new StringResourceWrapper(name, type);
        return entry(resource, count, args);
    }

com.alibaba.csp.sentinel.CtSph#entryWithPriority(com.alibaba.csp.sentinel.slotchain.ResourceWrapper, int, boolean, java.lang.Object...)

 private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args)
        throws BlockException {
        Context context = ContextUtil.getContext();
        if (context instanceof NullContext) {
            // The {@link NullContext} indicates that the amount of context has exceeded the threshold,
            // so here init the entry only. No rule checking will be done.
            return new CtEntry(resourceWrapper, null, context);
        }

        if (context == null) {
            // Using default context.
            context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME);
        }

        // Global switch is close, no rule checking will do.
        if (!Constants.ON) {
            return new CtEntry(resourceWrapper, null, context);
        }

        ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper);

        /*
         * Means amount of resources (slot chain) exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE},
         * so no rule checking will be done.
         */
        if (chain == null) {
            return new CtEntry(resourceWrapper, null, context);
        }

        Entry e = new CtEntry(resourceWrapper, chain, context);
        try {
            chain.entry(context, resourceWrapper, null, count, prioritized, args);
        } catch (BlockException e1) {
            e.exit(count, args);
            throw e1;
        } catch (Throwable e1) {
            // This should not happen, unless there are errors existing in Sentinel internal.
            RecordLog.info("Sentinel unexpected exception", e1);
        }
        return e;
    }

com.alibaba.csp.sentinel.CtSph#lookProcessChain

 ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) {
        ProcessorSlotChain chain = chainMap.get(resourceWrapper);
        if (chain == null) {
            synchronized (LOCK) {
                chain = chainMap.get(resourceWrapper);
                if (chain == null) {
                    // Entry size limit.
                    if (chainMap.size() >= Constants.MAX_SLOT_CHAIN_SIZE) {
                        return null;
                    }

                    chain = SlotChainProvider.newSlotChain();
                    Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>(
                        chainMap.size() + 1);
                    newMap.putAll(chainMap);
                    newMap.put(resourceWrapper, chain);
                    chainMap = newMap;
                }
            }
        }
        return chain;
    }

 

com.alibaba.csp.sentinel.slotchain.SlotChainProvider#newSlotChain

    public static ProcessorSlotChain newSlotChain() {
        if (slotChainBuilder != null) {
            return slotChainBuilder.build();
        }

        // Resolve the slot chain builder SPI.
        slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class);

        if (slotChainBuilder == null) {
            // Should not go through here.
            RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default");
slotChainBuilder = new DefaultSlotChainBuilder(); } else { RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: " + slotChainBuilder.getClass().getCanonicalName()); } return slotChainBuilder.build(); } private SlotChainProvider() {} }

 

标签:return,chain,resourceWrapper,context,sentinel,entry,null,ProcessorSlot
From: https://www.cnblogs.com/LQBlog/p/18499314

相关文章

  • sentinel-SPI初始化时机
    时机一引入alibaba-starter-sentinel如果使用了alibaba-starter-sentinel则不需要手动调用因为com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration#init在这里面执行了自动调用@PostConstructprivatevoidinit(){if(StringUtils.isEmpty(System.ge......
  • docker-compose安装sentinel
    1.获取sentinel.zip,并上传至/data/soft目录下,目录可任意通过网盘分享的文件:sentinel.zip链接:百度网盘请输入提取码提取码:1234--来自百度网盘超级会员v7的分享2.解压sentinel.zip:unzipsentinel.zip注意:如果unzip命令不可用则需要安装unzip:sudoyuminstallunz......
  • 03 Sentinel限流降级
    Sentinel介绍随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定......
  • Spring Cloud Alibaba 体系-组件-Sentinel
    Sentinel是阿里巴巴开源的一款面向分布式服务架构的流量控制组件,主要用于处理微服务中的限流、熔断和降级,帮助提高系统的稳定性和可靠性。它在微服务架构中,尤其是与SpringCloud、Dubbo等框架结合时,起到了至关重要的保护作用。1.限流、熔断、降级的概念限流(RateLimitin......
  • 微服务03 微服务sentinel, springcloudgateway
    6Sentinel6.1Sentinel介绍和工作机制6.1.1微服务流量治理组件介绍随着微服务的流行,服务和服务之间的调用导致服务的稳定性问题变得越来越重要。雪崩问题:微服务调用链路中的某个服务故障,引起整个链路中的所有微服务都不可用,即雪崩。解决雪崩问题的常见方式有四种:1......
  • sentinel接入记录
       1.引入pom依赖   <!--SpringCloudailibabasentinel-datasource-nacos持久化需要用到--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId>......
  • Redis: Sentinel工作原理和故障迁移流程
    Sentinel哨兵几个核心概念1)定时任务Sentinel它是如何工作的,是如何感知到其他的Sentinel节点以及Master/Slave节点的就是通过它的一系列定时任务来做到的,它内部有三个定时任务第一个就是每一秒每个Sentinel对其他Sentinel和Redis节点执行PING操作(监......
  • GEE教程:利用sentinel-2数据和NDVI指数监测火灾
    目录简介NDVI在火灾监测中的作用利用Sentinel-2数据监测火灾的步骤sentinel-2代码差异结果简介Sentinel-2卫星搭载的多光谱成像仪提供了高分辨率、高频次的地表反射率数据,而归一化植被指数(NDVI)则是一种广泛应用于植被覆盖监测的遥感指数。将两者结合,可以有效地监测......
  • sentinel-transport-SPI-HeartbeatSenderInitFunc
    说明我们引入以下依赖<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.6</version></dependency>配置环境变量-Dcsp.sentinel.dashboard.se......
  • sentinel-tansport-SPI-CommandSPI
    说明我们引入以下<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.6</version></dependency>通过初始化com.alibaba.csp.sentinel.init.Ini......