1. Sentinel 介绍
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展点。可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。
2. 如何使用 Sentinel
如果要在项目中引入 Sentinel,使用 group ID 为
com.alibaba.cloud和 artifact ID 为spring-cloud-starter-alibaba-sentinel的 starter。[source,yaml]<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
下面这个例子就是一个最简单的使用 Sentinel 的例子:
@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}}@RestControllerpublic class TestController {@GetMapping(value = "/hello")@SentinelResource("hello")public String hello() {return "Hello Sentinel";}}
@SentinelResource注解用来标识资源是否被限流、降级。上述例子上该注解的属性 ‘hello’ 表示资源名。@SentinelResource还提供了其它额外的属性如blockHandler,blockHandlerClass,fallback用于表示限流或降级的操作,更多内容可以参考 Sentinel注解支持。
以上例子都是在 WebServlet 环境下使用的,Sentinel 目前已经支持 WebFlux,需要配合spring-boot-starter-webflux依赖触发 sentinel-starter 中 WebFlux 相关的自动化配置。@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}}@RestControllerpublic class TestController {@GetMapping("/mono")@SentinelResource("hello")public Mono<String> mono() {return Mono.just("simple string").transform(new SentinelReactorTransformer<>("otherResourceName"));}}
2.1 Sentinel 控制台
Sentinel 控制台提供一个轻量级的控制台,它提供机器发现、单机资源实时监控、集群资源汇总,以及规则管理的功能。只需要对应用进行简单的配置,就可以使用这些功能。 :::danger 注意: 集群资源汇总仅支持 500 台以下的应用集群,有大概 1 - 2 秒的延时。 ::: 开启该功能需要3个步骤:
获取控制台
可以从 release 页面 下载最新版本的控制台 jar 包。
也可以从最新版本的源码自行构建 Sentinel 控制台:下载 控制台 工程
- 使用以下命令将代码打包成一个 fat jar:
mvn clean package
启动控制台
Sentinel 控制台是一个标准的 SpringBoot 应用,以 SpringBoot 的方式运行 jar 包即可。
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
如若8080端口冲突,可使用 -Dserver.port=新端口 进行设置。
2.2 配置控制台信息
application.yml
spring:cloud:sentinel:transport:port: 8719dashboard: localhost:8080
这里的 spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。
更多 Sentinel 控制台的使用及问题参考: Sentinel控制台
3. OpenFeign 支持
Sentinel 适配了 OpenFeign 组件。如果想使用,除了引入 sentinel-starter 的依赖外还需要 2 个步骤:
- 配置文件打开 sentinel 对 feign 的支持:
feign.sentinel.enabled=true 加入
openfeign starter依赖使sentinel starter中的自动化配置类生效:<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
这是一个
FeignClient的简单使用示例:@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)public interface EchoService {@GetMapping(value = "/echo/{str}")String echo(@PathVariable("str") String str);}class FeignConfiguration {@Beanpublic EchoServiceFallback echoServiceFallback() {return new EchoServiceFallback();}}class EchoServiceFallback implements EchoService {@Overridepublic String echo(@PathVariable("str") String str) {return "echo fallback";}}
:::info Feign 对应的接口中的资源名策略定义:httpmethod:protocol://requesturl。
@FeignClient注解中的所有属性,Sentinel 都做了兼容。 :::EchoService接口中方法echo对应的资源名为GET:http://service-provider/echo/{str}。4. RestTemplate 支持
Spring Cloud Alibaba Sentinel 支持对
RestTemplate的服务调用使用 Sentinel 进行保护,在构造RestTemplatebean的时候需要加上@SentinelRestTemplate注解。@Bean@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)public RestTemplate restTemplate() {return new RestTemplate();}
@SentinelRestTemplate注解的属性支持限流(blockHandler,blockHandlerClass)和降级(fallback,fallbackClass)的处理。
其中blockHandler或fallback属性对应的方法必须是对应blockHandlerClass或fallbackClass属性中的静态方法。
该方法的参数跟返回值跟org.springframework.http.client.ClientHttpRequestInterceptor#interceptor方法一致,其中参数多出了一个BlockException参数用于获取 Sentinel 捕获的异常。
比如上述@SentinelRestTemplate注解中ExceptionUtil的handleException属性对应的方法声明如下:public class ExceptionUtil {public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {...}}
:::info 应用启动的时候会检查
@SentinelRestTemplate注解对应的限流或降级方法是否存在,如不存在会抛出异常 :::@SentinelRestTemplate注解的限流(blockHandler,blockHandlerClass)和降级(fallback,fallbackClass)属性不强制填写。
当使用RestTemplate调用被 Sentinel 熔断后,会返回RestTemplate request block by sentinel信息,或者也可以编写对应的方法自行处理返回信息。这里提供了SentinelClientHttpResponse用于构造返回信息。
Sentinel RestTemplate 限流的资源规则提供两种粒度:httpmethod:schema://host:port/path:协议、主机、端口和路径httpmethod:schema://host:port:协议、主机和端口 :::info 以[https://www.taobao.com/test](https://www.taobao.com/test)这个 url 并使用 GET 方法为例。对应的资源名有两种粒度,分别是GET:https://www.taobao.com以及GET:https://www.taobao.com/test:::5. 动态数据源支持
SentinelProperties内部提供了TreeMap类型的datasource属性用于配置数据源信息。
比如配置 4 个数据源:
这种配置方式参考了 Spring Cloud Stream Binder 的配置,内部使用了spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.jsonspring.cloud.sentinel.datasource.ds1.file.rule-type=flow#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json#spring.cloud.sentinel.datasource.ds1.file.data-type=custom#spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter#spring.cloud.sentinel.datasource.ds1.file.rule-type=flowspring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinelspring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUPspring.cloud.sentinel.datasource.ds2.nacos.data-type=jsonspring.cloud.sentinel.datasource.ds2.nacos.rule-type=degradespring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOWspring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181spring.cloud.sentinel.datasource.ds3.zk.rule-type=authorityspring.cloud.sentinel.datasource.ds4.apollo.namespace-name = applicationspring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinelspring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = testspring.cloud.sentinel.datasource.ds4.apollo.rule-type=param-flow
TreeMap进行存储,comparator 为String.CASE_INSENSITIVE_ORDER。 :::info d1, ds2, ds3, ds4 是ReadableDataSource的名字,可随意编写。后面的file,zk,nacos,apollo就是对应具体的数据源。它们后面的配置就是这些具体数据源各自的配置。 ::: 每种数据源都有两个共同的配置项:data-type、converter-class以及rule-type。data-type配置项表示Converter类型,Spring Cloud Alibaba Sentinel 默认提供两种内置的值,分别是json和xml(不填默认是json)。如果不想使用内置的json或xml这两种Converter,可以填写custom表示自定义Converter,然后再配置converter-class配置项,该配置项需要写类的全路径名(比如spring.cloud.sentinel.datasource.ds1.file.converter-class=org.springframework.cloud.alibaba.cloud.examples.JsonFlowRuleListConverter)。rule-type配置表示该数据源中的规则属于哪种类型的规则(flow,degrade,authority,system,param-flow,gw-flow,gw-api-group)。
当某个数据源规则信息加载失败的情况下,不会影响应用的启动,会在日志中打印出错误信息。
默认情况下,xml 格式是不支持的。需要添加jackson-dataformat-xml依赖后才会自动生效。
关于 Sentinel 动态数据源的实现原理,参考: 动态规则扩展6. Zuul 支持
参考 Sentinel 网关限流
若想跟 Sentinel Starter 配合使用,需要加上spring-cloud-alibaba-sentinel-gateway依赖,同时需要添加spring-cloud-starter-netflix-zuul依赖来让spring-cloud-alibaba-sentinel-gateway模块里的 Zuul 自动化配置类生效:<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency>
7. Spring Cloud Gateway 支持
参考 Sentinel 网关限流
若想跟 Sentinel Starter 配合使用,需要加上spring-cloud-alibaba-sentinel-gateway依赖,同时需要添加spring-cloud-starter-gateway依赖来让spring-cloud-alibaba-sentinel-gateway模块里的 Spring Cloud Gateway 自动化配置类生效:<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
8. Sentinel 对外暴露的 Endpoint
Sentinel 内部提供了一个 Endpoint, 对应的 endpoint id 为sentinel。
Endpoint 暴露的 json 中包含了多种属性:
- appName: 应用名
- logDir: 日志所在目录
- logUsePid: 日志文件名是否带上进程id
- blockPage: 限流 block 之后跳转的页面
- metricsFileSize: metrics 文件的大小
- metricsFileCharset: metrics 文件对应的字符集
- totalMetricsFileCount: metrics 最多保留的文件数
- consoleServer: sentinel dashboard 地址
- clientIp: 客户端 ip
- heartbeatIntervalMs: 客户端跟 dashboard 的心跳间隔时间
- clientPort: 客户端需要暴露的端口跟 dashboard 进行交互
- coldFactor: 冷启动因子
- filter: CommonFilter 相关的属性, 比如 order, urlPatterns 以及 enable
- datasource: 客户端配置的数据源信息
- rules: 客户端生效的规则,内部含有 flowRules, degradeRules, systemRules, authorityRule, paramFlowRule
这是 Endpoint 暴露的 json 示例:
{"blockPage": null,"appName": "sentinel-example","consoleServer": "localhost:8080","coldFactor": "3","rules": {"flowRules": [{"resource": "GET:http://www.taobao.com","limitApp": "default","grade": 1,"count": 0.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}, {"resource": "/test","limitApp": "default","grade": 1,"count": 0.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}, {"resource": "/hello","limitApp": "default","grade": 1,"count": 1.0,"strategy": 0,"refResource": null,"controlBehavior": 0,"warmUpPeriodSec": 10,"maxQueueingTimeMs": 500,"clusterMode": false,"clusterConfig": null}]},"metricsFileCharset": "UTF-8","filter": {"order": -2147483648,"urlPatterns": ["/*"],"enabled": true},"totalMetricsFileCount": 6,"datasource": {"ds1": {"file": {"dataType": "json","ruleType": "FLOW","converterClass": null,"file": "...","charset": "utf-8","recommendRefreshMs": 3000,"bufSize": 1048576},"nacos": null,"zk": null,"apollo": null,"redis": null}},"clientIp": "30.5.121.91","clientPort": "8719","logUsePid": false,"metricsFileSize": 52428800,"logDir": "...","heartbeatIntervalMs": 10000}
9. 关于 Sentinel Starter 更多的配置项信息
下表显示当应用的 ApplicationContext 中存在对应的Bean的类型时,会进行自动化设置:
| 存在Bean的类型 | 操作 | 作用 |
|---|---|---|
UrlCleaner |
WebCallbackManager.setUrlCleaner(urlCleaner) |
资源清理(资源(比如将满足 /foo/:id 的 URL 都归到 /foo/* 资源下)) |
UrlBlockHandler |
WebCallbackManager.setUrlBlockHandler(urlBlockHandler) |
自定义限流处理逻辑 |
RequestOriginParser |
WebCallbackManager.setRequestOriginParser(requestOriginParser) |
设置来源信息 |
Spring Cloud Alibaba Sentinel 提供了这些配置选项
| 配置项 | 含义 | 默认值 |
|---|---|---|
spring.application.name or project.name |
Sentinel项目名 | |
spring.cloud.sentinel.enabled |
Sentinel自动化配置是否生效 | true |
spring.cloud.sentinel.eager |
是否提前触发 Sentinel 初始化 | false |
spring.cloud.sentinel.transport.port |
应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer | 8719 |
spring.cloud.sentinel.transport.dashboard |
Sentinel 控制台地址 | |
spring.cloud.sentinel.transport.heartbeat-interval-ms |
应用与Sentinel控制台的心跳间隔时间 | |
spring.cloud.sentinel.transport.client-ip |
此配置的客户端IP将被注册到 Sentinel Server 端 | |
spring.cloud.sentinel.filter.order |
Servlet Filter的加载顺序。Starter内部会构造这个filter | Integer.MIN_VALUE |
spring.cloud.sentinel.filter.url-patterns |
数据类型是数组。表示Servlet Filter的url pattern集合 | /* |
spring.cloud.sentinel.filter.enabled |
Enable to instance CommonFilter | true |
spring.cloud.sentinel.metric.charset |
metric文件字符集 | UTF-8 |
spring.cloud.sentinel.metric.file-single-size |
Sentinel metric 单个文件的大小 | |
spring.cloud.sentinel.metric.file-total-count |
Sentinel metric 总文件数量 | |
spring.cloud.sentinel.log.dir |
Sentinel 日志文件所在的目录 | |
spring.cloud.sentinel.log.switch-pid |
Sentinel 日志文件名是否需要带上pid | false |
spring.cloud.sentinel.servlet.block-page |
自定义的跳转 URL,当请求被限流时会自动跳转至设定好的 URL | |
spring.cloud.sentinel.flow.cold-factor |
https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81—- %E5%86%B7%E5%90%AF%E5%8A%A8[冷启动因子] | 3 |
spring.cloud.sentinel.zuul.order.pre |
SentinelZuulPreFilter 的 order | 10000 |
spring.cloud.sentinel.zuul.order.post |
SentinelZuulPostFilter 的 order | 1000 |
spring.cloud.sentinel.zuul.order.error |
SentinelZuulErrorFilter 的 order | -1 |
spring.cloud.sentinel.scg.fallback.mode |
Spring Cloud Gateway 熔断后的响应模式(选择 redirect or response) |
|
spring.cloud.sentinel.scg.fallback.redirect |
Spring Cloud Gateway 响应模式为 ‘redirect’ 模式对应的重定向 URL | |
spring.cloud.sentinel.scg.fallback.response-body |
Spring Cloud Gateway 响应模式为 ‘response’ 模式对应的响应内容 | |
spring.cloud.sentinel.scg.fallback.response-status |
Spring Cloud Gateway 响应模式为 ‘response’ 模式对应的响应码 | 429 |
spring.cloud.sentinel.scg.fallback.content-type |
Spring Cloud Gateway 响应模式为 ‘response’ 模式对应的 content-type | application/json |
