Spring Cloud Alibaba | Sentinel: Traffic defense soldiers distributed systems dynamic current limit rule

Spring Cloud Alibaba | Sentinel: Traffic defense soldiers distributed systems dynamic current limit rule

Previous articles a more detailed account of the use of Sentinel posture, have not seen the small partners can access the following link to view:

But still can not meet our daily production needs, which is very important point is to configure rules limiting the presence of memory currently in use, we restart after each application of the rules we configured in the Sentinel console is lost, Here, we introduce the Sentinel rule persistent manner.

Sentinel provides two ways for us to modify the rules:

  • Direct modification (loadRules) by API
  • Modified by different data sources adapted DataSource

loadRules () method takes rule object memory state, but more often the rule stored in a file, database or the configuration of which center. DataSource interface provides us with the ability to dock any configuration source. Compared directly through the API to modify the rules, implements the DataSource interface is a more reliable approach.

DataSource expand common implementations are:

  • Pull mode: The client periodically polls the initiative to a rule management center pulling rule, this rule may be the center of RDBMS, files, and even VCS and so on. The way to do this is simple, the disadvantage is unable to get change in a timely manner;
  • Push mode: Center for unified rules push the client by way of registered listeners listening for changes, such as the use Nacos, Zookeeper and other distribution center. This approach has better real-time and ensure consistency.

Sentinel currently supports the following data source extensions:

  • Pull-based: file, Consul (since 1.7.0)
  • Push-based: ZooKeeper, Redis, Nacos, Apollo

Here, we focus on what Sentinel dynamic rules based on Nacos.

1. Sentinel dynamic rule-based combat Nacos

1.1 create a sub-project sentinel_nacos

Project relies pom.xml as follows:

Listing: Alibaba / Sentinel-springcloud-High / sentinel_nacos / pom.xml
***

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

1.2 application.yml profile as follows:

Listing: Alibaba / Sentinel-springcloud-High / sentinel_nacos / src / main / Resources / application.yml
***

server:
  port: 10000
spring:
  application:
    name: spring-cloud-sentinel-nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.44.129:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8720
      datasource:
        ds:
          nacos:
            server-addr: 192.168.44.129:8848
            dataId: spring-cloud-sentinel-nacos
            groupId: DEFAULT_GROUP
            rule-type: flow
            namespace: 8282c713-da90-486a-8438-2a5a212ef44f
  • spring.cloud.sentinel.transport.dashboard: Access address Sentinel console.
  • spring.cloud.sentinel.datasource.ds.nacos.server-addr: Nacos access address.
  • spring.cloud.sentinel.datasource.ds.nacos.dataId: GroupId nacos stored rules.
  • spring.cloud.sentinel.datasource.ds.nacos.groupId: DataId nacos stored rules.
  • spring.cloud.sentinel.datasource.ds.nacos.rule-type: Rule used to define the type of storage, not empty.
  • spring.cloud.sentinel.datasource.ds.nacos.namespace: Namespace nacos stored rules.

Since version iterative relationship configuration information in this example does not necessarily apply to all versions, you can analyze DataSourcePropertiesConfiguration, NacosDataSourcePropertiesand AbstractDataSourcePropertiesthree configuration to obtain detailed configuration would be more accurate.

For example, this configuration example is derived from NacosDataSourcePropertiesand AbstractDataSourceProperties.

NacosDataSourcePropertiesSource as follows:

public class NacosDataSourceProperties extends AbstractDataSourceProperties {

    private String serverAddr;

    @NotEmpty
    private String groupId = "DEFAULT_GROUP";

    @NotEmpty
    private String dataId;

    private String endpoint;
    private String namespace;
    private String accessKey;
    private String secretKey;

    // 代码省略...
}

AbstractDataSourcePropertiesSource as follows:

public class AbstractDataSourceProperties {

    @NotEmpty
    private String dataType = "json";
    @NotNull
    private RuleType ruleType;
    private String converterClass;
    @JsonIgnore
    private final String factoryBeanName;
    @JsonIgnore
    private Environment env;
}

Here I only configure a non-empty and no default value ruleType, the relevant ruleTypevalues can be viewed com.alibaba.cloud.sentinel.datasource.RuleType, this is an enumerated type.

1.3 interface to create a test class HelloController.java as follows:

代码清单:Alibaba/sentinel-springcloud-high/sentinel_nacos/src/main/java/com/springcloud/sentinel_nacos/controller/HelloController.java
***

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(HttpServletRequest request) {
        return "Hello, port is: " + request.getLocalPort();
    }
}

1.4 Configuration Nacos Configuration Center

Configuration content as shown:

Note that the configuration wherein the consistency and Group Data ID and the program to be configured. Format selection JSON, filled in as follows:

[
    {
        "resource": "/hello",
        "limitApp": "default",
        "grade": 1,
        "count": 1,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

The above configuration can be seen that the rule is an array type, each object in the array is arranged for each of the protected resource objects, each object properties explained as follows:

  • resource: resource name, that is, the role of objects limiting rules.
  • limitApp: call the source for flow control, if the default is not case call source.
  • grade: limit threshold type, or the number of QPS thread mode, 0 indicates the number of concurrent current limiting, representative of a flow rate control is performed in accordance with QPS.
  • count: limit threshold
  • strategy: is determined according to the resource itself, or according to other related resources (refResource), or according to the link inlet
  • controlBehavior: flow control effect (directly rejected / queued / slow start mode)
  • clusterMode: whether the cluster model

1.5 Test

Normal project promoters, open the browser to access several http: // localhost: 10000 / hello, speed limiting some of which may have been found, and after limiting the page shown below:

Blocked by Sentinel (flow limiting)

Front limiting configuration is successful, then we open the Sentinel console, look at the traffic restriction rules, have a piece of data is the data we have configured in Nacos, as shown:

note:

In the Sentinel integrates dynamic rule after Nacos, to modify the interface flow control there are two places, and one is Sentinel console, there is a Nacos console.

But to remember that in the current version, modified in the Sentinel Console rules, will not be synchronized to the distribution center Nacos, and modify the configuration rules in Nacos, the client will be by the Listener to be synchronized Sentinel console . So, after the integration of the dynamic rules are stored Nacos do Two things to note:

  • Sentinel console to modify the rules: the service exists only in memory, does not modify the configuration values ​​Nacos, restore the original values ​​after the restart.
  • Nacos console to modify the rules: the rules of service in memory is updated, Nacos persistent rules will be updated after the restart remains.

I suggest that blocking rules do best in Nacos console to modify the operation, try to avoid direct rule changes made directly in the Sentinel console.

2. Sample Code

Github- Sample Code

Gitee- Sample Code

Guess you like

Origin www.cnblogs.com/babycomeon/p/11525661.html