SpringCloudAlibaba——Sentinel服务熔断

目录

1、Sentinel概述

1.1、什么是Sentinel?

1.2、Sentinel的特性

2、Sentinel快速入门

2.1、Sentinel控制台的下载

2.2、启动Sentinel控制台

2.3、访问sentinel控制台

2.4、搭建项目

2.5、启动sentinel-service项目

3、Sentinel功能

3.1、实时监控

3.2、簇点链路

3.3、流控规则

3.4、熔断/降级规则

3.5、热点规则

3.6、服务熔断


1、Sentinel概述

1.1、什么是Sentinel?

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。

1.2、Sentinel的特性

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架 / 库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
  • 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

想要了解更多关于Sentinel的信息请查看官方网站home | Sentinelhomehttps://sentinelguard.io/zh-cn/index.html

2、Sentinel快速入门

2.1、Sentinel控制台的下载

sentinel-dashboard-1.8.6.jar

链接:https://pan.baidu.com/s/1QXbvWl18P5g-phDPFWHQCw 
提取码:vr80

也可以去github上进行下载最新版本

GitHub - alibaba/Sentinel: A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件) - GitHub - alibaba/Sentinel: A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)https://github.com/alibaba/Sentinel

2.2、启动Sentinel控制台

在jar包的路径中输入cmd,打开命令框 

 调用java -jar命令运行jar包

注意:该Sentinel运行默认的端口号是8080,请保证8080端口不被其他程序占用。

这样就代表运行成功

2.3、访问sentinel控制台

访问localhost:8080,此时就变成了sentinel的控制台登录页面

默认的用户密码为:sentinel  sentinel

登录进入,发现什么都没有,这是正常的 

2.4、搭建项目

创建一个sentinel-service项目

pom文件依赖 

<dependencies>
        <dependency>
            <groupId>cn.itssl</groupId>
            <artifactId>common-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--Nacos服务注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel-datasource-nacos 后续sentinel做持久化用到-->
        <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>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

application.yml文件 

spring:
  application:
    name: 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: '*'

启动类

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

controller类

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

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

2.5、启动sentinel-service项目

 

刷新sentinel控制台,我们会发现多出了一个sentinel-service的服务,必须先去访问路径,这边的sentinel控制台才会获取到,因为sentinel控制台是懒加载的。

 

我们可以看到,sentinel技术可以拥有这么多的功能,sentinel是非常强大的,这就是为什么sentinel逐渐替代Hystrix豪猪哥的原因。

3、Sentinel功能

3.1、实时监控

我们快速访问localhost:8401/testA或者localhost:8401/testB路径10次以上

 可以看到,所有的实时信息都显示到了页面上,并且还有QPS折线图显示,非常的直观。

3.2、簇点链路

这个推荐使用列表视图,更加直观简洁 

3.3、流控规则

QPS(直接失败):一秒时间内超过单机阈值数,直接进行失败

可以看到,快速访问的话,QPS肯定会超过阈值1,就会进行熔断保护,跳转到sentinel默认的保护页面输出Blocked by Sentinel(flow limiting) 

 

并发线程数(直接失败):线程数量超过阈值的话,就会失败。

关联:方法A和方法B,方法B关联方法A,当方法B出现异常时,方法A进行限流,使用场景:支付业务关联下单业务,订单业务出现异常了,就限流支付业务。

Warmup:预热,可以设置预热时间,默认coldFactor为3,即请求QPS从(threshold/3)开始,经多少预热时长才逐渐升至设定的QPS阈值。使用场景:秒杀系统可以先进行预热,慢慢的把流量放进来。

排队等待:让请求均匀的速度通过,底层是漏桶算法,会严格的控制请求的间隔时间。

3.4、熔断/降级规则

RT(平均响应时间):如果业务的处理时间>设置的RT(平均响应时间),在下一窗口时间,会触发降级。

异常比例(DEGRADE_GRADE_EXCEPTION_RATIO):当资源的每秒请求量(QPS)>=5,并且每秒异常总数占通过量的比值超过阈值(DegradeRule中的count)之后,资源进入降级状态,即在接下的时 间窗口(DegradeRule 中的 timewindow,以s为单位)之内,对这个方法的调用都会自动地返 回。异常比率的阔值范围是[0.0,1.0],代表0%-100%。

异常数(DEGRADE_GRADE_EXCEPTION_COUNT):当资源近1分钟的异常数目超过阈值之后会进行熔断。注意由于统计时间窗口是分钟级别的,若 timewindow 小于60s,则结束熔断状态后仍可能再进入熔断状态。

3.5、热点规则

@RestController
public class FlowLimitController {
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "del_testHotKey")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        return "----testHotKey";
    }
    public String del_testHotKey(String p1, String p2, BlockException blockException) {
        return "┭┮﹏┭┮,发生错误了!";
    }
}

但我们访问并携带参数p1快速访问时,此时的已经大于单机阈值1了,所有会走blockHandler属性,找到del_testHotKey方法 

 

设置对某个参数值进行高级配置,我们选择参数类型为String类型,参数值为5时,让它的限流阈值达到200,就是说参数值为5时,此时QPS达到200才会进行熔断。

 

可以看到,以最快的手速也不会进行熔断,因为QPS达到200只能通过Jmeter等一些测试工具。

 

3.6、服务熔断

只配置fallback,如果请求过程中出现了异常,就会通过fallback找到降级方法

@RequestMapping("consumer")
@RestController
public class ConsumerController {
    @Reference
    private UserService userService;

    @GetMapping("/getUser/{id}")
    @SentinelResource(value = "fallback",fallback = "handlerFallback")
    public User getUser(@PathVariable Integer id) {
        if (id == 2) {
            throw new RuntimeException("非法参数异常");
        }
        return userService.getUser(id);
    }
    public User handlerFallback(@PathVariable Integer id){
        User user=new User();
        user.setEmail("走了fallback降级服务");
        return user;
    }


}

只配置blockHandler,就不会触发降级方法

@RequestMapping("consumer")
@RestController
public class ConsumerController {
    @Reference
    private UserService userService;

    @GetMapping("/getUser/{id}")
    @SentinelResource(value = "fallback",blockHandler="blockHandler")
    public User getUser(@PathVariable Integer id) {
        if (id == 2) {
            throw new RuntimeException("非法参数异常");
        }
        return userService.getUser(id);
    }
    public User handlerFallback(@PathVariable Integer id){
        User user=new User();
        user.setEmail("走了fallback降级服务");
        return user;
    }


}

同时配置fallback和blockHandler属性

如果违反了sentinel控制台设置的限流,就会走blockHandler方法,如果请求有异常的路径,就会走fallback方法,如果在请求异常路径并又违反了sentinel控制台的限流规则,那么还是会走blockHandler方法的。

猜你喜欢

转载自blog.csdn.net/select_myname/article/details/128932609