Sentinel实现熔断与限流

Spring Alibaba Sentinel 简介

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。在大规模微服务架构的场景下,避免服务出现雪崩,要减少停机时间,要尽可能的提高服务可用性。限流和降级是一个非常重要的手段,具体实施方法可以归纳为八字箴言,分别是限流,降级,熔断和隔离。Sentinel 以流量为切入点,从流量控制熔断降级、系统负载保护等多个维度保护服务的稳定性。

官网地址https://github.com/alibaba/Sentinel

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

Sentinel 的主要特性:
在这里插入图片描述
Sentinel 目前已经针对 Servlet、Dubbo、Spring Boot/Spring Cloud、gRPC 等进行了适配,用户只需引入相应依赖并进行简单配置即可非常方便地享受 Sentinel 的高可用流量防护能力。

Sentinel与Hystrix的区别

详情参考https://yq.aliyun.com/articles/633786/
下面简单介绍:

  1. Hystrix常用的线程池隔离会造成线程上下切换的overhead比较大;
  2. Hystrix使用的信号量隔离对某个资源调用的并发数进行控制,效果不错,但是无法对慢调用进行自动降级;
  3. Sentinel通过并发线程数的流量控制提供信号量隔离的功能;
  4. Sentinel支持的熔断降级维度更多,可对多种指标进行流控、熔断,且提供了实时监控和控制面板,功能更为强大。

下载安装Sentinel

Sentinel 分为两个部分组成

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

地址:https://github.com/alibaba/Sentinel/releases/tag/1.7.2

在这里插入图片描述

运行

  1. Sentinel 身是一个SpringBoot项目,直接使用 jar命令启动项目

java -jar sentinel-dashboard-1.7.2.jar

  1. Sentinel 默认端口是 8080 , 如果8080被占用,可以使用下面的方式映射到8888端口启动

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

  1. 运行如图
    在这里插入图片描述
  2. 浏览器访问控制台 : http://localhost:8080
  3. 登录账号密码都是 sentinel
  4. 如图
    在这里插入图片描述
  5. 登录成功后暂时没有任何数据
    在这里插入图片描述

开始整合Sentinel

  1. 创建SpringBoot模块, 引入一个 web 模块即可
    在这里插入图片描述
  2. pom.xml文件中引入
<!--  引入sentinel相关的依赖-->
<dependency>
   <groupId>com.alibaba.csp</groupId>
   <artifactId>sentinel-datasource-nacos</artifactId>
 </dependency>
 <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 </dependency>

 <!-- nacos注册中心客户端依赖 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>0.2.2.RELEASE</version>
 </dependency>
 
 <!-- 引入feign的依赖 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
   <version>2.2.2.RELEASE</version>
 </dependency>
 
 <!-- 引入 hystrix 依赖 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   <version>2.2.1.RELEASE</version>
 </dependency>
 
 <!-- SpringCloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud alibaba 2.1.0.RELEASE-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  1. yml文件中的配置
spring:
  application:
    name: nacos-consumer-user

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848   #Nacos服务注册中心地址
    sentinel:
      transport:
        port: 8719  # 默认是 8716
        dashboard: localhost:8080   #配置Sentinel dashboard地址
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: nacos-consumer-user-sentinel
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

server:
  port: 9000

feign:
  hystrix:
    enabled: true   # 启用支持 hystrix
  sentinel:
    enabled: true   # 激活Sentinel对Feign的支持

  1. 再次出现就可以查看到请求的数据
    在这里插入图片描述
    在这里插入图片描述
  2. 添加流控规则 访问 http://localhost:9000/user?id=1 每秒请求数量超过1次的请求都会被sentinel直接快速返回失败
    在这里插入图片描述
    以上就是对 Sentinel 熔断及数据流监控的整合了

感谢阅读, 如有什么更好的建议或方法 ,可以留言或进群交流:1101584918

猜你喜欢

转载自blog.csdn.net/Lance_welcome/article/details/106608569