【Java】DNS缓存

在这里插入图片描述

1.概述

看es代码的时候,刚刚看到主启动类,然后第一行

/**
     * Main entry point for starting elasticsearch
     */
    public static void main(final String[] args) throws Exception {
    
    
        overrideDnsCachePolicyProperties();
       。。。。。
    }

方法如下,发现第一行就是dns缓存相关内容的清理


    private static void overrideDnsCachePolicyProperties() {
    
    
        /**
         * networkaddress.cache.ttl
         * #参数意义:设置jdk解析域名成功的缓存时间。
         * 其他:设置缓存时间,单位秒
         * 默认值:如果开启了-Djava.security.manager,默认是-1,如果未开启,默认是30s
         *
         * networkaddress.cache.negative.ttl=10
         * #参数意义:设置jdk解析域名失败的缓存时间。
         * -1:永久缓存
         * 0:不缓存
         * 其他:设置缓存时间,单位秒
         */
        for (final String property : new String[] {
    
    "networkaddress.cache.ttl", "networkaddress.cache.negative.ttl" }) {
    
    
            final String overrideProperty = "es." + property;
            final String overrideValue = System.getProperty(overrideProperty);
            if (overrideValue != null) {
    
    
                try {
    
    
                    // round-trip the property to an integer and back to a string to ensure that it parses properly
                    Security.setProperty(property, Integer.toString(Integer.valueOf(overrideValue)));
                } catch (final NumberFormatException e) {
    
    
                    throw new IllegalArgumentException(
                            "failed to parse [" + overrideProperty + "] with value [" + overrideValue + "]", e);
                }
            }
        }
    }

N.扩展阅读

DNS缓存那些事

记一次JAVA域名解析的故障

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/109637982