本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent
微服务系统中,会遇到在线发布,一般的发布更新策略是:启动一个新的,启动成功之后,关闭一个旧的,直到所有的旧的都被关闭。Spring Boot 具有优雅关闭的功能,可以保证请求处理完再关闭,同时会拒绝新的请求。对于这些拒绝的请求,为了保证用户体验不受影响,是需要重试的。
云上部署的微服务,对于同一个服务,同一个请求,很可能不会所有实例都同时异常,例如:
这时候,就需要我们对请求进行无感知的重试。
FeignClient 本身带重试,但是重试策略相对比较简单,同时我们还想使用断路器以及限流器还有线程隔离,resilience4j 就包含这些组件。
Resilience4J 提供了 Retryer 重试器,官方文档地址:https://resilience4j.readme.io/docs/retry
从配置上就能理解其中的原理,但是官方文档配置并不全面,如果想看所有的配置,最好还是通过源码:
RetryConfigurationProperties.java
//重试间隔,默认 500ms
@Nullable
private Duration waitDuration;
//重试间隔时间函数,和 waitDuration 只能设置一个,默认就是 waitDuration
@Nullable
private Class<? extends IntervalBiFunction
引入 resilience4j-spring-boot2 的依赖,就可以通过 Properties 配置的方式去配置 Retryer 等所有 resilience4j 组件,例如:
application.yml
resilience4j.retry:
configs:
default:
## 最大重试次数,包括第一次调用
maxRetryAttempts: 2
## 重试等待时间
waitDuration: 500ms
## 启用随机等待时间,范围是 waitDuration - randomizedWaitFactor*waitDuration ~ waitDuration + randomizedWaitFactor*waitDuration
enableRandomizedWait: true
randomizedWaitFactor: 0.5
test-client1:
## 最大重试次数,包括第一次调用
maxRetryAttempts: 3
## 重试等待时间
waitDuration: 800ms
## 启用随机等待时间,范围是 waitDuration - randomizedWaitFactor*waitDuration ~ waitDuration + randomizedWaitFactor*waitDuration
enableRandomizedWait: true
randomizedWaitFactor: 0.5
这样,我们就可以通过如下代码,获取到配置对应的 Retryer:
@Autowired
RetryRegistry retryRegistry;
//读取 resilience4j.retry.configs.test-client1 下的配置,构建 Retry,这个 Retry 命名为 retry1
Retry retry1 = retryRegistry.retry("retry1", "test-client1");
//读取 resilience4j.retry.configs.default 下的配置,构建 Retry,这个 Retry 命名为 retry1
//不指定配置名称即使用默认的 default 下的配置
Retry retry2 = retryRegistry.retry("retry2");
引入 resilience4j-spring-cloud2 的依赖,就相当于引入了 resilience4j-spring-boot2 的依赖。并在其基础上,针对 spring-cloud-config 的动态刷新 RefreshScope 机制,增加了兼容。
io.github.resilience4j
resilience4j-spring-cloud2
官方提供了粘合 OpenFeign 的依赖库,即 resilience4j-feign
io.github.resilience4j
resilience4j-feign
接下来,我们使用这个依赖,给 OpenFeign 添加重试,首先启用 OpenFeign Client 并指定默认配置:
OpenFeignAutoConfiguration
@EnableFeignClients(value = "com.github.jojotech", defaultConfiguration = DefaultOpenFeignConfiguration.class)
在这个默认配置中,通过覆盖默认的 Feign.Builder 的方式粘合 resilience4j 添加重试:
@Bean
public Feign.Builder resilience4jFeignBuilder(
List feignDecoratorBuilderInterceptors,
FeignDecorators.Builder builder
) {
feignDecoratorBuilderInterceptors.forEach(feignDecoratorBuilderInterceptor -> feignDecoratorBuilderInterceptor.intercept(builder));
return Resilience4jFeign.builder(builder.build());
}
@Bean
public FeignDecorators.Builder defaultBuilder(
Environment environment,
RetryRegistry retryRegistry
) {
String name = environment.getProperty("feign.client.name");
Retry retry = null;
try {
retry = retryRegistry.retry(name, name);
} catch (ConfigurationNotFoundException e) {
retry = retryRegistry.retry(name);
}
//覆盖其中的异常判断,只针对 feign.RetryableException 进行重试,所有需要重试的异常我们都在 DefaultErrorDecoder 以及 Resilience4jFeignClient 中封装成了 RetryableException
retry = Retry.of(name, RetryConfig.from(retry.getRetryConfig()).retryOnException(throwable -> {
return throwable instanceof feign.RetryableException;
}).build());
return FeignDecorators.builder().withRetry(
retry
);
}
页面更新:2024-04-04
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号