Sentinel是阿里开源的项目,提供了流量控制、熔断降级、系统负载保护等多个维度来保障服务之间的稳定性。
java -Dserver.port=8088 -Dcsp.sentinel.dashboard.server=localhost:8088 -jar sentinel-dashboard-1.7.0.jar
在浏览器当中输入localhost:8088
用户名和密码为sentinel
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
sentinel:
transport:
dashboard: localhost:8088
启动服务访问 :http://localhost:8000/getGoodsWithID/1
查看控制台
资源名称,访问的路径,唯一名称** 针对来源:
阈值类型:
设置阈值 一秒种超过一次时, 就会触发限流
当联的资源达到域值时, 限流自己 查询接口调用过快, 如果影响修改接口,或者修改接口影响查询接口, 根据业务需求,来进行关联限流
示例:
@RequestMapping("/test")
public void test(){
System.out.println("test-------");
}
public static void main(String[] args) throws InterruptedException {
for (int i=0;i<1000;i++){
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://localhost:8000/test",Object.class);
Thread.sleep(500);
}
}
根据codeFactor从设置的阈值除以codeFactor,经过预热时长,才到达设置的QPS阈值 假设设置的阈值为90 那么就会90/3(默认) 30 作为最初的阈值 ,预热指定的时长 才去达到90的值限流
匀速排队,让请求以均匀的速度通过,阈值类型必须设置成QPS,否则无效 超过了定义的时长, 就会抛出异常
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.0</version>
</dependency>
private static final String GETGOODS_KEY = "getMyGoods";
private static final String GETGOODS_KEY = "getMyGoods";
@RequestMapping("/initFlowRules")
public void initFlowRules(){
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
//设置资源名称
rule.setResource(GETGOODS_KEY);
//设置针对来源
rule.setLimitApp("default");
//设置阈值类型
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 设置阈值
rule.setCount(1);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
@RequestMapping("/getMyGoods")
public String getMyGoods(){
try (Entry entry = SphU.entry(GETGOODS_KEY)) {
// 被保护的逻辑
return "getMyGoods";
} catch (BlockException ex) {
// 处理被流控的逻辑
return "被限流了";
}
}
启动运行 首先执行 initFlowRules 方法开启限流 http://localhost:8000/initFlowRules
然后执行http://localhost:8000/getMyGoods
当刷新次数超过一秒一次的时候就会被限流
但是每次启动都需要先访问开启initFlowRules方法,所以要设置服务启动时开启限流
private static final String GET_GOODS_KEY = "getMyGoods";
@SentinelResource(value = GET_GOODS_KEY,blockHandler = "blockHandlerMethod")
@RequestMapping("/getMyGoods")
public String getMyGoods(){
return "getMyGoods";
}
public String blockHandlerMethod(BlockException e){
e.printStackTrace();
return "被限流了---";
}
降级策略: RT:
异常比例:
异常次数:
指定索引参数,如果在时间窗口内超过了指定的阈值,就会触发熔断
参数例外项 可以对指定的参数再进行设置限流 参数级别 对指定的参数限流,甚至对指定参数的值进行限流 注意事项 : 参数必须是基本数据类型或String类型
发生以上这些情况的时候把你整个应用给你断掉。所有服务都不提供了
使用授权时要先指定来源,对指定来源的内容进行限流
指定来源方式 新建配置类
@Component
public class RequestOrigin implements RequestOriginParser {
@Override
public String parseOrigin(HttpServletRequest httpServletRequest) {
//从请求参数中获取origin的参数并返回
String origin = httpServletRequest.getParameter("origin");
//如果获取不到origin 就抛出异常
if (StringUtils.isBlank(origin)){
throw new IllegalArgumentException("必须指定origin");
}
//有参数就返回
return origin;
}
}
指定降级 .设置为黑名单,我这里截图没截好
feign:
sentinel:
enabled: true
@Component
public class GoodsFeignClientFalback implements GoodsFeignClient {
@Override
public Object getGoods() {
return ResponseResult.error("GoodsFeignFallback服务器正在维护,请稍后重试--");
}
@Override
public ResponseResult getgoodsWithID(Integer id) {
return ResponseResult.error("GoodsFeignFallback服务器正在维护,请稍后重试--");
}
@Override
public ResponseResult getgoodsObj(Goods goods) {
return ResponseResult.error("GoodsFeignFallback服务器正在维护,请稍后重试--");
}
}
FeignClient(name = "goods-provide",fallback = GoodsFeignClientFalback.class)
正常访问
设置流控
刷新多次
使用Nacos配置中心实现规则持久化
<!--添加此依赖,项目一启动时, 就去到nacos当中读取配置中心-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.0</version>
</dependency>
eager: true
datasource:
ds:
nacos:
server-addr: localhost:8849
group-id: DEFAULT_GROUP
rule-type: flow
data-id: getGoods
data-type: json
启动 读取nacos的流控规则
选择公网
勾选java、sdk、Springboot
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</exclusion>
</exclusions>
</dependency>
访问一次服务后 看AHAS控制台
和本地的sintinel一样的操作