【问题标题】:Best way to implement "retries" when hitting api endpoint?访问 api 端点时实现“重试”的最佳方法?
【发布时间】:2020-06-16 04:17:00
【问题描述】:

我正在使用 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(看起来总是如此) ,它会尝试向 url 发送 post。好像因为某种原因那里有一个断点,所以跳转到try块后,总是会断掉,没有重试机制。

Java 中是否有一个通用的设计模式来实现重试模式以访问外部端点?我知道在 Javascript 中你可以使用 Fetch API 并返回一个 promise,Java 有类似的东西吗?

【问题讨论】:

  • 你要达到什么样的 API 端点,是 AWS、GCP 等?
  • 重试已经是ApacheHttpClient的一部分
  • promise 是 JS 代表一个异步操作,promise 本身不会重试失败。

标签: java api http endpoint


【解决方案1】:

像 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;
  }
}

【讨论】:

  • 如果你想看的话,我用已有的代码更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多