Micronaut 使用 HttpClient 读取超时

Pau*_*ulB 5 micronaut micronaut-client micronaut-rest

我正在努力使用Micronaut HTTPClient多次调用第三方 REST 服务,但没有收到io.micronaut.http.client.exceptions.ReadTimeoutException

要消除第三方依赖性,可以使用调用其自己的服务的简单 Micronaut 应用程序来重现该问题。

控制器示例:

@Controller("/")
public class TestController {
      
    @Inject
    private TestClient client;

    @Get("service")
    String service() {
        return "Hello World Service";
    }
    @Get("mproxy")
    String multiproxy() {
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<20;i++){
            sb.append(client.getService());
        }
        return sb.toString();
    }
    @Get("proxy")
    String proxy() {
        return client.getService();
    }  
}
Run Code Online (Sandbox Code Playgroud)

测试客户端:

@Client("http://localhost:8080")
public interface TestClient {
    
    @Get("/service")
    String getService();

}
Run Code Online (Sandbox Code Playgroud)

使用curl、ab 或postman 直接调用/service 端点不会产生错误。

调用 /mproxy 端点将引发异常

ERROR i.m.r.intercept.RecoveryInterceptor - Type [clienttest.TestClient$Intercepted] executed with error: Read Timeout
io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
        at io.micronaut.http.client.exceptions.ReadTimeoutException.<clinit>(ReadTimeoutException.java:26)
        at io.micronaut.http.client.netty.DefaultHttpClient$12.exceptionCaught(DefaultHttpClient.java:2316)
        at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:302)
        at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:281)
        at io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:273)
        at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireExceptionCaught(CombinedChannelDuplexHandler.java:424)
        at io.netty.channel.ChannelHandlerAdapter.exceptionCaught(ChannelHandlerAdapter.java:92)
        at io.netty.channel.CombinedChannelDuplexHandler$1.fireExceptionCaught(CombinedChannelDuplexHandler.java:145)
        at io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught(ChannelInboundHandlerAdapter.java:143)
        at io.netty.channel.CombinedChannelDuplexHandler.exceptionCaught(CombinedChannelDuplexHandler.java:231)
        at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:302)
        at io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:281)
        at io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:273)
        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:504)
        at io.netty.handler.timeout.IdleStateHandler$AbstractIdleTask.run(IdleStateHandler.java:476)
        at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
        at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        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:831)
Run Code Online (Sandbox Code Playgroud)

或者,如果通过 ab 测试 /proxy 端点,也会引发相同的异常

ab -c 5 -n 200 本地主机:8080/代理

或通过与邮递员的多次通话。

这是针对 micronaut 版本 2.5.5,具有绝对普通的模板应用程序,在 application.yml 中没有指定连接池或超时。

它似乎在 4 个连接/客户端后出错,但更改连接池和超时似乎不会改变结果。我是否缺少一些客户端配置?

Shï*_*màr 1

如果这不会引发异常,那么我不知道会发生什么。

这是由于blocking使用Netty's event loop.

这里的代码连续发出 20 次阻塞请求,导致机器崩溃。我不知道来自客户端的数据是什么,但我绝不会建议以这种方式进行操作。

 for(int i=0;i<20;i++){
        sb.append(client.getService());
    }
Run Code Online (Sandbox Code Playgroud)

关键信息:don't block the event loop

要解决这个问题,您可以做的就是提出您的要求Asynchronous。为此,请使用RxJava. RxJava 允许您以异步方式执行操作。它为您提供了一些非常有用的可观察量和运算符。

唯一的其他方法:在另一个线程上运行此运算符,这样主线程就不会被阻塞,但这可能不会非常有效地工作,并且仍然会导致问题。

要开始使用 RxJava,请点击链接:https://factoryhr.medium.com/understanding-java-rxjava-for-beginners-5eacb8de12ca

Micronaut 教程反应:https://piotrminkowski.com/2019/11/12/micronaut-tutorial-reactive/