文章目录

  • ​​Sentinel介绍​​
  • ​​Sentinel 和 Hystrix 对比​​
  • ​​快速开始​​
  • ​​下载启动控制台​​
  • ​​示例代码​​
  • ​​@SentinelResource 介绍​​
  • ​​名词解释​​
  • ​​配置项​​

Sentinel介绍

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel具有以下特征:

  • 丰富的应用场景: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。
  • 完备的实时监控: Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态: Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
  • 完善的 SPI 扩展点: Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。

Sentinel 的主要特性:

Spring Cloud Alibaba Sentinel 初体验_Sentinel

Sentinel 分为两个部分:

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

Sentinel 和 Hystrix 对比

关于Hystrix 的介绍可以看springcloud 入门(4) Hystrix

Spring Cloud Alibaba Sentinel 初体验_ide_02

快速开始

下载启动控制台

Sentinel 控制台提供了一系列规则配置,如果不需要控制就要自己在代码里面写规则了,比较麻烦,所以有还是最好的。

下载地址:https://github.com/alibaba/Sentinel/releases

找到这个jar包下载就行

Spring Cloud Alibaba Sentinel 初体验_spring_03

Sentinel 控制台本身就是一个springboot的jar包

启动命令如下:

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

其中 -Dserver.port=8080 用于指定 Sentinel 控制台端口为 8080。

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel。

访问http://localhost:8080/#/login,进入Sentinel 管理页面

Spring Cloud Alibaba Sentinel 初体验_ide_04

登录成功后首页

Spring Cloud Alibaba Sentinel 初体验_Sentinel 入门_05

示例代码

引入的依赖都是在这篇文章springCloud Alibaba 2021版 nacos 注册中心初体验的基础上建立的。

引入的Spring Cloud Alibaba依赖及版本请查看上文

  1. 新建一个springboot项目sentinel-provider,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

spring-boot-starter-web 是必须要有的,Sentinel配置 需要在Servlet 环境才生效。

  1. 配置文件
server.port=8085
# 服务名
spring.application.name=SentinelProvider

spring.cloud.sentinel.transport.port=8719
# dashboard 地址
spring.cloud.sentinel.transport.dashboard=localhost:8080

配置的意思在后面会说到。

  1. 创建测试类

TestServiceImpl.java

@Service
public class TestServiceImpl implements TestService{

// 原函数
@SentinelResource(value = "sayHello", blockHandler = "exceptionHandler", fallback = "helloFallback")
public String sayHello(String name) throws Exception {

if ("throws".equals(name)) {
throw new Exception("exceptionHandler 生效了");
}
return "Hello, " + name;
}

// Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
public String helloFallback(String name) {
System.out.println("helloFallback 生效,params = "+name);
return "helloFallback 生效,params = "+name;
}

// Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
public String exceptionHandler(String name , BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "exceptionHandler起作用了,params="+name;
}
}

SentinelProviderController.java

@RestController
@RequestMapping("/provider")
public class SentinelProviderController {

@Autowired
private TestService service;

@GetMapping(value = "/hello/{name}")
public String apiHello(@PathVariable String name) throws Exception {
return service.sayHello(name);
}
}
  1. 测试
    启动sentinel-provider,启动之后访问sentinel控制台就能看到该服务,
    Spring Cloud Alibaba Sentinel 初体验_Sentinel 入门_06
  2. 先通过浏览器访问进行一个正常的测试:
    Spring Cloud Alibaba Sentinel 初体验_spring_07
  3. 再进行一个抛异常的测试:
    Spring Cloud Alibaba Sentinel 初体验_ide_08
  4. 最后再通过控制台进行一个降级测试:
  • 在簇点链路下找到我们使用了@SentinelResource的资源,只有先调用接口之后才能在簇点链路找到对应的资源
    Spring Cloud Alibaba Sentinel 初体验_spring_09
  • 在流控中进行配置流控规则
    为了简单测试一下,我们就将QPS单机阈值设置为1
    Spring Cloud Alibaba Sentinel 初体验_Sentinel_10
    在流控规则列表中可以看到我们配置的规则:

Spring Cloud Alibaba Sentinel 初体验_spring_11

降级测试:

多刷新几个调用就会看到如下页面

Spring Cloud Alibaba Sentinel 初体验_Sentinel 入门_12

这样一个简单的测试就结束了,至于@SentinelResource注解是什么意思,请接着往下看。

@SentinelResource 介绍


注意:注解方式埋点不支持 private 方法。


​@SentinelResource​​​ 用于定义资源,并提供可选的异常处理和 fallback 配置项。 ​​@SentinelResource​​ 注解包含以下属性:

  • ​value​​:资源名称,必需项(不能为空)
  • ​entryType​​​:资源调用的流量类型,配置是入口流量(​​EntryType.IN​​​)还是出口流量(​​EntryType.OUT​​​),注意系统规则只对 IN 生效,可选项(默认为​​EntryType.OUT​​)
  • ​blockHandler​​​ /​​blockHandlerClass​​​: 在原方法被限流/降级/系统保护的时候调用,​​blockHandler​​​ 对应处理​​BlockException​​​ 的方法名称,可选项。blockHandler 方法访问范围需要是​​public​​​,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为​​BlockException​​​。blockHandler 方法默认需要和原方法在同一个类中。若希望使用其他类的方法,则可以指定​​blockHandlerClass​​​ 为对应的类的​​Class​​ 对象,注意对应的方法必需为 static方法,否则无法解析。
  • ​fallback​​​ /​​fallbackClass​​​:fallback 方法名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 方法可以针对所有类型的异常(除了​​exceptionsToIgnore​​ 里面排除掉的异常类型)进行处理。fallback 方法签名和位置要求:
  • 返回值类型必须与原方法返回值类型一致;
  • 方法参数列表需要和原方法一致,或者可以额外多一个​​Throwable​​ 类型的参数用于接收对应的异常。
  • fallback方法默认需要和原方法在同一个类中。若希望使用其他类的方法,则可以指定​​fallbackClass​​​ 为对应的类的​​Class​​ 对象,注意对应的方法必需为 static方法,否则无法解析。
  • ​defaultFallback​​​(since 1.6.0):默认的 fallback 方法名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 方法可以针对所有类型的异常(除了​​exceptionsToIgnore​​ 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 方法签名要求:
  • 返回值类型必须与原方法返回值类型一致;
  • 方法参数列表需要为空,或者可以额外多一个​​Throwable​​ 类型的参数用于接收对应的异常。
  • defaultFallback方法默认需要和原方法在同一个类中。若希望使用其他类的方法,则可以指定​​fallbackClass​​​ 为对应的类的​​Class​​ 对象,注意对应的方法必需为 static 方法,否则无法解析。
  • ​exceptionsToIgnore​​(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。

原方法 是指使用了 ​​@SentinelResource​​​注解的方法,1.8.0 版本开始,​​defaultFallback​​ 支持在类级别进行配置。


注:1.6.0 之前的版本 fallback 函数只针对降级异常(​​DegradeException​​)进行处理,不能针对业务异常进行处理


特别地,若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 ​​BlockException​​​ 时只会进入 ​​blockHandler​​​ 处理逻辑。若未配置 ​​blockHandler​​​、​​fallback​​​ 和 ​​defaultFallback​​​,则被限流降级时会将 ​​BlockException​直接抛出(若方法本身未定义 throws BlockException 则会被 JVM 包装一层 ​​UndeclaredThrowableException​​)。

名词解释

  • 资源:可以是任何东西,服务,服务里的方法,甚至是一段代码。使用 Sentinel 来进行资源保护,主要分为几个步骤:
  1. 定义资源
  2. 定义规则
  3. 检验规则是否生效

先把可能需要保护的资源定义好,之后再配置规则。也可以理解为,只要有了资源,我们就可以在任何时候灵活地定义各种流量控制规则。在编码的时候,只需要考虑这个代码是否需要保护,如果需要保护,就将之定义为一个资源。

配置项

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​

WarmUp 模式中的 冷启动因子

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

NOTE: 请注意,这些配置只有在 Servlet 环境下才会生效,RestTemplate 和 Feign 针对这些配置都无法生效

参考文章:


​https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel​


能力一般,水平有限,如有错误,请多指出。

如果对你有用点个关注给个赞呗

更多文章可以关注一下我的微信公众号suncodernote

Spring Cloud Alibaba Sentinel 初体验_Sentinel 入门_13