SpringCloud高级(三) ——SpringCloud Alibaba Sentinel篇一

简介

是什么

Sentinel是一个轻量级的流量控制,熔断降级的java库。分布式系统的流量防卫兵。Hystrix豪猪哥的阿里版。

Hystrix与Sentinel对比

在这里插入图片描述

能干嘛

秒杀,抢红包,抢金条,抽奖等形式多种多样的活动,本质上这些活动都有类似的特点:时间短,并发高,有流量洪峰。那么,系统能抗住每次的流量洪峰吗?如果超过了系统的压力阀值系统会怎样?系统目前运行的情况又是怎样的呢?
Sentinel可以解决如下问题:

  • 限流:当我们的系统上线一个接口,那么这个接口在被访问时会消耗一些资源,如果该接口的处理上限是1秒服务3000个QPS(QPS每秒查询率),但如果实际情况遇到高于3000的QPS该如何解决呢?Sentinel提供两种限流方式

    • 一种是统计并发线程数:当并发线程数超过某个设定的阀值,新的请求会被立即拒绝
    • 一种是统计QPS:当QPS超出某个设定的阀值,系统可以通过直接决绝、冷启动、匀速器三种方式来应对,从而起流量控制作用
  • 熔断降级:服务与服务之间会有相互依赖关系,例如:服务A能够支撑1秒1W个QPS,但此时服务B并无法满足1W个QPS,那么如何保证服务A在高频调用服务B时,服务B仍能正常工作呢?这时就需要服务熔断和降级。

    • Sentinel能实现对服务进行熔断或降级。这与Hystrix的原则是一致的,当调用链路中某个资源出现不稳定,例如:表现为超时,异常比例升高的时候,则对这个资源的调用进行限制,并让请求快速失败,避免影响到其他的资源,最终产生雪崩的效果
    • 服务降级:服务器忙,不让客户端等待并立刻返回一个友好提示,fallback
    • 服务熔断:类似于我们家用的保险丝,当某服务出现不可用或响应超时的情况时,为了防止整个系统出现雪崩,暂时停止对该服务的调用。
  • 塑形:通常我们遇到的流量具有随机性,不规则性,不受控,突然高峰流量等特点,但是系统的能力往往是有限的,我们需要根据系统的处理能力对流量进行塑形,即规则化,从而根据我们的需要来处理流量。Sentinel通过资源的调用关系,运行指标,控制的效果三个维度来对流量进行控制。
    在这里插入图片描述

  • 系统负载保护:正常情况下我们的系统运行都没问题,但是遇到高峰流量的时候,发现机器的负载load非常高,这时候对系统的负载保护就显得非常重要,以防止雪崩。Sentinel提供了对应的保护机制,让系统入口流量和系统的负载达到一个平衡,保证系统在能力范围内处理最多的请求。

怎么玩

官网:https://github.com/alibaba/Sentinel
中文文档:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
服务使用中的各种问题:

  • 服务雪崩
  • 服务降级
  • 服务熔断
  • 服务限流

安装

Sentinel 分为两个部分:

扫描二维码关注公众号,回复: 11919534 查看本文章
  • 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud
    等框架也有较好的支持。
  • 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

安装Sentinel控制台

直接安装就行了,不要用docker去装,坑多
下载地址:https://github.com/alibaba/Sentinel/releases
下载到本地sentinel-dashboard-1.7.2.jar
启动:如果使用的是云服务器,记得开启对应的8082端口号,这个端口号可以自定义。
命令:java -Dserver.port=8082 -Dcsp.sentinel.dashboard.server=localhost:8082 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.2.jar

-Dserver.port用于指定 Sentinel 控制台端口为 8080
-Dcsp.sentinel.dashboard.server=localhost:8082 客户端连接sentinel时,需要指定sentinel地址,这里指定是因为sentinel自带一个客户端
-Dproject.name=sentinel-dashboard 指定客户端的名字

访问接口:http://localhost:8082/#/login 默认账号密码:sentinel sentinel
在这里插入图片描述
在这里插入图片描述
不想把自带的客户端接入控制台直接启动就行:
命令:java -Dserver.port=8082 -jar sentinel-dashboard-1.7.2.jar

在这里插入图片描述

初始化演示工程

启动nacos注册中心。

新建Module

module :cloudalibaba-sentinel-service8401
依赖:
sentinel导入
actuator也要导入,sentinel实时监控需要

    <dependencies>
        <dependency>
            <groupId>com.lzh.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--nacos注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
         <!--sentinel持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

配置文件:

server:
  port: 8401
spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #nacos注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8082 #sentinel控制台地址
        port: 8719  #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口
#暴露监控端口,sentinel实时监控需要
management:
  endpoints:
    web:
      exposure:
        include: '*'

启动类:

@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}

业务类:

@RestController
public class FlowLimitController
{
    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {

        return "------testB";
    }
}

测试:启动sentinel,nacos。再启动当前启动微服务8401
访问接口:http://localhost:8401/testA http://localhost:8401/testB
刷新sentinel控制台:发现客户端已经接入控制台了。注意是以懒加载的方式接入的,访问了接口才接入
在这里插入图片描述
第二种方式把客户端接入控制台:上面是通过在配置文件里配置来接入控制台的
也可以在项目启动的时候,加入启动参数来接入

-Dproject.name=项目名
-Dcsp.sentinel.dashboard.server=sentinelIP:Port

猜你喜欢

转载自blog.csdn.net/weixin_42412601/article/details/107295703