Category : sentinel

Order This article mainly studies sentinel’s SentinelGatewayFilter SentinelGatewayFilter Sentinel-1.6.2/sentinel-adapter/sentinel-spring-cloud-gateway-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/SentinelGatewayFilter.java public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter { private final GatewayParamParser<ServerWebExchange> paramParser = new GatewayParamParser<>( new ServerWebExchangeItemParser()); @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); Mono<Void> asyncResult = chain.filter(exchange); if (route != null) { String routeId = route.getId(); Object[] params = paramParser.parseParameterFor(routeId, exchange, ..

Read more

Order This article mainly studies sentinel’s SentinelGatewayFilter SentinelGatewayFilter Sentinel-1.6.2/sentinel-adapter/sentinel-spring-cloud-gateway-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/sc/SentinelGatewayFilter.java public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter { private final GatewayParamParser<ServerWebExchange> paramParser = new GatewayParamParser<>( new ServerWebExchangeItemParser()); @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); Mono<Void> asyncResult = chain.filter(exchange); if (route != null) { String routeId = route.getId(); Object[] params = paramParser.parseParameterFor(routeId, exchange, ..

Read more

Order This article mainly studies sentinel’s SimpleHttpCommandCenter SimpleHttpCommandCenter sentinel-transport-simple-http-0.1.1-sources.jar! /com/alibaba/csp/sentinel/transport/command/SimpleHttpCommandCenter.java /*** * The simple command center provides service to exchange information. * * @author youji.zj */ public class SimpleHttpCommandCenter implements CommandCenter { private static final int PORT_UNINITIALIZED = -1; private static final int DEFAULT_SERVER_SO_TIMEOUT = 3000; private static final int DEFAULT_PORT = 8719; private static ..

Read more

Order This article mainly studies sentinel’s SentinelWebAutoConfiguration SentinelWebAutoConfiguration spring-cloud-alibaba-sentinel-autoconfigure-0.2.0.BUILD-SNAPSHOT-sources.jar! /org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java @Configuration @ConditionalOnProperty(name = “spring.cloud.sentinel.enabled”, matchIfMissing = true) @EnableConfigurationProperties(SentinelProperties.class) public class SentinelWebAutoConfiguration { private static final Logger logger = LoggerFactory .getLogger(SentinelWebAutoConfiguration.class); @Value(“${project.name:${spring.application.name:}}”) private String projectName; @Autowired private SentinelProperties properties; public static final String APP_NAME = “project.name”; @PostConstruct private void init() { if (StringUtils.isEmpty(System.getProperty(APP_NAME))) { System.setProperty(APP_NAME, projectName); ..

Read more

Order This article mainly studies sentinel’s ModifyRulesCommandHandler ModifyRulesCommandHandler sentinel-transport-common-0.1.1-sources.jar! /com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java @CommandMapping(name = “setRules”) public class ModifyRulesCommandHandler implements CommandHandler<String> { static DataSource<?, List<FlowRule>> flowDataSource = null; static DataSource<?, List<AuthorityRule>> authorityDataSource = null; static DataSource<?, List<DegradeRule>> degradeDataSource = null; static DataSource<?, List<SystemRule>> systemSource = null; public static synchronized void registerFlowDataSource(DataSource<?, List<FlowRule>> datasource) { flowDataSource = datasource; } ..

Read more

Order This article mainly studies sentinel’s DataSource DataSource sentinel-datasource-extension-0.1.1-sources.jar! /com/alibaba/csp/sentinel/datasource/DataSource.java public interface DataSource<S, T> { /** * Load data data source as the target type. * * @return the target data. * @throws Exception */ T loadConfig() throws Exception; /** * Read original data from the data source. * * @return the original data. * ..

Read more

Order This article mainly studies sentinel’s SentinelResourceAspect SentinelResourceAspect com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java @Aspect public class SentinelResourceAspect { private final Logger logger = LoggerFactory.getLogger(SentinelResourceAspect.class); @Pointcut(“@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)”) public void sentinelResourceAnnotationPointcut() { } @Around(“sentinelResourceAnnotationPointcut()”) public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable { Method originMethod = getMethod(pjp); SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class); if (annotation == null) { // Should not go through here. throw new ..

Read more

Order This article mainly studies how to customize sentinel’s DataSource, taking jdbc as an example. maven <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sentinel</artifactId> <version>0.2.0.BUILD-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Extending AutoRefreshDataSource public class JdbcDataSource<T> extends AutoRefreshDataSource<String, T> { ..

Read more

Order This article mainly studies sentinel’s NettyHttpCommandCenter. NettyHttpCommandCenter com/alibaba/csp/sentinel/transport/command/NettyHttpCommandCenter.java public class NettyHttpCommandCenter implements CommandCenter { private final HttpServer server = new HttpServer(); private final ExecutorService pool = Executors.newSingleThreadExecutor( new NamedThreadFactory(“sentinel-netty-command-center-executor”)); @Override public void start() throws Exception { pool.submit(new Runnable() { @Override public void run() { try { server.start(); } catch (Exception ex) { RecordLog.info(“Start netty ..

Read more

Order This article mainly studies sentinel’s ArrayMetric ArrayMetric com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java public class ArrayMetric implements Metric { private final WindowLeapArray data; public ArrayMetric(int windowLength, int interval) { this.data = new WindowLeapArray(windowLength, interval); } /** * For unit test. */ public ArrayMetric(WindowLeapArray array) { this.data = array; } @Override public long success() { data.currentWindow(); long success = 0; ..

Read more

Order This article mainly studies sentinel’s DefaultSlotsChainBuilder DefaultSlotsChainBuilder com/alibaba/csp/sentinel/slots/DefaultSlotsChainBuilder.java public class DefaultSlotsChainBuilder implements SlotsChainBuilder { @Override public ProcessorSlotChain build() { ProcessorSlotChain chain = new DefaultProcessorSlotChain(); chain.addLast(new NodeSelectorSlot()); chain.addLast(new ClusterBuilderSlot()); chain.addLast(new LogSlot()); chain.addLast(new StatisticSlot()); chain.addLast(new SystemSlot()); chain.addLast(new AuthoritySlot()); chain.addLast(new FlowSlot()); chain.addLast(new DegradeSlot()); return chain; } } Implement the build method, return DefaultProcessorSlotChain, and use it to ..

Read more

Order This article mainly studies sentinel’s StatisticSlot. StatisticSlot com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> { @Override public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object… args) throws Throwable { try { fireEntry(context, resourceWrapper, node, count, args); node.increaseThreadNum(); node.addPassRequest(); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseThreadNum(); context.getCurEntry().getOriginNode().addPassRequest(); } if (resourceWrapper.getType() == EntryType.IN) { Constants.ENTRY_NODE.increaseThreadNum(); Constants.ENTRY_NODE.addPassRequest(); ..

Read more