• Spring Cloud Alibaba基础教程:Sentinel


    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来保障微服务的稳定性。

    1. 基本概念

    1.1 资源

    资源是 sentinel 中的一个关键概念,它代表受保护的内容,可以是程序中的任何内容,比如一个变量,一段代码等等。

    1.2 规则

    针对资源的控制规则,比如流量控制、熔断降级以及系统保护规则等,并且 sentinel 支持实时调整规则。

    2. 原理

    Sentinel 在熔断降级的设计理念上与 Hystrix 完全不一样。

    • Hystrix 采用线程池的方式对资源进行隔离,这样做的好处是资源与资源做到了彻底的隔离,但是由于使用了线程池,存在上下文切换消耗,对性能有一定的影响。
    • Sentinel 主要采取两种方式进行控制
      • 通过并发线程数控制:限定访问资源的最大并发线程数,超过则直接拒绝后续请求,直到之前堆积的请求处理完毕才能接受新的请求。好处是没有线程池,不需要而外的上下文切换消耗。
      • 通过请求响应时间对资源降级:当依赖的资源出现响应时间过长之后,后续所有请求都会被直接拒绝,直到过了指定的时间窗口期才会重新恢复。

    3. 实战

    3.1 部署控制台

    下载 Sentinel,下载完成之后启动控制台

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

    Sentinel

    3.2 创建Spring Boot项目

    添加依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            </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>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.1.RELEASE</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

    配置文件

    spring.application.name=sentinel-demo
    server.port=9090
    spring.cloud.sentinel.transport.dashboard=localhost:8080
    

    添加Endpoint

    @RestController
    @SpringBootApplication
    public class SentinelDemoApplication {
    
        @GetMapping("echo")
        public String echo(String message) {
            return "Echo: Hello " + message;
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SentinelDemoApplication.class, args);
        }
    
    }
    

    启动项目,测试Endpoint可以正常访问。接下来配置 Sentinel,添加流控规则
    Sentinel
    测试

    curl 'http://localhost:9090/echo?message=bro'
    

    可以看出来,如果在短时间内发出多个请求,会出现请求响应延迟和被拒绝的情况。说明配置的限流策略已经生效。

    Blocked by Sentinel (flow limiting)
    

    Sentinel 支持更多的流控策略,具体可以参考官方文档

  • 相关阅读:
    最新macOS 11.4+Xcode 12.5+iOS 14.6 编译安装WebDriverAgent填坑记
    python +pytest +yaml + Allure 实现接口自动化框架
    DNS原理及其解析过程
    jmeter性能测试--ramp_up和同步定时器的区别
    jmeter常用定时器
    基本sql语句
    MySQL连表查询
    MySQL约束条件
    MySQL单表查询
    linux 查看文件的前几行 或者 某些行
  • 原文地址:https://www.cnblogs.com/thisismarc/p/13035260.html
Copyright © 2020-2023  润新知