Sentinel 运行、流控规则


一、安装Sentinel(阿里版Hystrix)

1.1下载Sentinel

管网:https://github.com/alibaba/Sentinel

Sentinel:分布式系统的流量防卫兵,以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel特点:

  • 丰富的应用场景
  • 完备的实时监控
  • 广泛的开源生态
  • 完善的SPI扩展点

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

本人用的1.7.0版本

 1.2 运行Sentinel控制台

sentinel组件由2部分构成:后台服务和前台8080页面

执行命令: 

java -jar sentinel-dashboard-1.7.0.jar

 1.3访问Sentinel控制台界面

访问地址:http://localhost:8080/#/login

 账号和密码均为:sentinel

 二、Sentinel初始化监控

2.1 启动Nacos服务(服务注册中心)和Sentinel控制台服务

2.2  建立sentinel-service8401微服务

2.2.1 新建Module命名为“cloudalibaba-sentinel-service8401‘

 2.2.2 改POM

<?xml version="1.0" encoding="UTF-8"?>
"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">
    
        springcloud-nacos
        com.ckfuture.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloudalibaba-sentinel-service8401

    
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

2.2.3 建YML

新建”application.yml“配置文件

server:
  port: 8401
spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719
management:
  endpoints:
    web:
      exposure:
        include:  '*' 

2.2.4 主启动

新建主启动类”MainApp8401“

package com.ckfuture.springcloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

2.2.5 业务类

新建业务类”FlowLimitController“

package com.ckfuture.springcloud.alibaba.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

整体目录结构:

2.3 启动Sentinel-dashboard8080

2.4 启动微服务8401

2.5 启动8401微服务后查看sentinel控制台

 登录Sentinel后发现啥也没有,这是因为Sentinel的懒加载模式。这里需要执行一次访问即可!!!

http://localhost:8401/testA

 然后在看Sentinel监控台效果,多次刷新访问接口查看效果!

 以上结论:sentinel8080正在监控微服务8401

三、Sentinel 流控(流量控制)规则

3.1 Sentinel流控规则简介

  • 资源名:唯一名称、默认请求路径
  • 针对来源:Sentinel可以针对调用者进行限流,填写微服务名称,默认default(不区分来源)
  • 阈值类型/单机阈值:

    QPS(每秒钟的请求数量):当调用该api的QPS达到阈值的时候,进行限流。

    线程数:当调用该api的线程数达到阈值的时候,进行限流。

  • 是否集群:不需要集群。
  • 流控模式:

    直接:api达到限流条件时,直接限流。

    关联:当关联的资源达到阈值时,就限流自己。

    链路:只记录指定链路上的流量(指定资源从入口资源进来的限流,如果达到阈值,就进行限流)【api级别的针对来源】。

  • 流控效果:

    快速失败:直接失败,抛异常。

    Warm Up:根据codeFactor(冷加载因子,默认3)的值,从阈值/codeFactor,经过预热时长,才达到设置的QPS阈值。

    排队等待:匀速排队,让请求以匀速的速度通过,阈值类型必须设置为QPS,否则无效。

3.2 Sentinel流控-直接失败(默认)

新建一个”流控规则“,针对”/testA“api的限制。

 疯狂请求接口会出现:

 此时达到限流效果。

3.3 Sentinel流控-关联

”别人惹事我自己买单“

当关联的资源达到阈值时候,就限流自己。当与A关联的资源B达到阈值后,就限流A自己。B惹事,A挂了

设置效果:

当关联资源/testB的QPS阈值超过1时候,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名。

利用Postman并发访问api

手动测试接口

 保存到指定的Collection中,本人的Collection为”SpringCloud“,选择Collection右边三角,选择【Run】

 设置并发线程数量和请求间隔时间。

点击【Run SpringCloud】Collection。

 请求testB接口的同时手动请求testA

 postman请求后在手动请求testA接口,此时无论多频繁(手速)访问都正常!!!

四、Sentinel 流控(流量控制)效果

4.1 直接->快速失败(默认流控处理)

4.2 预热(Warm Up)

公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值

Warm Up方式,即预热/冷启动方式。当系统长期处于低水位的情况下,当流量突然增加时,直接把系统拉升到高水位可能瞬间把系统压垮,通过”冷启动“,让通过的流量缓慢增加,在一定时间内逐渐增加到阈值上限,给冷系统一个预热时间,避免冷系统被压垮。

 开始快速请求接口

 5秒钟后快速请求就正常了保证阈值为10的并发~

4.3 排队等待

匀速排队,让请求以均匀的速度通过,阈值类型必须设成QPS,否则无效

设置含义:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。 

 修改一下java代码,增加打印日志便于查看效果。

package com.ckfuture.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA() {
        log.info(Thread.currentThread().getName()+"\t"+".....testA");
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }
}

利用Postman模拟请求

 查看java打印日志,一秒钟一次请求