SpringCloudAlibaba Microservice Practical Series (5) Sentinel1.8.5+Nacos Persistence

Sentinel data persistence

We introduced Sentinel's flow control, circuit breaker and downgrade functions earlier. At the same time, Sentinel applications are also facing a problem: we configured a bunch of flow control and downgrade rules in the Sentinel background management interface, but as soon as Sentinel is restarted, these rules all disappear. Then we have to consider the persistence issue of Sentinel.

Sentinel provides us with several persistence solutions:

  • save to file
  • Using Redis storage
  • Using Nacos storage
  • Use Zookeeper storage
  • Use Apollo Storage

We use Nacos to store this data.

Tip: Since you use Nacos to persist these Sentinel rule data, your Nacos must first have a persistence environment~~

achieve persistence

pom.xml文件加入Nacos数据源的依赖

<!-- sentinel持久化访问Nacos的数据源依赖 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

修改bootstrap.yml文件,增加datasource的配置

server:
  port: 9001
spring:
  application:
    name: consumer # 应用名

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos服务地址
    sentinel:
      transport:
        port: 8719 # 启动http server,并且该服务与Sentinel仪表板进行交互,使sentinel可以控制应用,若端口占用则8719+1依次扫描
        dashboard: 127.0.0.1:8080 # 仪表版访问地址
      datasource: # sentinel数据源
        ds1:	# 自定义连接名
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds1 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: flow # flow表示流控规则

rule-typeFor values ​​in , you can refer to RuleTypethe enumeration class

Five rules persisted into Nacos

  • authority(authorization rules)
  • degrade(demotion rules)
  • flow(flow control rules)
  • param(hotspot rules)
  • system(system rules)

When your rule-type is not set, a null pointer exception will be reported when the program starts.

The specific configuration content of the newly created DataId in the Nacos control panel sentinel-consumer-ds1is as follows:

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

First of all, configuring DataId and GroupId on the Nacos background management interface must correspond to the configuration file one by one, and json []indicates that multiple configurations can be configured.

  • resource: Indicates the resource name
  • limitApp:Indicates which sources of calls are to be restricted
  • grade:Threshold type, value reference RuleConstantclass (0-thread number current limit 1-QPS current limit)
  • count: Indicates the current limit threshold
  • strategy: indicates flow control mode, 0 indicates direct, 1 indicates association, 2 indicates link
  • controlBehavior: Flow control effect (0 means fast failure, 1 means Warm Up, 2 means waiting in line)

So these parameters, which parameters should be set for different rules, you can refer to some rule classes to view

  • Flow control rules:FlowRule

  • Fuse downgrade:DegradeRule

    Field illustrate Defaults
    resource Resource name, that is, the object of the rule
    grade Fuse strategy, support slow call ratio/abnormal ratio/abnormal number strategy slow call ratio
    count In the slow call ratio mode, it is the slow call critical RT (beyond this value is counted as slow call); in the abnormal ratio/abnormal number mode, it is the corresponding threshold
    timeWindow Fuse duration, unit is s
    minRequestAmount The minimum number of requests triggered by the circuit breaker. When the number of requests is less than this value, the circuit breaker will not be interrupted even if the exception ratio exceeds the threshold (introduced in 1.7.0) 5
    statIntervalMs Statistical duration (unit is ms), such as 60*1000 represents minute level (introduced in 1.8.0) 1000 ms
    slowRatioThreshold Slow call ratio threshold, only slow call ratio mode is valid (introduced in 1.8.0)
  • Hotspot rules:ParamFlowRule

    Field illustrate Defaults
    resource Resource name, that is, the object of the rule
    grade current limiting mode QPS mode
    count Current limiting threshold, required
    durationInSec Statistical window length (in seconds), supported from version 1.6.0 1s
    controlBehavior Flow control effect (supports fast failure and uniform queuing modes), supported since version 1.6.0 fail fast
    maxQueueingTimeMs Maximum queuing waiting time (only valid in the uniform queuing mode), supported from version 1.6.0 0
    paramIdx The index of the hot parameter, required, corresponding to the parameter index position in SphU.entry(xxx, args)
    paramFlowItemList For parameter exceptions, the current limiting threshold can be set independently for the specified parameter value, which is not restricted by the previous count threshold. Only basic types and string types are supported

In addition to controlling these json rules in the Sentinel control panel, you can also use Java code, but it is not flexible enough.

After configuring the above properties, use the Jmeter request /sentinelTestBinterface to test the flow control effect.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-QU9fy6um-1690078521125)(../imgs4/1.png)]

As you can see, flow control has taken effect. Let’s take a look at the sentinel dashboard:

Insert image description here

A flow control rule has been generated.

Let’s try the downgrade rules again

bootstrap.ymlAdd ds2 data source in

server:
  port: 9001
spring:
  application:
    name: consumer # 应用名

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos服务地址
    sentinel:
      transport:
        port: 8719 # 启动http server,并且该服务与Sentinel仪表板进行交互,使sentinel可以控制应用,若端口占用则8719+1依次扫描
        dashboard: 127.0.0.1:8080 # 仪表版访问地址
      datasource: # sentinel数据源
        ds1:
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds1 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: flow # flow表示流控规则
        ds2:
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds2 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: degrade # degrade表示降级规则

Add sentinel-consumer-ds2files in nacos panel

[
    {
    
    
        "resource":"/sentinelTest",
        "count":1,
        "grade":2,
        "timeWindow":5,
        "minRequestAmount":1
    }
]
  • timeWindow: represents the time window, the number of times for circuit breaker.
  • count:threshold
  • grade: downgrade strategy, refer to RuleConstant class (0-RT 1-abnormal ratio 2-number of exceptions)

In the above configuration, we /sentinelTestsimulate an exception and then use Jmeter to make multiple requests

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-lwozYNFW-1690078521127)(../imgs4/3.png)]

Enter circuit breaker downgrade.

Finally, let’s look at an example of system rules

In bootstrap.yml

server:
  port: 9001
spring:
  application:
    name: consumer # 应用名

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos服务地址
    sentinel:
      transport:
        port: 8719 # 启动http server,并且该服务与Sentinel仪表板进行交互,使sentinel可以控制应用,若端口占用则8719+1依次扫描
        dashboard: 127.0.0.1:8080 # 仪表版访问地址
      datasource: # sentinel数据源
        ds1:
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds1 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: flow # flow表示流控规则
        ds2:
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds2 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: degrade # degrade表示降级规则

        ds3:
          nacos:
            server-addr: localhost:8848 # nacos服务地址
            dataId: sentinel-consumer-ds3 # nacos的dataId
            groupId: DEFAULT_GROUP # 默认分组
            data-type: json # 数据类型 json类型
            rule-type: system # system表示系统规则

Add ds3 sentinel-consumer-ds3files in nacos

[
    {
    
    
        "qps":1
    }
]

System rules correspond to SystemRuleclasses, which have the following attributes:

  • avgRt: average system response time
  • highestCpuUsage:CPU usage
  • highestSystemLoad:load
  • maxThread: maximum number of threads
  • qps: Number of requests processed per second

Add a /sentinelTestCnew resource, and then use Jmeter to perform a stress test. 系统规则It is at the system level, that is, the flow control relative to the resource is relatively coarse-grained.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-SagtgDhy-1690078521128)(../imgs4/4.png)]

Similarly, for other rules, you can find its corresponding Java configuration method, find that class, draw inferences from one example, and modify the corresponding configuration.

There are two points to note:

  • Modify the rules in the Nacos console. The rules on Sentinel will take effect immediately. They will still be effective after restarting the service. After all, they have been persisted.
  • Rules modified in the Sentinel console will not be modified in nacos, and will change to the values ​​set by nacos after restarting.

Guess you like

Origin blog.csdn.net/weixin_45248492/article/details/131876750
Recommended