达到 api 端点时实施 "retries" 的最佳方式?
Best way to implement "retries" when hitting api endpoint?
我正在使用 ApacheHttpClient
我有一个 Java 方法(在 Java 微服务中)向外部端点(端点)发出 Http POST 请求我不拥有)。一切通常都运行良好,但有时端点出现故障。代码看起来像这样(简化):
private HttpResponseData postRequest(String url) throws Exception {
HttpResponseData response = null;
try (InputStream key = MyAPICaller.class.getResourceAsStream(keyPath)) {
MyAPICaller.initializeApiClient(Username, PassString, key);
int attempts = REQUEST_RETRY_COUNT; // starts at 3
while (attempts-- > 0) {
try {
response = MyAPICaller.getInstance().post(url);
break;
} catch (Exception e) {
log.error("Post Request to {} failed. Retries remaining {}", url, attempts);
Thread.sleep(REQUEST_RETRY_DELAY * 1000);
}
}
if (response == null)
throw new Exception("Post request retries exceeded. Unable to complete request.");
}
return response;
}
我没有写原始代码,但正如你所看到的,它看起来像是在发出请求,而 REQUEST_RETRY_COUNT 大于 0 (看起来总是如此),它将尝试将 post 转换为 url。貌似那里不知为何有个断点,所以跳进try
块后,会一直断,没有重试机制。
Java 中是否有通用设计模式来实现重试模式以命中外部端点?我知道在 Java 脚本中你可以使用带有 returns 承诺的 Fetch API,是否有与 Java 类似的东西?
像 GCP 和 AWS 这样的云平台通常都有自己的重试策略,这应该是首选方法。
如果您想使用自己的重试策略,指数回退可能是一个很好的起点。
它可以是基于注释的,您可以在其中注释您的客户端方法。例如,
您将 API 方法注释如下:
@Retry(maxTries = 3, retryOnExceptions = {RpcException.class})
public UserInfo getUserInfo(String userId);
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Retry {
int maxTries() default 0;
/**
* Attempt retry if one of the following exceptions is thrown.
* @return array of applicable exceptions
*/
Class<? extends Throwable> [] retryOnExceptions() default {Throwable.class};
}
方法拦截器可以实现如下:
public class RetryMethodInterceptor implements MethodInterceptor {
private static final Logger logger = Logger.getLogger(RetryMethodInterceptor.class.getName());
@Override
public Object invoke(MethodInvocation methodInvocator) throws Throwable {
Retry retryAnnotation = methodInvocator.getMethod().getAnnotation(Retry.class);
Set<Class<? extends Throwable>> retriableExceptions =
Sets.newHashSet(retryAnnotation.retryOnExceptions());
String className = methodInvocator.getThis().getClass().getCanonicalName();
String methodName = methodInvocator.getMethod().getName();
int tryCount = 0;
while (true) {
try {
return methodInvocator.proceed();
} catch (Throwable ex) {
tryCount++;
boolean isExceptionInAllowedList = isRetriableException(retriableExceptions, ex.getClass());
if (!isExceptionInAllowedList) {
System.out.println(String.format(
"Exception not in retry list for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
throw ex;
} else if (isExceptionInAllowedList && tryCount > retryAnnotation.maxTries()) {
System.out.println(String
.format(
"Exhausted retries, rethrowing exception for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
throw ex;
}
System.out.println(String.format("Retrying for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
}
}
}
private boolean isRetriableException(Set<Class<? extends Throwable>> allowedExceptions,
Class<? extends Throwable> caughtException) {
for (Class<? extends Throwable> look : allowedExceptions) {
// Only compare the class names we do not want to compare superclass so Class#isAssignableFrom
// can't be used.
if (caughtException.getCanonicalName().equalsIgnoreCase(look.getCanonicalName())) {
return true;
}
}
return false;
}
}
我正在使用 ApacheHttpClient
我有一个 Java 方法(在 Java 微服务中)向外部端点(端点)发出 Http POST 请求我不拥有)。一切通常都运行良好,但有时端点出现故障。代码看起来像这样(简化):
private HttpResponseData postRequest(String url) throws Exception {
HttpResponseData response = null;
try (InputStream key = MyAPICaller.class.getResourceAsStream(keyPath)) {
MyAPICaller.initializeApiClient(Username, PassString, key);
int attempts = REQUEST_RETRY_COUNT; // starts at 3
while (attempts-- > 0) {
try {
response = MyAPICaller.getInstance().post(url);
break;
} catch (Exception e) {
log.error("Post Request to {} failed. Retries remaining {}", url, attempts);
Thread.sleep(REQUEST_RETRY_DELAY * 1000);
}
}
if (response == null)
throw new Exception("Post request retries exceeded. Unable to complete request.");
}
return response;
}
我没有写原始代码,但正如你所看到的,它看起来像是在发出请求,而 REQUEST_RETRY_COUNT 大于 0 (看起来总是如此),它将尝试将 post 转换为 url。貌似那里不知为何有个断点,所以跳进try
块后,会一直断,没有重试机制。
Java 中是否有通用设计模式来实现重试模式以命中外部端点?我知道在 Java 脚本中你可以使用带有 returns 承诺的 Fetch API,是否有与 Java 类似的东西?
像 GCP 和 AWS 这样的云平台通常都有自己的重试策略,这应该是首选方法。
如果您想使用自己的重试策略,指数回退可能是一个很好的起点。
它可以是基于注释的,您可以在其中注释您的客户端方法。例如, 您将 API 方法注释如下:
@Retry(maxTries = 3, retryOnExceptions = {RpcException.class}) public UserInfo getUserInfo(String userId);
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Retry {
int maxTries() default 0;
/**
* Attempt retry if one of the following exceptions is thrown.
* @return array of applicable exceptions
*/
Class<? extends Throwable> [] retryOnExceptions() default {Throwable.class};
}
方法拦截器可以实现如下:
public class RetryMethodInterceptor implements MethodInterceptor {
private static final Logger logger = Logger.getLogger(RetryMethodInterceptor.class.getName());
@Override
public Object invoke(MethodInvocation methodInvocator) throws Throwable {
Retry retryAnnotation = methodInvocator.getMethod().getAnnotation(Retry.class);
Set<Class<? extends Throwable>> retriableExceptions =
Sets.newHashSet(retryAnnotation.retryOnExceptions());
String className = methodInvocator.getThis().getClass().getCanonicalName();
String methodName = methodInvocator.getMethod().getName();
int tryCount = 0;
while (true) {
try {
return methodInvocator.proceed();
} catch (Throwable ex) {
tryCount++;
boolean isExceptionInAllowedList = isRetriableException(retriableExceptions, ex.getClass());
if (!isExceptionInAllowedList) {
System.out.println(String.format(
"Exception not in retry list for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
throw ex;
} else if (isExceptionInAllowedList && tryCount > retryAnnotation.maxTries()) {
System.out.println(String
.format(
"Exhausted retries, rethrowing exception for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
throw ex;
}
System.out.println(String.format("Retrying for class: %s - method: %s - retry count: %s",
className, methodName, tryCount));
}
}
}
private boolean isRetriableException(Set<Class<? extends Throwable>> allowedExceptions,
Class<? extends Throwable> caughtException) {
for (Class<? extends Throwable> look : allowedExceptions) {
// Only compare the class names we do not want to compare superclass so Class#isAssignableFrom
// can't be used.
if (caughtException.getCanonicalName().equalsIgnoreCase(look.getCanonicalName())) {
return true;
}
}
return false;
}
}