在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用,但是由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时若有大量的网络涌入,会形成任务堆积,最终导致服务瘫痪。
由于服务于服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的后果,这就是服务故障的”雪崩效应“。对于服务雪崩的源头的产生是无法杜绝的,但是我们可以做好足够的容错,保证一个服务发生问题时,不会影响到其他服务的正常运行,也就是”雪落而不雪崩“,这就时服务容错的目的。
1. Sentinel的使用
首先我们需要在微服务的pom.xml中引入Sentinel的依赖,如下所示:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
然后在application.yml中加入有关Sentinel的配置
spring:
cloud:
sentinel:
transport:
port: 9999 #跟控制台交流的端口,随意指定一个未使用的端口即可
dashboard: localhost:8080 # 指定控制台服务的地址
此时我们在前端访问Sentinel的控制页面即可看到对应的Controller类中的方法。
2. 常见的容错方案
常见的容错思路主要有隔离、超时、限流、熔断、降级这几种,下面主要介绍一下这几种容错方案。
隔离是指将系统按照一定的原则划分为若干个服务模块,各个模块之间相对独立,无强依赖,当有故障发生时,能将问题和影响隔离在某个模块内部,而不扩散风险,不波及其他模块,不影响整体服务。常见的隔离方式有:线程池隔离和信号量隔离。
超时是指在上游服务调用下游服务的时候,设置一个最大响应时间,如果超越整个时间,下游还未做出反应,就断开请求,释放掉线程。
限流就是限制系统的输入和输出流量以达到保护系统的目的,为了保证系统的稳固运行,一旦达到需要限制的阈值,就需要限制流量并采取少量措施以完成限制流量的目的。
熔断是指在互联网系统中,当下游服务因为访问压力过大而影响变慢或者失败,上有服务为了保证系统整体的可用性,可以暂时切断对下游服务的调用。这种牺牲局部,保全整体的措施就叫做熔断。
降级其实就是为服务提供一个托底方案,一旦服务无法正常调用,就是用托底方案。
3. Sentinel的容错实施
3.1 使用Sentinel实现流控
流量控制的原理就是监控应用流量的QPS(每秒查询率)或并发线程数的指标,当达到指定的阈值时对流量进行控制,以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性。我们使用Sentinel的前端界面配置资源的流控,可以看到在下图中,我们对/order/message资源设置了QPS为3的流控,当QPS大于3时,将不能访问该资源。
流控模式分为直接模式和关联模式以及链路模式。直接流控模式将会直接关联资源,而关联模式是指其他的资源的流量较大是,本资源也会被限流不能访问。如下图所示,当我们关联的资源/order/message2超过我们定义的QPS为3时,资源/order/message将会被限制,不能被访问,这就是关联模式的流控。
链路流控模式是指,当某个接口过来过来的资源达到限流条件时,开启限流。
流控的效果也分为三种类型,分别是快速失败(默认),Warm Up和排队等待三种效果。
快速失败:直接失败,抛出异常,不做额外的处理,是最简单的效果。
Warm Up:他从开始阈值到最大QPS阈值会有一个缓冲阶段,一开始的阈值是最大QOS阈值的1/3,然后慢慢增长,直到最大阈值,适用于将突然增大的流量转换为缓步增长的场景。
排队等待: 让请求以均匀的速度通过,单机阈值为每秒通过数量,其余的排队等待,该模式还会设置一个超时时间,当请求超过超时时间还未处理,则会被丢弃。
3.2 配置降级规则
降级规则就是设置当满足什么条件的时候,对服务进行降级。Sentinel的降级规则有三个衡量规则分别是平均响应时间、异常比例和异常数。
平均访问时间:当资源的平均响应时间超过阈值(以ms为单位)之后,资源进入准降级状态。如果在接下来1s内持续进入5个请求,他们的RT都持续超过整个阈值,那么在接下的时间窗口(以s为单位)之内,就会对整个方法进行服务降级。
3.3 配置热点规则
热点参数流控规则是一种更细粒度的流控规则,它允许将规则具体到参数上。
首先我们需要在想要使用热点规则的方法上加上@SentinelResource()注解,加上这个注解,热点规则才会生效。
然后我们就可以在前端界面对该方法中参数的热点规则进行设置,如下图所示,我们对message这个资源中的第一个参数采取了QPS的流控模式,并设计在1s内的阈值为1。
3.4 授权规则
对于一些时候,我们需要根据调用来源判断该请求是否允许放行,这个时候可以使用Sentinel的来源访问的功能。来源访问控制根据资源的请求来源限制资源是否通过。如下所示,我们设置访问message这个资源的来源,白名单代表可以访问该资源,黑名单表示不可以访问该资源。
其中流控应用指的是来源标识,如果想要访问Sentinel保护的接口资源,Sentinel就会调用RequestOriginParser的实现类去解析访问来源,如果解析出来的来源是黑名单的就不会放行访问Sentinel保护的资源。
@Component
public class RequestOriginParserDefinition implements RequestOriginParser{
@Override
public String parseOrigin(HttpServletRequest request) {
String serviceName = request.getParameter("serviceName");
return serviceName;
}
}
3.5 系统规则
系统保护规则是从应用级别的入口流量进行控制,从单台机器的总体 Load、RT、入口 QPS 、CPU 使用率和线程数五个维度监控应用数据,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。 系统保护规则是应用整体维度的,而不是资源维度的,并且仅对入口流量 (进入应用的流量) 生效。
Load(仅对 Linux/Unix-like 机器生效):当系统 load1 超过阈值,且系统当前的并发线程数超过 系统容量时才会触发系统保护。系统容量由系统的 maxQps * minRt 计算得出。设定参考值一般 是 CPU cores * 2.5。
RT:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒。
线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。
入口 QPS:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。
CPU使用率:当单台机器上所有入口流量的 CPU使用率达到阈值即触发系统保护。
3.6 @SentinelResource的使用
在定义了资源点之后,我们可以通过Dashboard来设置限流和降级策略来对资源点进行保护。同时还能 通过@SentinelResource来指定出现异常时的处理策略。 @SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。示例如下面的代码所示,我们在blockHandlerClass类中定义了限流降级方法blockHandler,在fallbackClass类中定义了熔断降级方法fallback:
@Service
@Slf4j
public class OrderServiceImpl3 {
int i = 0;
@SentinelResource(
value = "message",
blockHandlerClass = OrderServiceImpl3BlockHandlerClass.class,
blockHandler = "blockHandler",
fallbackClass = OrderServiceImpl3FallbackClass.class,
fallback = "fallback"
)
public String message() {
i++;
if (i % 3 == 0) {
throw new RuntimeException();
}
return "message4";
}
}
@Slf4j
public class OrderServiceImpl3BlockHandlerClass {
//注意这里必须使用static修饰方法
public static String blockHandler(BlockException ex) {
log.error("{}", ex);
return "接口被限流或者降级了...";
}
}
@Slf4j
public class OrderServiceImpl3FallbackClass {
//注意这里必须使用static修饰方法
public static String fallback(Throwable throwable) {
log.error("{}", throwable);
return "接口发生异常了...";
}
}
3.7 Sentinel规则的持久化
我们前面在前端控制界面设置的限流、熔断、降级的方法默认事存放在内存中的,极不稳定,我们可以将这些配置持久化到文件中。本地文件数据源会定时轮询文件的变更,读取规则。这样我们既可以在本地直接修改文件来更新规则,也可以通过Sentinel控制台推送规则。代码示例如下所示:
//规则持久化
public class FilePersistence implements InitFunc {
@Value("spring.application:name")
private String appcationName;
@Override
public void init() throws Exception {
String ruleDir = System.getProperty("user.home") + "/sentinelrules/"+appcationName;
String flowRulePath = ruleDir + "/flow-rule.json";
String degradeRulePath = ruleDir + "/degrade-rule.json";
String systemRulePath = ruleDir + "/system-rule.json";
String authorityRulePath = ruleDir + "/authority-rule.json";
String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
this.mkdirIfNotExits(ruleDir);
this.createFileIfNotExits(flowRulePath);
this.createFileIfNotExits(degradeRulePath);
this.createFileIfNotExits(systemRulePath);
this.createFileIfNotExits(authorityRulePath);
this.createFileIfNotExits(paramFlowRulePath);
// 流控规则
ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(flowRulePath,flowRuleListParser);
FlowRuleManager.register2Property(flowRuleRDS.getProperty());
WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
flowRulePath,this::encodeJson);
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
// 降级规则
ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(degradeRulePath,degradeRuleListParser);
DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(degradeRulePath,this::encodeJson);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
// 系统规则
ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(systemRulePath,systemRuleListParser);
SystemRuleManager.register2Property(systemRuleRDS.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(systemRulePath,this::encodeJson);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
// 授权规则
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(authorityRulePath,authorityRuleListParser);
AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(authorityRulePath,this::encodeJson);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
// 热点参数规则
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(paramFlowRulePath,paramFlowRuleListParser);
ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(paramFlowRulePath,this::encodeJson);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
}
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source,new TypeReference<List<FlowRule>>() {}
);
private Converter<String, List<DegradeRule>> degradeRuleListParser = source-> JSON.parseObject(source,new TypeReference<List<DegradeRule>>() {}
);
private Converter<String, List<SystemRule>> systemRuleListParser = source ->
JSON.parseObject(source,new TypeReference<List<SystemRule>>() {}
);
private Converter<String, List<AuthorityRule>> authorityRuleListParser =source -> JSON.parseObject(source,new TypeReference<List<AuthorityRule>>() {}
);
private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser =source -> JSON.parseObject(source,new TypeReference<List<ParamFlowRule>>() {}
);
private void mkdirIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
}
private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}
4. Feign整合Sentinel
我们首先在pom.xml中引入sentinel的依赖
<!--sentinel客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
然后在配置文件中开启Feign对Sentinel的支持
feign:
sentinel:
enabled: true
然后创建容错类,容错类要求必须实现被容错的接口,并为每个方法实现容错方案,代码示例如下所示,该容错类保证在我们遇到一些错误的时候,能够返回我们指定的东西,使得我们知道到底是哪里发生了错误:
@Component
@Slf4j
public class ProductServiceFallBack implements ProductService {
@Override
public Product findByPid(Integer pid) {
Product product = new Product();
product.setPid(-1);
return product;
}
}
然后为容器的接口实现指定容错类,我们可以看到该接口上加了注解@FeignClient(),这代表该注解是在是一个远程微服务,我们上面创建的容错类也是对远程微服务中的方法实现的容错。本地服务的容错类的指定,直接在对应的方法上加上@SentinelResource注解即可。
//value用于指定调用nacos下哪个微服务
//fallback用于指定容错类
@FeignClient(value = "service-product", fallback = ProductServiceFallBack.class)
public interface ProductService {
@RequestMapping("/product/{pid}")//指定请求的URI部分
Product findByPid(@PathVariable Integer pid);
}