Sentine

Sentinel

第1章 Sentinel概述

1.1 Sentinel下载地址以及官方文档

下载地址

https://github.com/alibaba/Sentinel/releases

官方文档

https://github.com/alibaba/Sentinel

1.2 Sentinel主要特征

在这里插入图片描述

1.3 Sentinel的作用
  • 防止服务雪崩
  • 服务降级
  • 服务熔断
  • 服务限流
1.4 Sentinel的两个部分
  • 核心库(Java客户端)不依赖任何框架/库,能够运行于Java所有运行时环境,同时对Dubbo/Spring Cloud等框架也有较好的支持。
  • 控制台(Dashboard)基于SpringBoot开发,打包后可以直接运行,不需要额外的Tomcat等应用容器
1.5 Sentinel的运行

下载好Sentinie的Jar包后,在当前目录进行cmd,输入

java -jar jar包名

Sentinel的默认端口为8080,使用前需要保证当前8080端口未被占用

在浏览器输入:localhost:8080

默认账号密码为:sentine

第2章 Sentinel初始化监控

需求:

将sentinel配合nacos进行微服务监控

步骤:

  1. 导入依赖

    父工程依赖管理

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.zhiwang</groupId>
        <artifactId>spring-cloud-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <modules>
            <module>nacos-provider</module>
            <module>nacos-consumer</module>
            <module>nacos-provider2</module>
            <module>cloudalibaba-sentinel-service8401</module>
        </modules>
        <!--统一管理jar包版本-->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
            <mysql.version>5.1.47</mysql.version>
            <druid.version>1.1.16</druid.version>
            <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
        </properties>
    
        <!--子模块继承后,提供作用:锁定版本+子module不用写groupId和version-->
        <dependencyManagement>
            <dependencies>
                <!--SpringBoot 2.2.2-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.2.2.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--SpringCloud Hoxton.SR1-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--SpringCloud alibaba 2.1.0-->
                <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>
                <!--mysql-->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.spring.boot.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    </project>
    

    子模块依赖

    <dependencies>
            <!--nacos-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    
            </dependency>
            <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>
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-datasource-nacos</artifactId>
    
            </dependency>
            <!--openfeigen-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
    
            </dependency>
            <!--SpringBoot-->
            <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>
        </dependencies>
    
  2. 创建启动类

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

    server:
      port: 8401
    spring:
      application:
        name: cloudalibaba-sentinel-service
      cloud:
        nacos:
          discovery:
            #nacos服务注册中心地址
            server-addr: localhost:8848
          config:
            server-addr: localhost:8848
        sentinel:
          transport:
            #配置sentinel的dashboard地址
            dashboard: localhost:8080
            #默认为8719端口,假如端口被占用,将会依次+1,直到未被占用的端口
            port: 8719
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    
  4. 编写controller

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class FlowLimitController {
        @GetMapping("/test1")
        public String test1(){
            return "----- test1";
        }
    
        @GetMapping("/test2")
        public String test2(){
            return "----- test2";
        }
    }
    
  5. 开启SpringBoot启动类,然后进行测试

    注意:

    Sentinel监控服务需要服务首先运行了一次,进入Sentinel后页面才会有显示

第3章 Sentinel流控规则

  • 资源名:唯一名称,默认请求路径
  • 针对来源: Sentinel可以针对调用者进行限流,填写微服务名,默认default(不区分来源)
  • 阈值类型/单机阈值:
    • QPS(每秒钟的请求数量):当调用该api的QPS达到阈值的时候,进行限流。
    • 线程数:当调用该api的线程数达到阈值的时候,进行限流。
  • 是否集群:不需要集群
  • 流控模式:
    • 直接: api达到限流条件时,直接限流
    • 关联:当关联的资源达到阈值时,就限流自己
    • 链路:只记录指定链路上的流量(指定资源从入口资源进来的流量,如果达到阈值,就进行限流)【api级别的针对来源】
  • 流控效果:
    • 快速失败:直接失败,抛异常
    • Warm Up:根据codeFactor (冷加载因子,默认3)的值,从阈值/codeFactor,经过预热时长,才达到设置的QPS阈值

第4章 Sentinel熔断降级规则

Sentine的熔断器是没有半开状态的

1.1 RT(平均响应时间,秒级)

平均响应时间超出阈值且在时间窗口内通过的请求>=5,两个条件同时满足后触发降级窗口期过后关闭断路器

RT最大4900(更大的需要通过-Dcsp.sentinel.statistic.max.rt=XXXX才能生效)

1.2 异常比例(秒级)

QPS >=5且异常比例(秒级统计)超过阈值时,触发降级;时间窗口结束后,关闭降级

1.3 异常数

当资源近1分钟的异常数目超过阈值之后会进行熔断。

注意由于统计时间窗口是分钟级别的,若 timewindow小于60s,则结束熔断状态后仍可能再进入熔断状态。

第5章 Sentinel持久化规则

  1. 导入依赖:

            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-datasource-nacos</artifactId>
            </dependency>
    
  2. 编写application.yml

    server:
      port: 8401
    spring:
      application:
        name: cloudalibaba-sentinel-service
      cloud:
        nacos:
          discovery:
            #nacos服务注册中心地址
            server-addr: localhost:8848
          config:
            server-addr: localhost:8848
        sentinel:
          transport:
            #配置sentinel的dashboard地址
            dashboard: localhost:8080
            #默认为8719端口,假如端口被占用,将会依次+1,直到未被占用的端口
            port: 8719
            datasource:
                     dsl:
                       nacos:
                         server-addr: localhost:8848
                         dataId: cloudalibaba-sentinel-service
                         groupId: DEFAULT_GROUP
                         data-type: json
                         rule-type: flow
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    feign:
      sentinel:
        enabled: true #激活sentinel对feign的支持
    
  3. 在Sentinel的控制台写入

    [
        {
            "resource": "/rateLimit/byUrl",
            "limitApp": "default",
            "grade" : 1,
            "count": 1,
            "strategy": o,
            "controlBehavior": 0,
            "clusterMode": false
        }
    ]
    

    详解:

    resource:资源名称

    limitApp:来源应用

    grade:阈值类型,0表示线程数,1表示QPS

    count:单机阈值

    strategy:流控模式,0表示直接,1表示关联,2表示链路

    controlBehavior:流控效果,0表示快速失败,1表示Warm up,2表示排队等待

    clusterMode:是否集群

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值