Spring Cloud Alibaba——Sentinel流量控制框架

宅哥聊构架 工具 2022-06-24

1. Sentinel简介

Sentinel被称为分布式系统的流量防卫兵,是阿里开源流控框架,从服务限流、降级、熔断等多个维度保护服务,Sentinel提供了简洁易用的控制台,可以看到接入应用的秒级数据,并且可以在控制台设置一些规则保护应用,它比Hystrix支持的范围广,如Spring Cloud、Dubbo、gRPC都可以整合。

资源是Sentinel最关键的概念,遵循Sentinel API的开发规范定义资源,就能将应用保护起来。

而规则可以通过控制面板配置,也可以和资源联合起来,规则可以在控制台修改并且即时生效。

名词解释

  • 限流:不能让流量一窝蜂的进来,否则可能会冲垮系统,需要限载流量,一般采用排队的方式有序进行

    • 对应生活中的小例子:比如在一个景区,一般是不会让所有人在同一时间进去的,会限制人数,排队进入,让景区内人数在一个可控范围,因为景区的基础设施服务不了那么多人。
  • 降级:即使在系统出故障的情况下,也要尽可能的提供服务,在可用和不可用之间找一个平衡点,比如返回友好提示等。

    • 例如现在稍有规模的电商系统,为了给用户提供个性化服务,一般都有推荐系统,假设现在推荐系统宕机了,不应该在推荐商品一栏不给用户展示商品,反而可以降低一点要求,保证给用户看到的是友好界面,给用户返回一些准备好的静态数据。
  • 熔断:直接拒绝访问,然后返回友好提示,一般是根据请求失败率或请求响应时间做熔断。

    • 熔断好比家里的保险盒,当线路过热时,就会跳闸,以免烧坏电路。

2. Sentinel和同类产品对比

Sentinel、Hystrix、Resilience4j的异同

基础特性SentinelHystrixResilience4j
限流QPS、线程数、调用关系有限的支持Rate LImiter
注解的支持支持支持支持
动态规则配置支持多种数据源支持多种数据源有限支持
实时统计信息滑动窗口滑动窗口Ring Bit Buffer
熔断降级策略平均响应时间、异常比例、异常数异常比例平均响应时间、异常比例
控制台可配置各种规则,接口调用的秒级信息,机器发现等简单监控不提供控制台,可对接其它监控平台
流量整形支持预热、排队模式不支持简单的Rate Limiter模式
系统自适应限流支持不支持不支持
扩展性多个扩展点插件的形式接口的形式
常用适配框架Servlet、Spring Cloud、Dubbo、gRPC等Servlet、Spring Cloud、NetflixSpring Boot、Spring Cloud

3. 下载和运行

github地址

github.com/alibaba/Sen…

可以直接下载jar包运行jar包,也可以下载源码编译运行

因为它是springboot项目,下载jar包后直接运行jar包即可

java -jar sentinel-dashboard-1.8.3.jar

默认端口8080,如果需要修改,可以增加-Dserver.port参数,启动命令修改为java -jar -Dserver.port=9000 sentinel-dashboard-1.8.3.jar ,即可将程序端口改为9000

默认账号sentinel,默认密码sentinel,登录后页面是空白的,是因为sentinel采用懒加载的方式,只有证正使用它,功能才会展示出来

4. 项目集成Sentinel

4.1 创建提供者服务

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- 健康监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 服务注册/服务发现需要引入的 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- nacos配置中心依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- sentinel组件依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

server:
  port: 8082 #程序端口号
spring:
  application:
    name: provider # 应用名称
  cloud:
    sentinel:
      transport:
        port: 8719 # 启动HTTP Server,并且该服务与Sentinel仪表进行交互,是Sentinel仪表盘可以控制应用,如被占用,则从8719依次+1扫描
        dashboard: 127.0.0.1:8080 # 指定仪表盘地址
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # nacos服务注册、发现地址
      config:
        server-addr: 127.0.0.1:8848 # nacos配置中心地址
        file-extension: yml # 指定配置内容的数据格式
management:
  endpoints:
    web:
      exposure:
        include: '*' # 公开所有端点

写一个简单的Controller给消费者调用

@RestController
public class TestController {
    
    @GetMapping("/test")
    public String test(){
        return "provider test方法" + RandomUtils.nextInt(0,100);
    }
}
Apipost 私有化火热进行中

评论