我在C#中使用gRPC客户端,并使用了长期存在的双工流。但是,TCP连接有时会关闭,因此我想在客户端中使用keepalive。服务器(用Go编写)已经为Keepalive正确配置,并且已经用Go编写的客户端进行了测试。
我使用以下代码将Keepalive设置为5分钟,并启用跟踪以查看所有传入/传出字节。
Environment.SetEnvironmentVariable("GRPC_TRACE", "tcp,channel,http,secure_endpoint");
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");
var callCredentials = CallCredentials.FromInterceptor(Interceptor());
var roots = Encoding.UTF8.GetString(Resources.roots);
Channel = new Channel(address, ChannelCredentials.Create(new SslCredentials(roots), callCredentials), new[]
{
new ChannelOption("grpc.keepalive_time_ms", 5 * 60 * 1000), // 5 minutes
});
await Channel.ConnectAsync(DateTime.UtcNow.AddSeconds(5));
Run Code Online (Sandbox Code Playgroud)
但是,在日志中5分钟没有发送字节,并且连接已关闭,因为在流空闲一段时间后,我无法再通过同一流发送/接收消息。
如何正确启用Keepalive?
我正在研究Netty应用程序.我想在不同的端口上运行多个服务器,没有(阻塞)就无法运行closeFuture().sync().
我ServerManager使用以下代码在我的类中启动服务器:
gpcmServer = new GpcmServer(port);
gpspServer = new GpspServer(port);
Run Code Online (Sandbox Code Playgroud)
在这些类中,我按如下方式启动服务器:
public GpspServer(int port) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// Add the server handler and its decoder
ch.pipeline().addLast(new GpspDecoder(), new GpspServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
bindFuture = …Run Code Online (Sandbox Code Playgroud)