关于Grpc中,ManagedChannel.shutdown(客户端关闭)的问题

weixin_38060006 2016-07-07 04:44:55
 开发四年只会写业务代码,分布式高并发都不会还做程序员?->>>    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) 2:ManagedChannel.shutdown()方法返回值为什么是:ManagedChannel 3:channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);为什么要这样,为什么要等一段时间再shutdown
...全文
603 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

473

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧