应用端 "echo > /dev/tcp/localhost/8877" 时的异常

Exception when "echo > /dev/tcp/localhost/8877" on the app's side

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

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

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.run(SingleThreadEventExecutor.java:989) [grpc-netty-shaded-1.31.1.jar!/:1.31.1]
    at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap.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 意义上),则会出现此异常and/or如果客户端和服务器使用不同的 HTTP 版本。

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

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

我的诀窍是采用众所周知的 wait-for-it.sh

在其中他们使用 nc -z 命令来测试连接,这不像 echo > /dev/tcp/<apps-ip>/8877 那样做 connect(2) 因此在我的应用程序方面没有例外。