最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jvm添加自定義dns實現(xiàn)過程示例

 更新時間:2023年08月31日 10:03:36   作者:supermassive  
這篇文章主要為大家介紹了jvm添加自定義dns實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

引言

有一個常見的場景, 我們在開發(fā)過程中,要配置很多的本地host,以實現(xiàn)測試環(huán)境一些資源的訪問,那么其它人參與進來開發(fā)的話,也得在自己電腦上配置,這樣既麻煩,又容易出錯。那么能不能把這些配置,寫到project中去實現(xiàn)呢, 這樣每個人本地的/etc/hosts文件中會很干凈,可以隨時clone啟動調試而不需任何配置。答案是肯定的,那么接下來我們就使用java反射的方式來實現(xiàn)。

操作對象 java.net.InetAddress

我們主要操作的對象就是java.net.InetAddress,可以從源碼中看到, 該類中有兩個核心的變量cache和expirySet。 

// mapping from host name to Addresses - either NameServiceAddresses (while
    // still being looked-up by NameService(s)) or CachedAddresses when cached
    private static final ConcurrentMap<String, Addresses> cache =
        new ConcurrentHashMap<>();
    // CachedAddresses that have to expire are kept ordered in this NavigableSet
    // which is scanned on each access
    private static final NavigableSet<CachedAddresses> expirySet =
        new ConcurrentSkipListSet<>();

這里會有個疑問, 為啥還有個expirySet,這個問題也可以通過源碼得到解決,即為了刪除失效的條目。

解析

具體含義可以通過閱讀注釋進行理解。

// remove expired addresses from cache - expirySet keeps them ordered
        // by expiry time so we only need to iterate the prefix of the NavigableSet...
        long now = System.nanoTime();
        for (CachedAddresses caddrs : expirySet) {
            // compare difference of time instants rather than
            // time instants directly, to avoid possible overflow.
            // (see System.nanoTime() recommendations...)
            if ((caddrs.expiryTime - now) < 0L) {
                // ConcurrentSkipListSet uses weakly consistent iterator,
                // so removing while iterating is OK...
                if (expirySet.remove(caddrs)) {
                    // ... remove from cache
                    cache.remove(caddrs.host, caddrs);
                }
            } else {
                // we encountered 1st element that expires in future
                break;
            }
        }

如何實現(xiàn)反射添加dns條目

接下來就是如何來實現(xiàn)反射添加dns條目了,本例中基于java17實現(xiàn),其它版本會有相應的變化。

Class<?> cachedAddresses_Class = Class.forName("java.net.InetAddress$CachedAddresses");
        Constructor<?> constructor = cachedAddresses_Class.getDeclaredConstructors()[0];
        constructor.setAccessible(true);
        Object o = constructor.newInstance(host, toInetAddressArray(host, ip), Long.MAX_VALUE);
        Field cacheField = InetAddress.class.getDeclaredField("cache");
        cacheField.setAccessible(true);
        ConcurrentMap<String, Object> cm = (ConcurrentMap<String, Object>) cacheField.get(null);
        cm.put(host, o);
        Field expirySetField = InetAddress.class.getDeclaredField("expirySet");
        expirySetField.setAccessible(true);
        ConcurrentSkipListSet<Object> cs = (ConcurrentSkipListSet<Object>) expirySetField.get(null);
        cs.add(o);

這樣的話, 就可以自己封裝一下,比如dns條目都寫在一個文件中, 編譯打包的時候, 按profile配置決定是否加載。

以上就是jvm添加自定義dns實現(xiàn)過程示例的詳細內容,更多關于jvm添加自定義dns的資料請關注腳本之家其它相關文章!

相關文章

最新評論

鱼台县| 敖汉旗| 海门市| 安宁市| 萨嘎县| 浮山县| 崇州市| 喀喇沁旗| 娄烦县| 武胜县| 罗田县| 尖扎县| 太仓市| 鲁山县| 雷波县| 宣汉县| 玉田县| 黔南| 溧水县| 连江县| 台州市| 武义县| 铁岭市| 阜平县| 曲水县| 田阳县| 沭阳县| 环江| 五原县| 道真| 通海县| 临澧县| 绍兴县| 威信县| 招远市| 裕民县| 扎赉特旗| 梅河口市| 白银市| 忻城县| 沙湾县|