简体   繁体   English

Java Grpc:使 dns 缓存无效

[英]Java Grpc: invalidate dns cache

I have a grpc client pointing to a url which resolves to 2 IP addresses.我有一个 grpc 客户端指向一个解析为 2 个 IP 地址的 URL。 The problem is when one server node goes down and then gets back, it's not picked by the grpc client and all the load goes to a single node.问题是当一个服务器节点出现故障然后又恢复时,grpc 客户端不会选择它,并且所有负载都转到单个节点。

I tried recommendation to change networkaddress.cache.ttl propetty but it didn't help.我尝试建议更改networkaddress.cache.ttl属性,但没有帮助。 My code (in Scala)我的代码(在 Scala 中)

java.security.Security.setProperty("networkaddress.cache.ttl", "30")
System.setProperty("networkaddress.cache.ttl", "30")
val channel = NettyChannelBuilder.forAddress(host, port).nameResolverFactory(
      new DnsNameResolverProvider).usePlaintext().build
val client = MyServiceGrpc.newStub(channel)

grpc version: 1.32.1 grpc 版本:1.32.1

Assuming that DNS returns both IPs all the time (probably shuffled), then the problem is not the DNS cache.假设 DNS 一直返回两个 IP(可能是 shuffled),那么问题不在于 DNS 缓存。 The problem is that gRPC has a working connection and so won't choose to reconnect and won't perform DNS queries.问题是 gRPC 有一个工作连接,所以不会选择重新连接,也不会执行 DNS 查询。

You should configure your server with MAX_CONNECTION_AGE to force clients to reconnect occasionally to rebalance the load.您应该使用MAX_CONNECTION_AGE配置您的服务器以强制客户端偶尔重新连接以重新平衡负载。 When clients are disconnected from the server they trigger a new DNS resolution, so this can also be used to find new addresses (although reconnections do not wait for the DNS resolution to complete).当客户端与服务器断开连接时,它们会触发新的 DNS 解析,因此这也可用于查找新地址(尽管重新连接不会等待 DNS 解析完成)。

In Java, MAX_CONNECTION_AGE is available via NettyServerBuilder.maxConnectionAge() :在 Java 中, MAX_CONNECTION_AGE可通过NettyServerBuilder.maxConnectionAge()

NettyServerBuilder.forPort(yourPort)
    .maxConnectionAge(30, TimeUnit.MINUTES)
    ....

You want to use as large of age as you can accept.您想使用尽可能大的年龄。 With a time like 30 minutes, then each client will rebalance every 30 minutes.如果时间为 30 分钟,那么每个客户端将每 30 分钟重新平衡一次。 So after 15 minutes of the server restarting that server would have ¼ of the load and after 30 minutes it would have roughly ½.因此,在服务器重新启动 15 分钟后,该服务器将有 1/4 的负载,而在 30 分钟后,它将有大约 ½。

似乎配置负载平衡策略可以完成这项工作:

NettyChannelBuilder.forAddress(host, port).defaultLoadBalancingPolicy("round_robin").usePlaintext().build()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM