15. SpringCloudAlibaba Sentinel实现熔断与限流

在这里插入图片描述

1.sentinel概述

1.1 官网

https://github.com/alibaba/Sentinel

1.2 是什么


一句话解释,之前我们用过的Hystrix

1.3 去哪下

https://github.com/alibaba/Sentinel/releases

1.4 能干嘛

1.5 怎么玩

https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel

服务使用中的各种问题:

  • 服务雪崩
  • 服务降级
  • 服务熔断
  • 服务限流

2. 安装Sentinel控制台

2.1 Sentinel分为两个部分

核心库(Java客户端) 不依赖任何框架/库,能够运行于所有Java运行时环境,同时对Dubbo/Spring Cloud等框架也有较好的支持
控制台(Dashboard)基于Spring Boot开发,打包后可以直接运行,不需要额外的Tomcat等应用容器

2.2 安装步骤
2.2.1 运行命令

java -jar sentinel-dashboard-1.7.1.jar 前提是(java8环境OK 8080端口不能被占用)

2.2.2 访问sentinel 管理界面

http://localhost:8080/#/login
登录账号密码均为sentinel

3. 初始化演示工程

3.1 启动Nacos8848成功
3.2 新建模块 cloudalibaba-sentinel-service8401

pom文件

<dependencies>
    <!-- nacos-discovery-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- nacos持久化-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
    <!-- SpringCloud alilibaba sentinel-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <!-- 引入自己定义的api通用包-->
    <dependency>
        <groupId>com.jd.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${
   
   project.version}</version>
    </dependency>
    <!-- boot 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>
    <!-- devtools-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <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: '*'

主启动

@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
   
   

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

业务类 FlowLimitController

@RestController
public class FlowLimitController {
   
   

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

    @GetMapping("/testB")
    public String testB(){
   
   
        return "------testB";
    }
}
3.3 启动sentinel8080

java -jar sentinel-dashboard-1.7.1.jar

3.4 启动微服务8401
3.5 查看sentinel控制台


空空如也,啥都没有

3.5.1 sentinel采用的懒加载说明

需要先执行一次访问

  • http://localhost:8401/testA
3.5.2 效果

4. 流控规则

4.1 基本介绍

4.2 流控模式
4.2.1 直接(默认)

直接-> 快速失败
配置及说明


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

测试
快速点击访问 http://localhost:8401/testA

结果

4.2.2 关联

是什么

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

配置A

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

postman模拟并发密集访问testB

(1) 访问testB成功


(2) postman里新建多线程集合组

(3) 将访问地址添加进新新线路组

大批量线程高并发访问B,导致testA挂了

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值