httpclient DNS cache問題解決辦法

DNS緩存在操做系統和JDK內部已經實現了吧,通常不須要再由程序介入了。 java

the JVM will cache the dns information for me automatically after the first query緩存

也就是說,httpclient去抓取每一個url時,JVM都會自動cache住 這個url和對應的ip,而且是永遠cache住,除非cache住的內容大於 JVM的限制 ,若是未來這個url(域名)更換了ip,httpclient會首先去JVM的cache裏取,若是取到了。直接根據這個ip去抓取。url

因此每每某個域名更換了IP,抓取結果都是604錯誤。spa

解決辦法:操作系統

networkaddress.cache.ttl (default: -1)
    Specified in java.security to indicate the caching policy for successful 
    name lookups from the name service. The value is specified as as integer 
    to indicate the number of seconds to cache the successful lookup.

    A value of -1 indicates "cache forever".code

因此只需在java代碼裏添加:orm

java.security.Security.setProperty("networkaddress.cache.ttl" , "0");
相關文章
相關標籤/搜索