应用程序一侧的“ echo> / dev / tcp / localhost / 8877”时发生异常

问题描述

使用bash脚本测试了Java Spring-boot gRPC应用程序的可用性。

脚本通过以下方式确定应用程序的可用性:

echo > /dev/tcp/<apps-ip>/8877 
e.g. echo > /dev/tcp/localhost/8877

但这会导致应用程序打印堆栈跟踪:

[INFO ] [2020-10-06 01:49:35.702] [thread=grpc-default-worker-ELG-1-2] [OpID=] [function=connections] [message=Transport Failed]
io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 0a
    at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2Exception.connectionError(Http2Exception.java:103) ~[grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.readClientPrefaceString(Http2ConnectionHandler.java:306) ~[grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.decode(Http2ConnectionHandler.java:239) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler.decode(Http2ConnectionHandler.java:438) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.BytetoMessageDecoder.decodeRemovalReentryProtection(BytetoMessageDecoder.java:498) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.BytetoMessageDecoder.callDecode(BytetoMessageDecoder.java:437) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.handler.codec.BytetoMessageDecoder.channelRead(BytetoMessageDecoder.java:276) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:792) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:475) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at java.lang.Thread.run(Thread.java:834) [?:?]

如果客户端和服务器之间存在TLS不匹配(从gRPC的角度)和/或客户端和服务器使用不同的HTTP版本,则会出现此异常。

我认为在这种特殊情况下,这是HTTP版本不匹配。

有人可以帮我解决这个问题吗?还是提出另一种测试Spring-boot应用程序可用性的方法

解决方法

众所周知的wait-for-it.sh

他们在其中使用nc -z命令来测试连接,该连接不像connect(2)那样进行echo > /dev/tcp/<apps-ip>/8877,因此我的应用程序一侧也不例外。