1 El concepto de reglas de control de flujo para el centinela de componentes tolerantes a fallas
资源名:保护的资源对象,可以是容器、类、方法、代码
阈值类型:QPS(每秒访问次数)、线程数;
流控模式:
直接:接口达到限流时开启限流
关联:关联达到限流时开启限流,适用应用让步场景:查询query为关联save让步
链路:某个接口来的资源达到限流时开启限流
流控效果:
快速失败: 直接失败
warm-up: 预热,开始阈值为最大阈值的1/3缓慢增长到最大阈值,适用于突然增大的流量转换为缓步增长的场景
排队等待:超过超时时间就会丢弃请求
2 Caso de demostración del modo de enlace en las reglas de control de flujo
2.1 Modifique el archivo pom del proyecto principal, solo 2.1.1.RELEASE admite la función de enlace
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
2.2 Modifique el archivo pom del proyecto de orden de servicio ascendente y desactive la instanciación de CommonFilter de centinela
# 实现流控规则中的链路功能,关闭sentinel的CommonFilter实例化
spring:
cloud:
sentinel:
filter:
enabled: false
2.3 Inyecte manualmente la instancia de CommonFilter y desactive la instanciación de CommonFilter de sentinel
@Configuration
public class FilterContextConfig {
@Bean
public FilterRegistrationBean sentinelFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new CommonFilter());
registration.addUrlPatterns("/*");
// 入口资源关闭聚合
registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
registration.setName("sentinelFilter");
registration.setOrder(1);
return registration;
}
}
2.4 Agregar serviceImpl para aumentar los recursos requeridos por el enlace
@Service
public class OrderFlowControlLinkServiceImpl {
/**
* 服务容错组件sentinel的流控测试:测试3种模式(直接、关联、链路)中的链路
* 注解@SentinelResource,指定资源,value值为资源名称
*/
@SentinelResource(value="resource")
public String flowControlLink(){
return "服务容错组件sentinel的流控测试:测试3种模式(直接、关联、链路)中的链路";
}
}
2.5 Método de prueba de agregar dos entradas al controlador
/**
* 服务容错组件sentinel的流控测试:3种模式(直接、关联、链路)
*/
@RequestMapping("/sentinel/flow1")
public String flow1() {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss:SSS");
log.info("start:{}", sdf.format(new Date()));
orderFlowControlLinkService.flowControlLink();
log.info("end:{}", sdf.format(new Date()));
return "流控测试:3种模式(直接、关联、链路)";
}
/**
* 服务容错组件sentinel的流控测试:配合测试3种模式(直接、关联、链路)中的关联
*/
@RequestMapping("/sentinel/flow2")
public String flow2() {
orderFlowControlLinkService.flowControlLink();
return "流控测试:配合测试3种模式(直接、关联、链路)中的关联和链路";
}
2.6 La consola puede agregar control de enlace al recurso y el recurso de entrada puede ser flow1 o flow2 para la verificación de la prueba.