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

发布于 2021-11-28 07:25:10 字数 3144 浏览 935 评论 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文