本文依托于
在SpringCloud Alibaba的使用过程中,我总结为如下步骤:
https://github.com/alibaba/Sentinel/releases/
java -jar sentinel-dashboard-1.8.4.jar
sentinel默认端口为8080,用户名:sentinel,密码:sentinel,上下文为空,路径不需要加sentinel
http://127.0.0.1:8080/
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
# 应用名称
spring.application.name=springcloud-alibaba-sentinel
server.port=9007
# Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=192.168.43.11:8080
# 取消Sentinel控制台懒加载
# 默认情况下 Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
# 配置 sentinel.eager=true 时,取消Sentinel控制台懒加载功能
spring.cloud.sentinel.eager=true
# 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用
# spring.cloud.sentinel.transport.client-ip=
# feign中激活sentinel,有些类似hystrix
feign.sentinel.enabled=true
package com.ykq.springcloudalibabasentinel.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: kqyin
* @date: 2022/3/25 11:14
* @Description:
*/
@RestController
public class SentinelController {
/**
* @Author kqyin
* @Date 2022/3/25 16:30
* @Description 测试限流
*/
@SentinelResource(value = "sentinelTestA", blockHandler = "handleTestA")
@RequestMapping(value = "/sentinelTestA")
public String sentinelTestA() {
return "sentinelTestA";
}
public String handleTestA(BlockException blockException) {
return "sentinelTestA is limited";
}
/**
* @Author kqyin
* @Date 2022/3/25 16:30
* @Description 测试熔断降级
*/
@SentinelResource(value = "sentinelTestB", fallback = "testBFallBack", blockHandler = "handleTestB")
@RequestMapping(value = "/sentinelTestB")
public String sentinelTestB() {
//return "sentinelTestB";
throw new RuntimeException("sentinelTestB throw RuntimeException");
}
public String handleTestB(BlockException blockException) {
return "sentinelTestB is limited";
}
public String testBFallBack() {
return "sentinelTestB is stop";
}
}
qps每秒多少次请求。下图表示在单机情况下,超过每秒3次请求就快速失败。
对应资源为代码中@SentinelResource值为sentinelTestA的代码。
在1000ms内,请求2次及以上,失败概率为100%的,熔断5秒
对应资源为代码中@SentinelResource值为sentinelTestB的代码。
@SentinelResource中的value和@RequestParam中的value不要一致。我测试时,两者一致,导致出现规则生效,但blockHandler大概率失效的情况。这是因为sentinel将方法已经定义为资源,value为@RequestParam的值
我们可以注意到,在sentinel中配置的规则,在客户端重启后,就失效了。这是因为规则默认保存在客户端的内存中,生产上是不允许这样使用的。需要将规则持久化到nacos配置中心。
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
<version>1.8.4version>
dependency>
# 资源sentinelTestA持久化到nacos配置
spring.cloud.sentinel.datasource.sentinelTestA.nacos.username=nacos
spring.cloud.sentinel.datasource.sentinelTestA.nacos.password=nacos
spring.cloud.sentinel.datasource.sentinelTestA.nacos.server-addr=192.168.43.11:8000
spring.cloud.sentinel.datasource.sentinelTestA.nacos.dataId=${spring.application.name}-sentinelTestA
spring.cloud.sentinel.datasource.sentinelTestA.nacos.groupId=SENTINEL_GROUP
# 规则:flow流控规则,degrade降级规则,authority授权规则,system系统规则, param-flow热点key规则
spring.cloud.sentinel.datasource.sentinelTestA.nacos.rule-type=flow
spring.cloud.sentinel.datasource.sentinelTestA.nacos.data-type=json
# 资源sentinelTestB持久化到nacos配置
spring.cloud.sentinel.datasource.sentinelTestB.nacos.username=nacos
spring.cloud.sentinel.datasource.sentinelTestB.nacos.password=nacos
spring.cloud.sentinel.datasource.sentinelTestB.nacos.server-addr=192.168.43.11:8000
spring.cloud.sentinel.datasource.sentinelTestB.nacos.dataId=${spring.application.name}-sentinelTestB
spring.cloud.sentinel.datasource.sentinelTestB.nacos.groupId=SENTINEL_GROUP
# 规则:flow流控规则,degrade降级规则,authority授权规则,system系统规则, param-flow热点key规则
spring.cloud.sentinel.datasource.sentinelTestB.nacos.rule-type=degrade
spring.cloud.sentinel.datasource.sentinelTestB.nacos.data-type=json
可以注意到,当nacos中配置的规则修改后,可以同步到sentinel的服务,但是sentinel修改后,不能修改nacos中的配置,只能保存到内存中。
规则配置的参数名,可以在AbstractRule的几个实现类中去查看,分别是
FlowRule(流控规则)参数
/**
* The threshold type of flow control (0: thread count, 1: QPS).
* 限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS来进行流量控制
*/
private int grade = RuleConstant.FLOW_GRADE_QPS;
/**
* Flow control threshold count.
* 限流阈值
*/
private double count;
/**
* Flow control strategy based on invocation chain.
* 调用关系限流策略
*
* {@link RuleConstant#STRATEGY_DIRECT} for direct flow control (by origin);
* {@link RuleConstant#STRATEGY_RELATE} for relevant flow control (with relevant resource);
* {@link RuleConstant#STRATEGY_CHAIN} for chain flow control (by entrance resource).
*/
private int strategy = RuleConstant.STRATEGY_DIRECT;
/**
* Reference resource in flow control with relevant resource or context.
*/
private String refResource;
/**
* Rate limiter control behavior.
* 0. default(reject directly), 1. warm up, 2. rate limiter, 3. warm up + rate limiter
*/
private int controlBehavior = RuleConstant.CONTROL_BEHAVIOR_DEFAULT;
private int warmUpPeriodSec = 10;
/**
* Max queueing time in rate limiter behavior.
*/
private int maxQueueingTimeMs = 500;
/**
* 是否为集群模式
*/
private boolean clusterMode;
/**
* Flow rule config for cluster mode.
*/
private ClusterFlowConfig clusterConfig;
/**
* The traffic shaping (throttling) controller.
*/
private TrafficShapingController controller;
DegradeRule(熔断规则)参数
/**
* Circuit breaking strategy (0: average RT, 1: exception ratio, 2: exception count).
* 0:慢
*/
private int grade = RuleConstant.DEGRADE_GRADE_RT;
/**
* Threshold count.
*/
private double count;
/**
* Recovery timeout (in seconds) when circuit breaker opens. After the timeout, the circuit breaker will
* transform to half-open state for trying a few requests.
*/
private int timeWindow;
/**
* Minimum number of requests (in an active statistic time span) that can trigger circuit breaking.
*
* @since 1.7.0
*/
private int minRequestAmount = RuleConstant.DEGRADE_DEFAULT_MIN_REQUEST_AMOUNT;
/**
* The threshold of slow request ratio in RT mode.
*/
private double slowRatioThreshold = 1.0d;
private int statIntervalMs = 1000;
https://blog.didispace.com/spring-cloud-alibaba-sentinel-2-4/