In this article, we introduce how to use nginx and RateLimiter stream limiting. In this article, we introduce another stream limiting method, Sentinel.

Sentinel is a lightweight and highly available flow control component that is open source by Ali middleware team and oriented to distributed service architecture. It mainly takes traffic as the entry point and helps users protect the stability of services from multiple dimensions such as flow control, fuse downgrade and system load protection. The application of the double Eleven and other core scenes makes it shine. Today we will introduce Alibaba Sentinel.

Vsentinel main features

Pictures from the network, deleted.

VLinux Sentinel installation and deployment

1.1 Download the sentinel_dashboardJAR package

Can the github.com/alibaba/Sen… Find the appropriate version and download the current latest version, Sentinel-Dashboard-1.8.0.jar

1.1 the deployment

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar Sentinel dashboard - 1.8.0 comes with jar

– Dserver. Port = 8080 represents the kanban port number of the project, – Dcsp. Sentinel. Dashboard. The server = localhost: 8858 on behalf of the dashboard service will be registered, – dproject. name= Sentinel-Dashboard represents the project name of its own service. Access localhost: 8858; Enter username, password (username/password default value sentinel).

Can also custom user name password, add – Dsentinel. At startup command dashboard. The auth. Username = toutou – Dsentinel. Dashboard. The auth. Password = 123456.

Vdocker Sentinel installation and deployment

I installed Docker on my virtual machine, so the following examples are based on Docker sentinel. If you don’t have Docker installed, you can check out this article. Docker is deployed from installation to Hello World.

2.1 Pull sentinel image

Docker pull bladex/sentinel - dashboard: 1.8.0 comes with

2.2 Creating a Container

Docker run --name sentinel-d -p 8858:8858 Bladex/Sentinel-Dashboard :1.8.0

2.3 Starting containers

docker start sentinel

V Verify the installation effect

Go to http://ip:8858/

As described above, log in with the username/password default value sentinel.

There is a Sentinel-Dashboard by default.

Sentinel vspringboot integration

3.1 Adding dependency POM.xml

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> . < version > 0.9.0 RELEASE < / version > < / dependency >Copy the code

3.2 Adding and configuring application.properties

spring.cloud.sentinel.transport.port=9719
spring.cloud.sentinel.transport.dashboard=toutou.com:8858
Copy the code

3.3 Add pocket bottom method

/** * @author toutou * @date by 2020/12 * @des https://www.cnblogs.com/toutou */ @Slf4j public class BlockHandlerUtil { public Result message(BlockException blockExe){ log.info(blockExe.toString()); Return Result.setErrorResult(555, "Network congestion, please try again later." ); }}Copy the code

3.4 add the controller

/** * @author toutou * @date by 2020/12 * @des https://www.cnblogs.com/toutou */ @Slf4j @RestController public class IndexController { @GetMapping("/hello") @SentinelResource(value="hello",blockHandlerClass = BlockHandlerUtil.class, blockHandler = "message") public Result hello(@RequestParam(value = "name",defaultValue = "toutou",required = false)String name){ return Result.setSuccessResult("hello " + name); }}Copy the code

3.5 @ SentinelResource annotation

@SentinelResource can cover most of the applications of Sentinel, so understanding @SentinelResource is a good way to use Sentinel.

The property name If required instructions
value is Resource name. (This parameter is mandatory and must be passedvalueValue find the corresponding rule to configure)
entryType no The value is IN/OUT. The default value is OUT
blockHandler no The name of the function that handles BlockException.. Function requirements: 1. Must be public modifier 2. Return type same as original method 3. The parameter type must match the original method and add a BlockException parameter at the end. 4. The default method must be in the same class as the original method. If you want to use functions of other classes, you can configure blockHandlerClass and specify the methods in the blockHandlerClass.
blockHandlerClass no The class that holds blockHandler. The corresponding handler must be public static, otherwise it will not parse. Other requirements: same as blockHandler.
fallback no Provides fallback handling logic when an exception is thrown.. The fallback function handles all types of exceptions except those excluded from exceptionsToIgnore. Function requirements: 1. Return type consistent with the original method 2. Parameter type must match with the original method. 3. By default, the method must be in the same class as the original method. If you want to use functions of other classes, you can configure fallbackClass and specify methods in fallbackClass.
fallbackClass no Classes to store fallback. The corresponding handler must be static, otherwise it cannot be parsed.
defaultFallback no Fallback logic for general purpose. The default fallback function handles all types of exceptions except those excluded from exceptionsToIgnore. If both Fallback and defaultFallback are configured, fallback prevails. The return type of the method must be the same as that of the original method. 2. The parameter list of the method must be empty or have a Throwable parameter. 3. The default method must be in the same class as the original method. If you want to use functions of other classes, you can configure fallbackClass and specify methods in fallbackClass.
exceptionsToIgnore no Specifies which exceptions to exclude.Exceptions that are excluded will not be counted in the exception statistics and will not enter fallback logic, but will be thrown as is.
exceptionsToTrace no Exceptions that need to be traced

VSentinel console

In the browser open http://localhost:8301/hello when Sentinel console related to configuration menu and visual interface parameters will be corresponding display.

4.1 Adding flow Control Rules

Various rules can be added in “cluster link”, such as “flow control”, which can be viewed in “Flow Control Rules” after being added.

4.2 AB test Test

Use ab test tool testing mean control rules, ab – n – 30 c 5 http://localhost:8301/hello. If you can’t use the AB Test, check out this article ab Test stress test.

Above is real-time monitoring from the Sentinel console, showing that the flow control rules are in effect. We use a browser to access http://localhost:8301/hello at the same time, the effect is the diagram below, you can see the return value consistent with our set of code.

4.3 Degradation Rules

Parameter Description:

 

  • High availability flow control and protection components for cloud native microservices
  • Docker deployment sentinel
  • Sentinel realizes downgrading current limiting fuse
  • Let’s talk about the use of @sentinelResource

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \