简体   繁体   English

HttpClients PoolingHttpClientConnectionManager和DNS缓存

[英]HttpClients PoolingHttpClientConnectionManager and DNS caching

So I seem to be having an issue related to the caching of DNS host name resolution when using PoolingHttpClientConnectionManager. 因此,在使用PoolingHttpClientConnectionManager时,我似乎遇到了与DNS主机名解析缓存相关的问题。 I have an api that makes calls out to an outside service. 我有一个api,可以拨打外面的服务电话。 This service uses Akamai edge caching so it's possible for the ip address to change. 此服务使用Akamai边缘缓存,因此可以更改IP地址。 This api is under heavy load. 这个api负载很重。 I have 100 connections max set for the pool size with an idle timeout thread closing idle connections after 30 seconds being idle. 我为池大小设置了100个最大连接,空闲超时线程在空闲30秒后关闭空闲连接。 But with the heavy traffic, I don't think connections are ever idle. 但由于交通拥挤,我不认为连接是空闲的。 So it seems that if the IP address changes for this services host, my app never picks up that change since all connections in the pool are holding on to the old DNS resolved ip address. 因此,如果此服务主机的IP地址发生更改,我的应用程序似乎永远不会接收该更改,因为池中的所有连接都保留了旧的DNS解析的IP地址。

I've tested this hypothesis locally by changing my /etc/hosts file on my mac (and flushing the os cache). 我已经通过更改我的mac上的/ etc / hosts文件(并刷新操作系统缓存)在本地测试了这个假设。 As long as there is steady traffic against my API app, it never picks up the change even with a TTL of 5 seconds set in java.security. 只要我的API应用程序有稳定的流量,即使在java.security中设置了5秒的TTL,它也永远不会接收到更改。 I let it run for 20 minutes after changing the hostname to point to an invalid ip address that would cause calls to fail, and it never failed. 我让它在更改主机名后运行20分钟,指向一个无效的IP地址,这会导致调用失败,并且它永远不会失败。 I stopped the JMeter traffic I was sending it, waiting about 30 seconds and hit it manually, and the call failed, telling me it picked up the change. 我停止了发送它的JMeter流量,等待大约30秒并手动点击它,并且呼叫失败,告诉我它接收了更改。

Any ideas? 有任何想法吗? Do I need to stop using the connection pool? 我是否需要停止使用连接池? Or write my own DnsResolver (not sure how it should work)? 或者写我自己的DnsResolver(不确定它应该如何工作)? How can I ensure that the connections in my connection pool will update if the DNS entry changes for this service? 如果此条目的DNS条目发生更改,如何确保连接池中的连接将更新?

So I somehow overlooked a constructor for PoolingHttpClientConnectionManager that takes a TimeToLive value. 所以我在某种程度上忽略了PoolingHttpClientConnectionManager的一个构造函数,它接受一个TimeToLive值。 This seems to set an expiration time for each created connection. 这似乎为每个创建的连接设置了到期时间。 I tested it out and it definitely picks up the change in my local hosts file after this TTL value. 我测试了它,它确实在我的本地主机文件中获取此TTL值之后的更改。 I just don't know if this is the right way to solve my problem or not. 我只是不知道这是否是解决问题的正确方法。 If I don't hear about other options, I will assume this is the correct answer to my question. 如果我没有听到其他选项,我会认为这是我的问题的正确答案。

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

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