问题描述
当使用Java中的gRPC时,我可以缓存存根(客户端)并在多线程环境中调用它们,还是通道线程安全且可以安全地缓存?
When using gRPC from Java, can I cache stubs (clients) and call them in a multi-threaded environment or are the channels thread-safe and can be safely cached?
如果网络中断,我应该重新创建频道还是足够聪明才能重新连接?我无法在上找到相关信息
If there is a network outage, should I recreate the channel or it is smart enough to reconnect? I couldn't find relevant info on http://www.grpc.io/docs/
谢谢
推荐答案
回答第一个问题:
频道是线程安全的; io.grpc.Channel
标有 @ThreadSafe
注释。存根也是线程安全的,这就是重新配置创建新存根的原因。
Channels are thread safe; io.grpc.Channel
is marked with @ThreadSafe
annotation. Stubs are also thread-safe, which is why reconfiguration creates a new stub.
回答第二个问题:
如果存在网络中断,则无需重新创建通道。该频道将重新连接指数退避,大致如 doc所述。 Java不会100%符合该算法,因为它不会在以后的重试中增加连接超时。 (不要与实现的指数退避相混淆。)
If there is a network outage, you don't need to recreate the channel. The channel will reconnect with exponential backoff, roughly as described by the connection backoff doc. Java does not 100% conform to that algorithm, because it doesn't increase connection timeouts in later retries. (Not to be confused with the exponential backoff, which is implemented.)
作为一个小小的问题,Netty当前(v0.9.0)的DNS解析发生在正在创建频道,以后不会重新执行。下一个版本不应该有这个问题。
As a small "gotcha," DNS resolution with Netty currently (v0.9.0) happens when the channel is being created, and will not be re-performed later. The next version should not have this issue.
这篇关于gRPC中的通道/存根是否是线程安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!