Skip to content

Sentinel

主要特性

Sentinel 分为两个部分

  • 核心库(Java 客户端):不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  • 控制台(Dashboard):基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

官方文档:https://github.com/alibaba/Sentinel/wiki

学习文档:https://www.iocoder.cn/Spring-Cloud-Alibaba/Sentinel/?self

使用注意事项和总结

整合Spring Cloud Alibaba Sentinel

xml
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
yaml
spring:
  application:
	name: demo
  cloud:
	# Sentinel 配置项,对应 SentinelProperties 配置属性类
	sentinel:
	  enabled: true # 是否开启。默认为 true 开启
	  eager: true # 是否饥饿加载。默认为 false 关闭 [默认情况下,Sentinel 是延迟初始化]
	  transport:
		dashboard: 81.68.218.181:8858 # Sentinel 控制台地址
	  filter:
		url-patterns: /** # 拦截请求的地址。默认为 /*

feign:
  sentinel:
	enabled: true # 开启 Sentinel 对 Feign 的支持,默认为 false 关闭。
resttemplate:
  sentinel:
	enabled: true # 开启 Sentinel 对 resttemplate 的支持,默认为 true 开启。
spring:
  application:
	name: demo
  cloud:
	# Sentinel 配置项,对应 SentinelProperties 配置属性类
	sentinel:
	  enabled: true # 是否开启。默认为 true 开启
	  eager: true # 是否饥饿加载。默认为 false 关闭 [默认情况下,Sentinel 是延迟初始化]
	  transport:
		dashboard: 81.68.218.181:8858 # Sentinel 控制台地址
	  filter:
		url-patterns: /** # 拦截请求的地址。默认为 /*

feign:
  sentinel:
	enabled: true # 开启 Sentinel 对 Feign 的支持,默认为 false 关闭。
resttemplate:
  sentinel:
	enabled: true # 开启 Sentinel 对 resttemplate 的支持,默认为 true 开启。
java
@Configuration
public class RestTemplateConfiguration {

    @Bean
    @SentinelRestTemplate
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
@Configuration
public class RestTemplateConfiguration {

    @Bean
    @SentinelRestTemplate
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

通过 @SentinelRestTemplate 注解,声明 Sentinel 对 RestTemplate 的支持。

另外,@SentinelRestTemplate 注解提供了 blockHandlerblockHandlerClassfallback

fallbackClass 属性,作用和 @SentinelResource 注解是一致的。

使用 Nacos 作为数据源

xml
<!-- Sentinel 对 Nacos 作为数据源的支持 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- Sentinel 对 Nacos 作为数据源的支持 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
yaml
spring:
  application:
    name: demo
  cloud:
    # Sentinel 配置项,对应 SentinelProperties 配置属性类
    sentinel:
      enabled: true # 是否开启。默认为 true 开启
      eager: true # 是否懒加载。默认为 false 关闭
      transport:
        dashboard: 81.68.218.181:8858 # Sentinel 控制台地址
      filter:
        url-patterns: /** # 拦截请求的地址。默认为 /*
      # Sentinel 规则的数据源,是一个 Map 类型。key 为数据源名,可自定义;value 为数据源的具体配置
      datasource:
        ds1:
          # 对应 DataSourcePropertiesConfiguration 类
          nacos:
            server-addr: loalhost:8848 # Nacos 服务器地址
            namespace: dev
            group-id: DEFAULT_GROUP # Nacos 分组
            data-id: ${spring.application.name}-flow-rule # Nacos 配置集编号
            data-type: json # 数据格式
            rule-type: FLOW # 规则类型
spring:
  application:
    name: demo
  cloud:
    # Sentinel 配置项,对应 SentinelProperties 配置属性类
    sentinel:
      enabled: true # 是否开启。默认为 true 开启
      eager: true # 是否懒加载。默认为 false 关闭
      transport:
        dashboard: 81.68.218.181:8858 # Sentinel 控制台地址
      filter:
        url-patterns: /** # 拦截请求的地址。默认为 /*
      # Sentinel 规则的数据源,是一个 Map 类型。key 为数据源名,可自定义;value 为数据源的具体配置
      datasource:
        ds1:
          # 对应 DataSourcePropertiesConfiguration 类
          nacos:
            server-addr: loalhost:8848 # Nacos 服务器地址
            namespace: dev
            group-id: DEFAULT_GROUP # Nacos 分组
            data-id: ${spring.application.name}-flow-rule # Nacos 配置集编号
            data-type: json # 数据格式
            rule-type: FLOW # 规则类型

key:为数据源名,可自定义,无特殊含义。

value:为数据源的具体配置,对应 DataSourcePropertiesConfiguration 类,可以选择 filenacoszkapolloredis 任一作为数据的数据源。这里我们选择 nacos 来接入 Nacos 作为数据源。

  • rule-type:数据源对应的 Sentinel 规则类型,在 RuleType 类枚举。这里我们设置了 FLOW 对应流量控制的规则。
  • data-type:数据源的数据格式,默认为 json。这里我们设置了 json,所以稍后创建的 Nacos 配置集的数据格式要为 JSON
  • server-addr:Nacos 服务器地址。
  • namespace:Nacos 分组。
  • data-id:Nacos 配置集编号。推荐配置集编号的命名规则为 ${applicationName}-${ruleType},因此这里我们设置为 demo-provider-flow-rule,即 demo-provider 应用的流控规则。

创建 Nacos 配置集

json
[
    {
        "resource": "/demo/echo",
        "limitApp": "default",
        "grade": 1,
        "count": 5,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]
[
    {
        "resource": "/demo/echo",
        "limitApp": "default",
        "grade": 1,
        "count": 5,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

参考 《Sentinel 控制规则 —— 流量控制》

  • resource:资源名,即限流规则的作用对象
  • count: 限流阈值
  • grade: 限流阈值类型(QPS 或并发线程数)
  • limitApp: 流控针对的调用来源,若为 default 则不区分调用来源
  • strategy: 调用关系限流策略
  • controlBehavior: 流量控制效果(直接拒绝、Warm Up、匀速排队)

Sentinel生产方案

https://gitee.com/k8cloud/sentinel-dashboard