netty - Java netty/okhttp gRPC 客户端在尝试连接时抛出间歇性 UNAVAILABLE:io 异常
问题描述
我正在尝试使用 java GRPC 客户端(netty 和 okhttp)连接到服务器。但是他们俩都间歇性地抛出 UNAVAILABLE: io 异常。
发生是随机的,我们不知道是什么导致了这种行为。我们发现请求没有到达 go 服务器。
用于连接的阻塞存根类似于:
public SomeServiceGrpc.SomeServiceBlockingStub getBlockingStub() {
ManagedChannel channel = OkHttpChannelBuilder
.forAddress(configuration.getHost(), configuration.getPort())
.useTransportSecurity()
.keepAliveTime(1, TimeUnit.MINUTES)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.intercept(deadlineInterceptor)
.build();
return SomeServiceGrpc.newBlockingStub(channel);
}
这些是用于 okhttp 和 protobuf 的库:
compile 'io.grpc:grpc-okhttp:1.21.0'
compile 'io.grpc:grpc-protobuf:1.20.0'
compile 'io.grpc:grpc-stub:1.20.0'
这些是用于 netty 和 protobuf 的库:
compile group: 'io.grpc', name: 'grpc-netty', version: '1.22.1'
compile group: 'io.netty', name: 'netty-handler', version: '4.1.35.Final'
compile group: 'io.netty', name: 'netty-tcnative-boringssl-static', version: '2.0.25.Final'
compile 'io.grpc:grpc-protobuf:1.20.0'
compile 'io.grpc:grpc-stub:1.20.0'
任何帮助或领导表示赞赏。
解决方案
通过在客户端和服务器端将 keepAliveWithoutCalls 添加为 true 来解决它。
public SomeServiceGrpc.SomeServiceBlockingStub getBlockingStub() {
ManagedChannel channel = OkHttpChannelBuilder
.forAddress(configuration.getHost(), configuration.getPort())
.useTransportSecurity()
.keepAliveTime(1, TimeUnit.MINUTES)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.intercept(deadlineInterceptor)
.build();
return SomeServiceGrpc.newBlockingStub(channel);
}
推荐阅读
- kotlin - How to remove html tags changed this Unicode of xml 在 kotlin 中更改为 String
- javascript - 如何使用 css 在 chrome mobile 上翻转视频(在全屏模式下)?
- c - 如何在c中实现框模糊?
- ios - SwiftUI 以编程方式选择列表项
- python - 将唯一列值分组为熊猫数据框列中每个唯一值的总和
- node.js - Node js socket io app web在生产中失去连接
- c# - 检测轴何时在某个点上
- python - 在python中将字节转换为十六进制
- javascript - 使用 Jest / Enzyme 在 React 中的功能组件内部测试方法
- sql-server - 某些列的 SQL 动态数据透视