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

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

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

引言

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

操作對象 java.net.InetAddress

我們主要操作的對象就是java.net.InetAddress,可以從源碼中看到, 該類中有兩個(gè)核心的變量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<>();

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

解析

具體含義可以通過閱讀注釋進(jìn)行理解。

// 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;
            }
        }

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

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

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條目都寫在一個(gè)文件中, 編譯打包的時(shí)候, 按profile配置決定是否加載。

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

相關(guān)文章

最新評論

张掖市| 永定县| 酉阳| 马龙县| 灵石县| 贵南县| 林西县| 疏勒县| 巧家县| 安义县| 裕民县| 汶川县| 北宁市| 浏阳市| 汉沽区| 东山县| 成都市| 巨野县| 阜城县| 锡林郭勒盟| 达尔| 延边| 宣化县| 科技| 白河县| 宜兰市| 时尚| 定州市| 永清县| 施甸县| 漳州市| 石景山区| 彰化县| 韶关市| 定陶县| 定南县| 舟山市| 格尔木市| 抚松县| 广丰县| 乌兰县|