常数“阅读超时” Micronaut声明客户的例外

发布于 2025-02-10 06:02:33 字数 1851 浏览 2 评论 0原文

我的Micronaut应用程序中有两个声明性客户端,它们以非阻滞方式(以及其他一些异步方法)收集他们的结果。但是他们偶尔会产生io.micronaut.http.client.exceptions.readtimeoutexception:read timeout。也许就像每个人都会发生的每10或20个请求一样,但我很确定外部服务没有按时出现,因为它们快速且高度可用。

这是我的客户的样子:

@Client("https://foo.bar")
public interface ServiceClient {
    @Post("/data/myData")
    CompletableFuture<List<ServiceResponse>> getDataAsync(@NonNull @Body ServiceRequest request);
}

这就是我在@async方法中称呼它们的方式:

.
.
@Inject
private Provider<ServiceClient> serviceClient;
.
.
@Async
public CompletableFuture<Map<String, Optional<ServiceResponse>>> getDataAsync() {
    ServiceRequest request = buildRequest(); // Some method

    return serviceClient.get().getDataAsync(request)
            .thenApply(this::parseResponse) // Some method
            .exceptionally(
                    ex -> {
                        LOG.error("Failed to connect to service", ex);
                        return someDefaultMap;
                    }
            );
}

在我的主要方法中,我得到了声明性客户端的结果 +其他一些异步方法并将它们组合在一起:

CompletableFuture<Map<String, Optional<ServiceResponse>>> servideFuture = getDataAsync();
CompletableFuture<Object> future2 = anotherAsyncMethod();
CompletableFuture<Object> future3 = andAnotherAsyncMethod();

CompletableFuture.allOf(servideFuture, future2, future3).thenApply(it -> {
    Map<String, Optional<ServiceResponse>> servieRes = servideFuture.join();
    Object future2Res = future2.join();
    Object future3Res = future3.join();

    // Some aggregation
})

我为客户设置了5秒的超时,这已经足够了,因为我要打电话的其他服务非常快。我还为我的I/O执行人使用了缓存类型。这是我的配置:

  http:
    client:
      read-timeout: 5s
  executors:
    io:
      type: cached

I have two declarative Clients in my Micronaut app which I'm collecting their results in a non-blocking manner (alongside with some other async methods). But every once in a while they produce io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout. Maybe like every 10 or 20 requests it happens, for either one of them, but I'm pretty sure that the external services are not timing out, as they are fast and highly available.

Here's how my clients look like:

@Client("https://foo.bar")
public interface ServiceClient {
    @Post("/data/myData")
    CompletableFuture<List<ServiceResponse>> getDataAsync(@NonNull @Body ServiceRequest request);
}

And this is how I call them inside an @Async method:

.
.
@Inject
private Provider<ServiceClient> serviceClient;
.
.
@Async
public CompletableFuture<Map<String, Optional<ServiceResponse>>> getDataAsync() {
    ServiceRequest request = buildRequest(); // Some method

    return serviceClient.get().getDataAsync(request)
            .thenApply(this::parseResponse) // Some method
            .exceptionally(
                    ex -> {
                        LOG.error("Failed to connect to service", ex);
                        return someDefaultMap;
                    }
            );
}

And in my main method, I get the results of my declarative clients + some other async methods and combine them together:

CompletableFuture<Map<String, Optional<ServiceResponse>>> servideFuture = getDataAsync();
CompletableFuture<Object> future2 = anotherAsyncMethod();
CompletableFuture<Object> future3 = andAnotherAsyncMethod();

CompletableFuture.allOf(servideFuture, future2, future3).thenApply(it -> {
    Map<String, Optional<ServiceResponse>> servieRes = servideFuture.join();
    Object future2Res = future2.join();
    Object future3Res = future3.join();

    // Some aggregation
})

I have set a 5 second timeout for my clients, which is more than enough because the other services that I'm calling are pretty fast. I'm also using a cached type for my I/O executor. Here's my configuration:

  http:
    client:
      read-timeout: 5s
  executors:
    io:
      type: cached

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2025-02-17 06:02:33

我想我发现了这个问题。由于我的请求量很高,我的客户似乎正在等待默认事件循环。我仅针对声明的客户定义了一个自定义事件循环,现在运行良好。

micronaut:
  netty:
    event-loops:
      default:
        num-threads: 3
      httpclient:
        num-threads: 5
  http:
    client:
      read-timeout: 5s
      event-loop-group: httpclient

I think I found the problem. It seems like my clients were awaiting in the default event-loop because of the high amount of requests. I defined a custom event-loop just for my declarative clients and now it's running pretty solid.

micronaut:
  netty:
    event-loops:
      default:
        num-threads: 3
      httpclient:
        num-threads: 5
  http:
    client:
      read-timeout: 5s
      event-loop-group: httpclient
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文