springcloud如何獲取hostname和ip

多個(gè)ip到底用哪個(gè)?

上次說到instanceinfo的hostname,ip,那么這些值是從哪來的呢?

public EurekaInstanceConfigBean(InetUtils inetUtils) {
        this.inetUtils = inetUtils;
        this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();
        this.ipAddress = this.hostInfo.getIpAddress();
        this.hostname = this.hostInfo.getHostname();
    }

可以看到是從InetUtils類獲取的,而這個(gè)類是從InetUtilsProperties獲取數(shù)據(jù)
及從配置文件spring.cloud.inetutils開頭的配置獲取。

你可以設(shè)置忽略的網(wǎng)卡,或者優(yōu)先選擇的網(wǎng)卡,這兩個(gè)設(shè)置都支持正則

        // @formatter:off
                    if (!ignoreInterface(ifc.getDisplayName())) {
                        for (Enumeration<InetAddress> addrs = ifc
                                .getInetAddresses(); addrs.hasMoreElements();) {
                            InetAddress address = addrs.nextElement();
                            if (address instanceof Inet4Address
                                    && !address.isLoopbackAddress()
                                    && isPreferredAddress(address)) {
                                log.trace("Found non-loopback interface: "
                                        + ifc.getDisplayName());
                                result = address;
                            }
                        }
                    }
                    // @formatter:on

如果這兩個(gè)設(shè)置你都不滿意,你可以設(shè)置忽略值為.*,忽略所有網(wǎng)卡

然后用hostname從/etc/hosts中獲取ip,也就是說你要在/etc/hosts設(shè)置你的ip對(duì)應(yīng)的本機(jī)hostname

如果你認(rèn)為這樣就完事了,那就是大錯(cuò)特錯(cuò)。

通過查找InetUtils.findFirstNonLoopbackAddress引用發(fā)現(xiàn),在HostInfoEnvironmentPostProcessor類中,并未使用配置文件的參數(shù),而是直接創(chuàng)建了一個(gè)對(duì)象。

@Override
    public void postProcessEnvironment(ConfigurableEnvironment environment,
            SpringApplication application) {
        InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("spring.cloud.client.hostname", hostInfo.getHostname());
        map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
        MapPropertySource propertySource = new MapPropertySource(
                "springCloudClientHostInfo", map);
        environment.getPropertySources().addLast(propertySource);
    }

    private HostInfo getFirstNonLoopbackHostInfo(ConfigurableEnvironment environment) {
        InetUtilsProperties target = new InetUtilsProperties();
        ConfigurationPropertySources.attach(environment);
        Binder.get(environment).bind(InetUtilsProperties.PREFIX,
                Bindable.ofInstance(target));
        try (InetUtils utils = new InetUtils(target)) {
            return utils.findFirstNonLoopbackHostInfo();
        }
    }

雖然是new了一個(gè)對(duì)象,但是其實(shí)使用了environment設(shè)置了屬性,那么我們有兩種方案。

  1. 在啟動(dòng)參數(shù)中傳入?yún)?shù),比如--spring.cloud.inetutils.ignoredInterfaces=.*
  2. 創(chuàng)建一個(gè)EnvironmentPostProcessor類,并且該類的order要比HostInfoEnvironmentPostProcessor小,及優(yōu)先加載我們定義的
public class InspectEurekaClientIp implements BeanPostProcessor, EnvironmentPostProcessor, Ordered {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Map<String, Object> ignoredInterfaces = Maps.newHashMap();
        ignoredInterfaces.put("spring.cloud.inetutils.ignoredInterfaces", Lists.newArrayList(".*"));
        SystemEnvironmentPropertySource systemEnvironmentPropertySource = new SystemEnvironmentPropertySource("systemEnvironment", ignoredInterfaces);
        environment.getPropertySources().addLast(systemEnvironmentPropertySource);

    }

    @Override
    public int getOrder() {
        return ConfigFileApplicationListener.DEFAULT_ORDER - 2;
    }
}

還要?jiǎng)?chuàng)建META-INF/spring.factories文件

org.springframework.boot.env.EnvironmentPostProcessor=\
上述類名
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評(píng)論 6 342
  • 前言 在微服務(wù)架構(gòu)的系統(tǒng)中,我們通常會(huì)使用輕量級(jí)的消息代理來構(gòu)建一個(gè)共用的消息主題讓系統(tǒng)中所有微服務(wù)實(shí)例都連接上來...
    Chandler_玨瑜閱讀 6,763評(píng)論 2 39
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時(shí)芥藍(lán)閱讀 42,753評(píng)論 11 349
  • 一蓑煙雨涼,半壺濁酒香。 醉夢(mèng)臥不起,雨打掛衣裳。
    唐尼濤閱讀 324評(píng)論 0 1

友情鏈接更多精彩內(nèi)容