SpringCloud Alibaba Sentinel实现熔断与限流
2021-02-16 12:19
标签:工程 超过 get group releases class rest 注册 lan 官网:https://github.com/alibaba/Sentinel 中文:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D 下载:https://github.com/alibaba/Sentinel/releases
1.1 大纲;
pom: yml: localhost:8080 控制台的地址,指定控制台后客户端会自动向该地址发送心跳包 (默认8719) 客户端提供给Dashboard访问或者查看Sentinel的运行访问的参数 主启动类: controller: 空空如也: 执行: 结果:
基本介绍
超过 1次/s:将会报
去掉暂停毫秒的代码,就是普通的QPS的模拟
应用场景:
SpringCloud Alibaba Sentinel实现熔断与限流 标签:工程 超过 get group releases class rest 注册 lan 原文地址:https://www.cnblogs.com/leeego-123/p/12706774.html运行:
1 初始化演示工程
1.2 创建cloudalibaba-sentinel-service8401
xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
parent>
artifactId>cloud2020artifactId>
groupId>com.eiletxie.springcloudgroupId>
version>1.0-SNAPSHOTversion>
parent>
modelVersion>4.0.0modelVersion>
artifactId>cloudalibaba-sentinel-service8401artifactId>
dependencies>
dependency>
groupId>com.alibaba.cloudgroupId>
artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependency>
groupId>com.alibaba.cspgroupId>
artifactId>sentinel-datasource-nacosartifactId>
dependency>
dependency>
groupId>com.alibaba.cloudgroupId>
artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
dependency>
groupId>com.eiletxie.springcloudgroupId>
artifactId>cloud-api-commonsartifactId>
version>${project.version}version>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-devtoolsartifactId>
scope>runtimescope>
optional>trueoptional>
dependency>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
optional>trueoptional>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependencies>
project>
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinal-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentin dashboard地址
dashboard: localhost:8080
# 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
port: 8719
management:
endpoints:
web:
exposure:
include: ‘*‘
/**
* @Author EiletXie
* @Since 2020/3/17 12:52
*/
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class,args);
}
}
package com.eiletxie.springcloud.alibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* @Author EiletXie
* @Since 2020/3/17 12:54
*/
@RestController
@Slf4j
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "----testA";
}
@GetMapping("/testB")
public String testB() {
return "----testB";
}
}
流控规则:
1 直接:api达到限流条件时,直接限流
阀值类型的QPS和线程数的区别:
QPS (每秒钟的请求数量) :当调用该api的QPS达到阈值的时候,进行限流
线程数:当调用该api的线程数达到阈值的时候,进行限流
2 关联: 当关联的资源达到阈值时,就限流自己
模拟访问B
结论:大批量线程高并发访问B,导致A失效了
流控效果:
2 预热:
3:排队等待
文章标题:SpringCloud Alibaba Sentinel实现熔断与限流
文章链接:http://soscw.com/index.php/essay/56089.html