Categories
Uncategorized

SpringBoot 2.0 + Alibaba Sentinel actual dynamic flow restrictor

Foreword

Spike in building distributed systems and to build the system from one hundred thousand Bowen 0-1, the flow restrictor is an indispensable link, but will reduce the resource overhead in the system against malicious attacks can afford.

In the previous article, we use the open-source toolkit limiting tools RateLimiter and OpenResty Guava provided Lua scripts were limiting API and application level. Today, we talk about traffic Sentinel Ali soldiers defend open-source distributed systems.

What Sentinel that?

With the stability between the popular micro-services, services, and services is becoming increasingly important. Sentinel flow rate as a starting point, the flow rate control, fuse downgrade, a plurality of load protection systems dimension stability protection services.

Sentinel has the following characteristics:

  • Rich scenarios: Sentinel undertake Ali Baba core scene nearly 10 years double XI promote flow, e.g. spike (i.e., bursty traffic control system can withstand capacity range), load shifting message, the cluster flow control , real-time application is unavailable downstream fuse.

  • Complete real-time monitoring: Sentinel while providing real-time monitoring. You can see a single machine second-level data access applications in the console, or even a summary of the operation of the cluster size of 500 or less.

  • Widely used open source ecology: Sentinel offers and other open-source framework / library integration module out of the box, such as integration with Spring Cloud, Dubbo, gRPC of. You only need to introduce appropriate dependent and simple configuration can quickly access Sentinel.

  • SPI perfect extension point: Sentinel provides easy to use, well-SPI expansion interface. You can quickly implement custom logic through expansion interface. Such as custom rules management, adaptation of dynamic data sources.

Sentinel’s main features:

Sentinel Open Source Ecology:

Sentinel divided into two parts:

  • Core libraries (Java client) does not depend on any framework / library, it can run on any Java runtime environment, while Dubbo / Spring Cloud and other frameworks and has a good support.

  • Console (Dashboard) Spring Boot developers, after pack can be run directly, no additional applications such as container based on Tomcat.

Console Configuration

Sentinel console should at least include the following functions:

  • View a list of machines and health: heartbeat packet sent by the client to collect Sentinel, used to determine the machine is online.

  • Monitoring (standalone and cluster aggregation): Sentinel client exposed by the API monitor, and periodically pull polymerizable application monitoring information, real-time monitoring ultimately order of seconds.

  • Management rules and push: push unified management rules.

  • Authentication: Authentication production environment is very important. Where each developer needs to be customized according to their own situation.

Directly from the [release page] (https://github.com/alibaba/Sentinel/releases “release page”) to download the latest version of the console jar package, launch Sentinel console requires JDK 1.8 and above. .

Startup script sentinel.sh:

#!/bin/bash
java -Dsentinel.dashboard.auth.username=admin \
-Dsentinel.dashboard.auth.password=admin \
-Dserver.port=8084 -Dcsp.sentinel.dashboard.server=localhost:8084 \
-Dproject.name=sentinel-dashboard \
-jar sentinel-dashboard-1.6.3.jar &

The user can configure the following parameters:

  • -Dsentinel.dashboard.auth.username = admin console used to log user-specified name admin;

  • -Dsentinel.dashboard.auth.password = admin console is used to specify the login password is admin; if you omit these parameters, the default user and password are sentinel;

  • -Dserver.servlet.session.timeout = 7200 Spring Boot specified expiration time for the session server, such as 7200 indicates 7200 seconds; 60m represents 60 minutes, 30 minutes by default;

Client Configuration

pom.xml introduce the following dependency:

 

     org.springframework.boot
     spring-boot-starter-parent
     2.1.5.RELEASE
     


     
         org.springframework.boot
         spring-boot-starter-web
     
     
         com.alibaba.cloud
         spring-cloud-starter-alibaba-sentinel
     


     
     
         
             com.alibaba.cloud
             spring-cloud-alibaba-dependencies
             2.1.0.RELEASE
             pom
             import
         
     

Profiles:

# 应用名称 https://blog.52itstyle.vip
spring.application.name=blog
spring.cloud.sentinel.transport.port=8720
# 测试请替换为自己的地址
spring.cloud.sentinel.transport.dashboard=116.190.247.112:8084

Here spring.cloud.sentinel.transport.port port configuration starts a Http Server application on the corresponding machine, the Server Sentinel will do interact with the console. For example, Sentinel console adds a limiting rule, the rule will push data to the receiving Http Server, Http Server rules and then registered to the Sentinel.

Code configuration:

/**
  * 博文 https://blog.52itstyle.vip
  */
@RequestMapping("{id}.shtml")
@SentinelResource("blogView")
public String page(@PathVariable("id") Long id, ModelMap model) {
     try{
         Blog blog = blogService.getById(id);
         String key = "blog_"+id;
         Long views = redisUtil.size(key);
         blog.setViews(views+blog.getViews());
         model.addAttribute("blog",blog);
     } catch (Throwable e) {
         return  "error/404";
     }
     return  "article";
}

@SentinelResource annotation is used to identify whether resources are limiting downgrade. The properties of the above-described example annotation ‘blogView’ represents a resource name.

By default, Sentinel Controller will intercept all requests, identifies the resource name here, because all of the articles will go to this request, in order to facilitate statistics and flow control, where custom resource identifier.

More annotation support, please refer to: Sentinel / wiki / annotation support.

Access client project, just click a few pages, and then log Sentinel console, if you see the following screen, the configuration was successful.

Limiting configuration, the search resource names we have just to configure, select the flow control function.

Input threshold parameters, in order to facilitate testing, 2 direct input here, the continuous refresh your browser, the following error if the background, along with the front page does not display properly explain the configuration take effect.

Caused by: com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

Of course, Sentinel workflow features not only simple, but also support the cluster model, the ultimate version of one hundred thousand in the post, we can cluster node, set the single sharing, you can also set an overall threshold.

Production environments

Sentinel core library is now available for production, in addition to the current Alibaba, there are a number of enterprises to use them in a production environment.

Rules and push management

Native version of the rules will be managed through the API rules to the clients and updates directly into memory, and can not be directly used in the production environment.

However Sentinel offers extended reading data sources ReadableDataSource, push unified rules center, client monitor changes in the way listeners by registering a time, such as the use Nacos, Zookeeper and other distribution center. This approach has better real-time and ensure consistency.

monitor

Sentinel will record second-level data access to resources (if there is no access will not be recorded) and saved in a local log. Sentinel Sentinel console may be reserved by a client HTTP API pulling data from the second monitoring level monitoring logs, and polymerization.

Sentinel console after the current monitoring data directly polymerized in the presence of memory, persistence is not performed, and retain only the last 5 minutes monitoring data. If you need to monitor data persistence feature, you can achieve on their own extension.

Precautions

Since you did not carefully read the documents, the deployment console to the external network, including network and the client starts, the client can not be accessed, real-time data links and cluster-point link does not display properly.

Note that a small partner testing, raw mode, the console client, and must be accessible to each other, the client periodically send a heartbeat request to the console, the console will push rule to the client, data flow control and pulling the polymerization.

Source

https://gitee.com/52itstyle/spring-boot-blog

reference

https://github.com/alibaba/Sentinel

Leave a Reply