Learn the usage of sentinel from ruoyi-gateway

The meaning of sentinel in Chinese is sentinel, which is an open source framework developed by Ali for current limiting, downgrading and fusing. The function is equivalent to hystrix, but sentinel is more powerful and has more functions. Its features include:

  1. Limiting
  2. downgrade
  3. Hotspot parameter current limit
  4. System load protection
  5. authorized
  6. fuse
  7. Service circuit breaker
  8. System load protection
    Specific functions and tutorials can be found on the official website: Sentinel official website documentation , we mainly briefly look at how to use sentinel in spring cloud gateway.

use of sentinel

1. Introduce dependencies

First we need to introduce sentinel dependencies, we can add the following dependencies in pom.xml:

<!-- SpringCloud Alibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!-- SpringCloud Alibaba Sentinel Gateway -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

        <!-- Sentinel Datasource Nacos -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

2. Configuration file

Add the following configuration to the configuration file:

spring:
  cloud:
    sentinel:
      # 取消控制台懒加载
      eager: true
      transport:
        # 控制台地址
        dashboard: 127.0.0.1:8718
      # nacos配置持久化
      datasource:
        ds1:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: sentinel-gateway
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

We can see that in the configuration file, the console address of sentinel and the configuration persistence of nacos are configured.
What we need to pay attention to here is that the sentinel console address must be configured, otherwise the sentinel console will not be accessible.
The configuration persistence of nacos is optional. If it is not configured, the sentinel rules will not be persisted and will become invalid after restarting.

3. Configure sentinel rules

In Zoe, we mainly use nacos as the rule storage center of sentinel, so we need to add sentinel rules to nacos.

The way to add rules in nacos is relatively simple. We only need to add a configuration with dataId in nacos sentinel-gateway, and then add the following content to the configuration:

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

The specific meaning of the rules is as follows:

  • resource: Resource name, which can be the route name in the gateway or a user-defined API group name.
  • limitApp: The call source targeted by flow control, if it is default, the call source will not be distinguished. Otherwise, if the call source is not default, only the traffic whose call source is limitApp will perform flow control.
  • grade: current limit threshold type, QPS or number of threads. The specific value is 0 or 1, corresponding to the QPS and the number of threads respectively.
  • count: current limit threshold.
  • strategy: Invoke relationship current limiting strategy, currently only supports three modes: direct, association and link. The specific values ​​are 0, 1, and 2, corresponding to direct, associated, and link respectively.
  • controlBehavior: flow control effect (direct rejection, warm up, uniform queue). The specific values ​​are 0, 1, and 2, which correspond to direct rejection, warm up, and uniform queuing respectively.
  • clusterMode: Whether it is cluster mode.

4. Start Dashboard

  1. Download the latest dashboard, go to github to download the appropriate dashboard version https://github.com/alibaba/Sentinel/releases
  2. Execute the following command to start the dashboard
java -Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
  1. Visit dashboard http://localhost:8718, the default account and password are sentinel

Guess you like

Origin blog.csdn.net/aofengdaxia/article/details/129294025