What is a sentinel

Sentinel, translated as Sentinel in Chinese, provides flow control and fuse downgrading functions for microservices. Like Hystrix, Sentinel can effectively solve the “avalanche” effect caused by microservice invocation and provide a stable solution for microservices. With Hytrxi entering maintenance and no longer offering new features, Sentinel is a good alternative. Hystrix typically uses thread pools to isolate service calls, while Sentinel uses user threads to isolate interfaces. Hystrxi provides interface level isolation. Sentinel provides a more sophisticated isolation level. In addition, Sentinel directly uses user threads to limit the overhead of thread switching compared to Hystrix’s thread pool isolation. Sentinel’s DashBoard, which allows you to change traffic limiting rules online, is also optimized.

According to the official documentation, Sentinel has the following characteristics:

  • Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba’s double Eleven traffic drive in the past 10 years, such as SEC killing (i.e., burst traffic control within the range of system capacity), message peaking and valley filling, real-time fusing of downstream unavailable applications, etc.
  • Complete real-time monitoring: Sentinel also provides real-time monitoring capabilities. From the console, you can see a summary of the performance of a single machine-by-second data, or even a cluster of less than 500 machines, for accessing the application.
  • Extensive Open source ecosystem: Sentinel provides out-of-the-box integration modules with other open source frameworks/libraries, such as Spring Cloud, Dubbo, and gRPC. You can quickly access Sentinel by introducing the appropriate dependencies and simple configuration.
  • Sophisticated SPI extension points: Sentinel provides easy-to-use, sophisticated SPI extension points. You can quickly customize logic by implementing extension points. For example, customize rule management and adapt data sources.

Download the sentinel dashboard

Sentinel Dashboard provides a lightweight console that provides machine discovery, real-time monitoring of stand-alone resources, aggregation of cluster resources, and rule management. You can use these features with simple application configuration.

Note: The cluster resource summary supports only less than 500 application clusters, with a delay of 1-2 seconds.

Download the latest Sentinel Dashboard at github.com/alibaba/Sen…

After downloading, the startup port is 8748, and the startup command is as follows:

java -Dserver.port=8748 -Dcsp.sentinel.dashboard.server=localhost:8748 -Dproject.name=sentinel-dashboard -jar Sentinel dashboard -, version 1.8.1. JarCopy the code

Renovation consumer

The examples in this tutorial build on the examples in the previous section.

Add the spring Cloud Sentinel dependencies to the Consumer POM file as follows:

      <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

Copy the code

The application. Yml configuration file adds the following configuration:

Server: port: 8763 Spring: Application: name: Consumer Cloud: nacos: Discovery: server-addr: 127.0.0.1:8848 Sentinel: transport: port: 18763 dashboard: localhost:8748 feign: sentinel: enabled: trueCopy the code

Enable Enable the automatic adaptation of Feign and sentinel. Configure the dashboard address for Sentinel.

With this simple configuration, Feign and Sentinel are configured. Start three more projects provider consumer Gateway.

Several times on the browser to http://localhost:5000/consumer/hi-feign

Go to localhost:8748 in your browser and log in to the sentinel console as user sentinel and password sentinel.

Add a flow control rule to the Consumer service/Hi-Feign interface:

QPS to 1, quick access to http://localhost:5000/consumer/hi-feign, there will be a failure.

Use Sentinel on the Spring Cloud Gateway

The Spring Cloud Gateway has been adapted to sentinel, adding related dependencies to the PoM file of the Gatewayg project:

     <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>
Copy the code

Add the sentinel Dashboard configuration to the configuration file application.yml:

spring:
  cloud:
    sentinel:
     transport:
       port: 15000
       dashboard: localhost:8748

Copy the code

Create a gateway group and the gateway traffic limiting rule, the code is as follows, see github.com/alibaba/Sen…

@Configuration public class GatewayConfiguration { private final List<ViewResolver> viewResolvers; private final ServerCodecConfigurer serverCodecConfigurer; public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) { this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); this.serverCodecConfigurer = serverCodecConfigurer; } @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { // Register the block exception handler for Spring Cloud Gateway. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); } @Bean @Order(-1) public GlobalFilter sentinelGatewayFilter() { return new SentinelGatewayFilter(); } @PostConstruct public void doInit() { initCustomizedApis(); initGatewayRules(); } private void initCustomizedApis() { Set<ApiDefinition> definitions = new HashSet<>(); ApiDefinition api1 = new ApiDefinition("consumer") .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/consumer/**") .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); }}); ApiDefinition api2 = new ApiDefinition("provider") .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/provider/**") .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); }}); definitions.add(api1); definitions.add(api2); GatewayApiDefinitionManager.loadApiDefinitions(definitions); } private void initGatewayRules() { Set<GatewayFlowRule> rules = new HashSet<>(); rules.add(new GatewayFlowRule("consumer") .setCount(10) .setIntervalSec(1) ); rules.add(new GatewayFlowRule("consumer") .setCount(2) .setIntervalSec(2) .setBurst(2) .setParamItem(new GatewayParamFlowItem() .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP) ) ); rules.add(new GatewayFlowRule("provider") .setCount(10) .setIntervalSec(1) .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) .setMaxQueueingTimeoutMs(600) .setParamItem(new GatewayParamFlowItem() .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER) .setFieldName("X-Sentinel-Flag") ) ); rules.add(new GatewayFlowRule("provider") .setCount(1) .setIntervalSec(1) .setParamItem(new GatewayParamFlowItem() .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM) .setFieldName("pa") ) ); rules.add(new GatewayFlowRule("provider") .setCount(2) .setIntervalSec(30) .setParamItem(new GatewayParamFlowItem() .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM) .setFieldName("type") .setPattern("warn") .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS) ) ); rules.add(new GatewayFlowRule("provider") .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME) .setCount(5) .setIntervalSec(1) .setParamItem(new GatewayParamFlowItem() .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM) .setFieldName("pn") ) ); GatewayRuleManager.loadRules(rules); }}Copy the code

Create a traffic limiting rule for the Gateway in the Sentinel console as follows:

Flow control rules for QPS = 1, quick access to http://localhost:5000/consumer/hi-feign, the QPS > 1, will be submitted to the following error:

Blocked by Sentinel: FlowException
Copy the code

The sentinel configuration is now in effect. For more sentinel tutorials, please follow fangzhipeng.com.

Reference documentation

www.fangzhipeng.com/springcloud…

Github.com/alibaba/Sen…

Github.com/alibaba/Sen…

Github.com/spring-clou…

Github.com/alibaba/Sen…

Download the source code

Github.com/forezp/Spri…