0
点赞
收藏
分享

微信扫一扫

尚硅谷SpringCloud2020简单学习记录(个人用)111-123集

一叶随风_c94d 2022-04-16 阅读 78

Sentinel

前往https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D看介绍。

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

Sentinel 具有以下特征:

丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。

完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring CloudApache DubbogRPCQuarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。

完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

Sentinel 的主要特性:

Sentinel 分为两个部分:

核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。

控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

进入https://github.com/alibaba/Sentinel/releases  下载sentinel-dashboard-1.7.0.jar

保证8080端口不被占用,jdk8安装完毕

打开控制台,输入:java -jar sentinel-dashboard-1.7.0.jar

启动Sentinel

输入http://localhost:8080/ ,账号密码均为sentinel,进入控制台页面

启动nacosD:\nacos-server-1.1.4\nacos\bin>startup.cmd -m standalone

输入http://localhost:8848/nacos 进入nacos页面

创建cloudalibaba-sentinel-service8401模块

pom

<dependencies>

    <!--SpringCloud ailibaba nacos -->

    <dependency>

        <groupId>com.alibaba.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

    </dependency>

    <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->

    <dependency>

        <groupId>com.alibaba.csp</groupId>

        <artifactId>sentinel-datasource-nacos</artifactId>

    </dependency>

    <!--SpringCloud ailibaba sentinel -->

    <dependency>

        <groupId>com.alibaba.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

    </dependency>

    <!--openfeign-->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-openfeign</artifactId>

    </dependency>

    <!-- SpringBoot整合Web组件+actuator -->

    <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>

    <!--日常通用jar包配置-->

    <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>

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: '*'

主启动类:

@SpringBootApplication

@EnableDiscoveryClient

public class MainApp8401 {

    public static void main(String[] args) {

        SpringApplication.run(MainApp8401.class, args);

    }

}

Controller

@RestController

public class FlowLimitController {

    @GetMapping("/testA")

    public String testA()

    {

        return "------testA";

    }



    @GetMapping("/testB")

    public String testB()

    {

        return "------testB";

    }

}

启动nacossentienl,本模块,进入sentinel,会发现什么都没发生

需要先访问http://localhost:8401/testB

http://localhost:8401/testA

才会有相关内容显示(懒加载):

流控规则:

接下来对流控模式进行测试

首先是直接

表示1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误

多次刷新http://localhost:8401/testA

就会出现:Blocked by Sentinel (flow limiting)

成功了,但是该如何实现自己的处理方案?

然后是关联

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

或者B惹事,A挂了

当关联资源/testBqps阀值超过1时,就限流/testARest访问地址,当关联资源到阈值后限制配置好的资源名

使用postman进行模拟:

Postman里新建多线程集合组

将访问地址添加进新线程组:

启动postman,再访问A,发现A也挂了,得到了和上面一样的结果

接下来是链路:

只针对从指定链路访问到本资源的请求做统计,判断是否超过阈值

例如有两条请求链路:

/test1      /common

/test2      /common

如果只希望统计从/test2进入到/common的请求,对/test2 进行限流

总结:

1.直接:对当前资源限流

2.关联:高优先级资源触发阈值,对低优先级资源限流。

3.链路:阈值统计时,只统计从指定资源进入当前资源的请求,是对请求来源的限流

接着是流控效果

直接→快速失败:上面演示过了,会直接在页面上显示Blocked by Sentinel (flow limiting)

然后是预热

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

案例,阀值为10+预热时长设置5秒。

系统初始化的阀值为10 / 3 约等于3,即阀值刚开始为3;然后过了5秒后阀值才慢慢升高恢复到10

应用场景:

如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。

然后是排队等待

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

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

降级规则

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

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

窗口期过后关闭断路器

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

异常比列(秒级)

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

异常数(分钟级)

异常数(分钟统计)超过阈值时,触发降级;时间窗口结束后,关闭降级

Sentinel断路器是没有半开状态的

半开的状态系统自动去检测是否请求有异常,没有异常则关闭断路器恢复使用,有异常则继续打开断路器不可用,具体可以参考Hystrix

降级策略实战

RT

controller中加入:

@GetMapping("/testD")

public String testD()

{

    //暂停几秒钟线程

    try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }

    log.info("testD 测试RT");

    return "------testD";

}

Jmeter压测,

按照上述配置,

永远一秒钟打进来10个线程(大于5个了)调用testD,我们希望200毫秒处理完本次任务,如果超过200毫秒还没处理完,在未来1秒钟的时间窗口内,断路器打开(保险丝跳闸)微服务不可用,保险丝跳闸断电了。

后续停止jmeter,没有这么大的访问量了,断路器关闭(保险丝恢复),微服务恢复。

异常比例

修改testD方法

JMETER测试:

按照上述配置,单独访问一次,必然来一次报错一次(int age  = 10/0),调一次错一次;

开启jmeter后,直接高并发发送请求,多次调用达到我们的配置条件了。

断路器开启(保险丝跳闸),微服务不可用了,不再报错error而是服务降级了。

异常数:

时间窗口一定要大于等于60秒。

Controller添加testE

@GetMapping("/testE")
public String testE()
{
    
log.info("testE 测试异常比例");
    
int age = 10/0;
    
return "------testE 测试异常比例";
}

http://localhost:8401/testE,第一次访问绝对报错,因为除数不能为零,

我们看到error窗口,但是达到5次报错后,进入熔断后降级。

Jmeter测试:

以上为111-123集的内容

举报

相关推荐

0 条评论