SpringCloud Alibaba之服务熔断与降级Sentinel(Alibaba第二篇)

本文详细介绍了如何使用Sentinel实现服务限流、降级策略,包括配置QPS、线程数、关联规则和链路限流,以及服务降级的RT规则、异常比例和异常数。还讨论了规则持久化的重要性及实现步骤,以及Sentinel在微服务架构中的应用实例。
摘要由CSDN通过智能技术生成

一、提前准备

1、分析之前的服务降级熔断Hystrix

  • 使用过我们的Hystrix后,就会发现它很是麻烦,很多东西我们要自己去配置表【就像我们之前学习的Eureka一样,我们不用再自己去建什么8001哦,作为Eureka的主机配置,使用了nacos就解决了,而sentinel就是替代料Hystrix】在这里插入图片描述
  • sentinel能干什么
    在这里插入图片描述

2、sentinel下载安装

3、sentinel初始化监控

  • 前提是你的nacos启动好。

(1)建项目

在这里插入图片描述

  • 导入依赖
<dependencies>
        <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>cn.mldn</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--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>

在这里插入图片描述

  • 主启动
    在这里插入图片描述

  • yaml
    在这里插入图片描述
    这个client-ip改为port:8719

  • 随便写点逻辑
    在这里插入图片描述

(2)启动相关8080,8401,8848

  • nacos启动
  • sentinel启动
  • 8401启动
    在这里插入图片描述
  • 访问测试
    在这里插入图片描述
    什么都没有,因为sentinel是采用懒加载机制,只有你的项目访问过了,才会被加载。
    在这里插入图片描述
  • 再次测试
    在这里插入图片描述
  • 我请求不断的来,但是就是不行,CAO哦,烦死了,天天报错。
  • 修改版本试一试还是不行,安装个Linux的试一试
    在这里插入图片描述
    在这里插入图片描述
  • 最后自己才发现是那个client-ip应该是port,这个client-ip后续才会用。

二、流量控制

(一)sentinel流量控制

1、sentinel流控的位置

在这里插入图片描述
在这里插入图片描述

  • 相关概念
    在这里插入图片描述
    在这里插入图片描述

2、流空规

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值