关于Grpc中,ManagedChannel.shutdown(客户端关闭)的问题
grpc版本:0.14.1
服务端,客户端均使用的是java
服务端代码:
package com.rpc.login; import io.grpc.Server; import io.grpc.netty.NettyServerBuilder; public class GrpcServer { public static void main(String[] args) throws Exception { Server server = NettyServerBuilder.forPort(8080).addService(LoginServiceGrpc.bindService(new LoginServiceImpl())).build(); server.start(); System.out.println("server startup at 8822"); server.awaitTermination(); } }
package com.rpc.login; import java.util.concurrent.TimeUnit; import com.rpc.login.LoginServiceGrpc.LoginServiceBlockingStub; import io.grpc.ManagedChannel; import io.grpc.netty.NettyChannelBuilder; public class GrpcClient { public static void main(String[] args) throws Exception { ManagedChannel channel = NettyChannelBuilder.forAddress("127.0.0.1", 8080).usePlaintext(true).build(); LoginServiceBlockingStub stub = LoginServiceGrpc.newBlockingStub(channel); LoginResponse resp = stub.login(LoginRequest.newBuilder().setPassword("123456").setUsername("admin").build()); System.out.println(resp.getMsg() + "t" + resp.getCode()); channel.shutdown().awaitTermination(1, TimeUnit.SECONDS); } }
关于channel.shutdown().awaitTermination(1, TimeUnit.SECONDS); 这一段代码的疑问
1:为什么直接写成channel.shutdown(); 则客户端调用报错:
七月 07, 2016 4:42:51 下午 io.grpc.netty.NettyServerTransport notifyTerminated 严重: Transport failed 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.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288) at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1098) at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:112) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:563) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:504) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:418) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:390) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145) at java.lang.Thread.run(Thread.java:745)
3:channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);为什么要这样,为什么要等一段时间再shutdown
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论