grpc java client, go server 错误 io.grpc.StatusRuntimeException: UNAVAILABLE

java client 获取 channel 的代码如下:

    public ManagedChannel getChannel() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress(config.getGrpc().getHost(), config.getGrpc().getPort())
                .disableRetry()
                .idleTimeout(2, TimeUnit.SECONDS)
                .build();
        return channel;
    }

Sping boot 报如下错,没有任何其它提示。

[2019-11-14 15:15:02] [ERROR] o.a.c.c.C.[.[.[.[dispatcherServlet] [DirectJDKLog.java:175] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is io.grpc.StatusRuntimeException: UNAVAILABLE: io exception] with root cause
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
	at sun.nio.ch.SocketDispatcher.read0(Native Method)
	at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
	at sun.nio.ch.IOUtil.read(IOUtil.java:192)
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
	at io.grpc.netty.shaded.io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
	at io.grpc.netty.shaded.io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1108)
	at io.grpc.netty.shaded.io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:345)
	at io.grpc.netty.shaded.io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
	at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:646)
	at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:581)
	at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:498)
	at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:460)
	at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
	at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:748)

go server 不设置 logger 连日志都没有,加了日志输出如下错误:

2019/11/14 15:14:12 rpc server listeing on :50051
log from Warningln
[grpc: Server.Serve failed to create ServerTransport:  connection error: desc = "transport: http2Server.HandleStreams received bogus greeting from client: \"\\x16\\x03\\x01\\x00\\x94\\x01\\x00\\x00\\x90\\x03\\x03\\xd7\\xf9\\x81\\x8cf\\xa8\\xeb\\xcc{\\xb2r\\x19N\""]
log from Warningln
[grpc: Server.Serve failed to create ServerTransport:  connection error: desc = "transport: http2Server.HandleStreams received bogus greeting from client: \"\\x16\\x03\\x01\\x00\\x94\\x01\\x00\\x00\\x90\\x03\\x03\\xadt\\xcb\\xed\\x1c\\xab~˺\\xe5\\x8dM\\x14\""]
log from Warningln
[grpc: Server.Serve failed to create ServerTransport:  connection error: desc = "transport: http2Server.HandleStreams received bogus greeting from client: \"\\x16\\x03\\x01\\x00\\x94\\x01\\x00\\x00\\x90\\x03\\x03Cm:\\x0e\\xf8Ǭ8\\a\\xdb[3\\x95\""]

断点和源码找到输出这个日志的地方: google.golang.org/grpc/internal/transport/http2_server.go
grpc java client, go server 错误 io.grpc.StatusRuntimeException: UNAVAILABLE_第1张图片
原因是 preface 和源码中的 clientPreface 不相等,clientPreface 是这个: []byte(http2.ClientPreface)ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

快去看看 ClientPreface 是什么:
grpc java client, go server 错误 io.grpc.StatusRuntimeException: UNAVAILABLE_第2张图片

stackoverflow 说创建 Channel 时要加 usePlaintext,。好吧~。

你可能感兴趣的:(Go)