This article will explain the use of Sentinel flow limiting from the most common Http flow limiting, respectively from two parts of stand-alone flow control and cluster flow control. For source analysis, please refer to the subsequent articles of Sentinel series.


1. Single-node current limiting

1.1 Starting a SpringBoot project

1.1.1 Introducing dependencies

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>  <version>0.22..RELEASE</version>
</dependency>
<dependency>Zk is used here to persist traffic limiting rules. For configuration rules, refer to 2.1 Configuring Dynamic Rule Sources
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-zookeeper</artifactId>
    <version>1.52.</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code

1.1.2 to write configuration

server: port: 8081 spring: application: name: hello cloud: sentinel: transport: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Sentinel: zooKeeper: address: 127.0.0.1:2181 Path: /sentinel_rule_configCopy the code

1.1.3 Creating a Controller

@Controller
@ResponseBody
public class HelloWorld {

    @GetMapping("hello")
    public String hello(a){
        return "Hello World!"; }}Copy the code

1.2 Starting the Dashboard Console

1.2.1 Pulling dashboard from Github

Address: github.com/alibaba/Sen…

1.2.2 Starting the console

Sentinel is a Springboot project, the default port is 8080, we can start directly :\

Account: sentinel Password: sentinel

Console page:

At this point, our Springboot will send a heartbeat request to Dashboard and receive dashboard’s request to obtain information and send the machine information to Dashboard display, because we can see the relevant information about the flow limiting of each client here. The monitoring picture here is the real-time monitoring picture generated by adding the traffic limiting policy:

1.2.3 Adding a Traffic Limiting Policy

When adding a rule, you need to fill in information such as target resource, source, and traffic limiting threshold, and then click the Add button. By default, the flow limiting rules are saved in local memory, which will cause the rules to disappear after the project restarts. We can use ZK or NACOS to implement persistent storage of rules, which will be configured later. At this point we can pressure the request, we can see the details of limiting on the real-time monitor:

At this point, our single-machine traffic limiting rules have been configured. The following describes the configuration of cluster traffic control.


2 Traffic limiting for the cluster

2.1 Configuring a Dynamic rule Source

In cluster flow control scenarios, we recommend using dynamic rule sources to manage rules dynamically. Here I use ZK as the middleware to store the rules. As shown below:

2.1.1 the deployment of zk

The process is not described; everyone deploys it.

2.1.2 configuration dashboard

Sentinel officials have completed the ZK integration for us, we just need to change the configuration and so on:

  1. Pull the zK integration code into the Rule directory of the Dashboard module. FlowRuleApiProvider and FlowRuleApiPublisher are commented out.

  1. Open FlowControllerV2. Java, if you set FlowRuleZookeeperProvider and publisher of the two beans has a name, you can specify the name of the set for you when autowired, Or at sign Resource.

  1. Modify sidebar. HTML, change the original flowV1 to the figure shown.

  1. Modify dashboard poM file to comment out scope

  1. Modify the POM file of the client and add zK dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>  <version>0.22..RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-zookeeper</artifactId>
    <version>1.52.</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code
  1. Obtain the rule from ZooKeeper where the client modifies the obtain rule

@Component
public class SentinelConfigZookeeper {

    @Value("${spring.application.name}")
    private String appName;

    /** * After the Bean is constructed, read the configuration rule from ZooKeeper */
    @PostConstruct
    public void loadRules(a) {
        final String remoteAddress = "127.0.0.1:2181";
        final String path = "/sentinel_rule_config/" + appName;

        ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path,
                source -> JSON.parseObject(source, newTypeReference<List<FlowRule>>() { })); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); }}Copy the code

The dynamic rule source is now configured ok.

Restart the Dashboard project and restart the client. In this way, Dashboard has been associated with ZooKeeper, and dashboard operations have changed from operating client APIS to operating ZooKeeper. All dashboard configurations are stored in ZooKeeper and pushed to the client in real time. After the client restarts, the Dashboard is not affected. This completes the multi-instance shared flow control rule.

2.2 Configuring the Token Client and Token Server

2.2.1 What are Token Client and Token Server

  • Token Client: a cluster flow control Client that communicates with the owning Token Server to request a Token. The cluster traffic limiting server returns the result to the client to determine whether to limit traffic.
  • Token Server: the flow control Server of the cluster processes the requests from the Token Client and determines whether to issue the Token based on the configured cluster rules.

2.2.2 Configuring Traffic Limiting to Import Dependencies

Here we combine server and client to implement an embedded pattern. Introduce both of the above dependencies in the POM.

Add client and server dependencies to the POM file of the Springboot project for single-node streaming limiting:

<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-cluster-server-default</artifactId> < version > 1.8.0 comes with < / version > < / dependency > < the dependency > < groupId > com. Alibaba. CSP < / groupId > < artifactId > sentinel - cluster - the client - the default < / artifactId > < version > 1.8.0 comes with < / version > < / dependency >Copy the code

2.2.3 Starting the Test Demo Cluster

Modify the VM options configuration to start three test demo instances with different ports.

2.2.4 Assigning a Token Server to the console

When all the above steps are complete, we can manage the allocation of Token Servers on the cluster Flow Control Token Server list page in the Sentinel Console. Suppose we start three application instances and select one instance as token Server and the other two as Token clients:

2.2.5 Configure rules on the console and observe the effect

Configuration rules

Real-time monitoring

Note: The use of token-server and token-client needs to be improved according to the actual use of the source code. In addition, token-server currently has a single point of issue, requiring individuals to implement master election.