Closed shelltea closed 2 years ago
在对SentinelRetrofitDegrade
类进行了以下修改后,可以正常记录异常数,并触发熔断:
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = Objects.requireNonNull(request.tag(Invocation.class)).method();
SentinelDegrade sentinelDegrade = AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(),
SentinelDegrade.class);
if (!needDegrade(sentinelDegrade)) {
return chain.proceed(request);
}
String resourceName = parseResourceName(method);
Entry entry = null;
try {
entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
return chain.proceed(request);
} catch (BlockException e) {
throw new RetrofitBlockException(e);
} catch (IOException | RetrofitException ex) {
Tracer.traceEntry(ex, entry);
throw ex;
} finally {
if (entry != null) {
entry.exit();
}
}
}
5次异常,995次拦截。
通过上述的改动,可以使@Retry
和@SentinelDegrade
组合使用,例如:
@SentinelDegrade(grade = 2, count = 1)
@RetrofitClient(baseUrl = "https://api.com", connectTimeoutMs = 3000,
fallback = Fallback.class, fallbackFactory = FallbackFactory.class)
public interface TexSmartApi {
@Retry(intervalMs = 20)
@POST("api")
Response api(@Body Request request);
}
需要在方法调用地方对Retry Failed的RetrofitException
进行捕获。
可以先进行2次重试,然后抛出Retry Failed的RetrofitException
,一共进行15次请求(minRequestAmount = 5)尝试后触发降级。
厉害了,分析的很深入。我在SentinelRetrofitDegrade
加上Tracer,再发一个版本。
感觉对于非BlockException,都需要trace
2.3.10版本已修复
@chentianming11 升级到了2.3.10,测试可以熔断。👍
}
Tracer
记录的代码: https://github.com/alibaba/Sentinel/blob/master/sentinel-adapter/sentinel-okhttp-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/okhttp/SentinelOkHttpInterceptor.java