Micronaut 读取超时异常 [英] Micronaut ReadTimeoutException

查看:53
本文介绍了Micronaut 读取超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提供 REST API 的 Grails 4 应用程序.其中一个端点有时会因以下异常而失败:

I have a Grails 4 application providing a REST API. One of the endpoints sometimes fail with the following exception:

io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
    at io.micronaut.http.client.exceptions.ReadTimeoutException.<clinit>(ReadTimeoutException.java:26)
    at io.micronaut.http.client.DefaultHttpClient$10.exceptionCaught(DefaultHttpClient.java:1917)
    at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:297)
    at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:276)
    at io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:268)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireExceptionCaught(CombinedChannelDuplexHandler.java:426)
    at io.netty.channel.ChannelHandlerAdapter.exceptionCaught(ChannelHandlerAdapter.java:92)
    at io.netty.channel.CombinedChannelDuplexHandler$1.fireExceptionCaught(CombinedChannelDuplexHandler.java:147)
    at io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught(ChannelInboundHandlerAdapter.java:143)
    at io.netty.channel.CombinedChannelDuplexHandler.exceptionCaught(CombinedChannelDuplexHandler.java:233)
    at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:297)
    at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:276)
    at io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:268)
    at io.netty.handler.timeout.ReadTimeoutHandler.readTimedOut(ReadTimeoutHandler.java:98)
    at io.netty.handler.timeout.ReadTimeoutHandler.channelIdle(ReadTimeoutHandler.java:90)
    at io.netty.handler.timeout.IdleStateHandler$ReaderIdleTimeoutTask.run(IdleStateHandler.java:505)
    at io.netty.handler.timeout.IdleStateHandler$AbstractIdleTask.run(IdleStateHandler.java:477)
    at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
    at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:127)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:405)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:906)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)

端点使用micronaut http客户端调用其他系统.远程系统响应时间过长,导致ReadTimeOutException.

The endpoint uses micronaut http client to call other systems. The remote system takes a very long time to respond, causing the ReadTimeOutException.

这是调用远程服务的代码:

Here is the code calling the remote Service:

class RemoteTaskService implements GrailsConfigurationAware {

    String taskStepperUrl

    // initializes fields from configuration
    void setConfiguration(Config config) {
        taskStepperUrl = config.getProperty('services.stepper')
    }

    private BlockingHttpClient getTaskClient() {
        HttpClient.create(taskStepperUrl.toURL()).toBlocking()
    }

    List<Map> loadTasksByProject(long projectId) {
        try {
            retrieveRemoteList("/api/tasks?projectId=${projectId}")
        } catch(HttpClientResponseException e) {
            log.error("Loading tasks of project failed with status: ${e.status.code}: ${e.message}")
            throw new NotFoundException("No tasks found for project ${projectId}")
        }
    }

    private List<Map> retrieveRemoteList(String path) {
        HttpRequest request = HttpRequest.GET(path)
        HttpResponse<List> response = taskClient.exchange(request, List) as HttpResponse<List>
        response.body()
    }
}

我已经尝试在我的 application.yml 中使用以下配置解决它:

I've tried resolving it using the following configuration in my application.yml:

micronaut:
    server:
        read-timeout: 30

micronaut.http.client.read-timeout: 30

...没有成功.尽管我进行了配置,但在调用端点后 10 秒左右仍会发生超时.

...with no success. Despite my configuration, the timeout still occurs around 10s after calling the endpoint.

如何更改 http rest 客户端的读取超时时间?

How can I change the read timeout duration for the http rest client?

推荐答案

手动创建的http客户端好像没有注入配置值.

It seems that the configuration values are not injected in the manually created http clients.

一个解决方案是在创建时配置HttpClient,设置readTimeout持续时间:

A solution is to configure the HttpClient at creation, setting the readTimeout duration:

private BlockingHttpClient getTaskClient() {
    HttpClientConfiguration configuration = new DefaultHttpClientConfiguration()
    configuration.readTimeout = Duration.ofSeconds(30)
    new DefaultHttpClient(taskStepperUrl.toURL(), configuration).toBlocking()
}

这篇关于Micronaut 读取超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆