Java Grpc:使DNS缓存无效 [英] Java Grpc: invalidate dns cache

查看:176
本文介绍了Java Grpc:使DNS缓存无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个grpc客户端,指向一个解析为2个IP地址的URL.问题是,当一个服务器节点发生故障然后又恢复时,grpc客户端不会选择它,并且所有负载都转移到单个节点上.

我尝试建议更改 networkaddress.cache.ttl 属性,但这没有帮助.我的代码(在Scala中)

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

grpc版本:1.32.1

解决方案

假定DNS一直都返回两个IP(可能被改组),那么问题就出在DNS缓存上.问题在于gRPC的连接正常,因此不会选择重新连接并且不会执行DNS查询.

您应使用 MAX_CONNECTION_AGE 强制客户端偶尔重新连接以重新平衡负载.当客户端与服务器断开连接时,它们会触发新的DNS解析,因此它也可用于查找新地址(尽管重新连接不会等待DNS解析完成).

在Java中,可以通过 NettyServerBuilder.forPort(yourPort).maxConnectionAge(30,TimeUnit.MINUTES)....

您希望使用可以接受的最大年龄.在30分钟左右的时间内,每个客户将每30分钟重新平衡一次.因此,在服务器重新启动15分钟后,该服务器将承担1/4的负载,而在30分钟后,它将承担大约½.

I have a grpc client pointing to a url which resolves to 2 IP addresses. 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.

I tried recommendation to change networkaddress.cache.ttl propetty but it didn't help. My code (in 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

解决方案

Assuming that DNS returns both IPs all the time (probably shuffled), then the problem is not the DNS cache. The problem is that gRPC has a working connection and so won't choose to reconnect and won't perform DNS queries.

You should configure your server with MAX_CONNECTION_AGE to force clients to reconnect occasionally to rebalance the load. 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).

In Java, MAX_CONNECTION_AGE is available via 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. So after 15 minutes of the server restarting that server would have ¼ of the load and after 30 minutes it would have roughly ½.

这篇关于Java Grpc:使DNS缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆