springcloud注冊(cè)hostname或者ip的那些事
SpringCloud簡(jiǎn)介
Spring cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的服務(wù)治理工具包,在微服務(wù)架構(gòu)中用于管理和協(xié)調(diào)服務(wù)的
微服務(wù):就是把一個(gè)單體項(xiàng)目,拆分為多個(gè)微服務(wù),每個(gè)微服務(wù)可以獨(dú)立技術(shù)選型,獨(dú)立開發(fā),獨(dú)立部署,獨(dú)立運(yùn)維.并且多個(gè)服務(wù)相互協(xié)調(diào),相互配合,最終完成用戶的價(jià)值.
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發(fā)便利性巧妙地簡(jiǎn)化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開發(fā),如服務(wù)發(fā)現(xiàn)注冊(cè)、配置中心、消息總線、負(fù)載均衡、斷路器、數(shù)據(jù)監(jiān)控等,都可以用Spring Boot的開發(fā)風(fēng)格做到一鍵啟動(dòng)和部署
五大重要組件
服務(wù)發(fā)現(xiàn)——Netflix Eureka
客服端負(fù)載均衡——Netflix Ribbon/Feign
服務(wù)網(wǎng)關(guān)——Netflix Zuul
斷路器——Netflix Hystrix
分布式配置——Spring Cloud Config
默認(rèn)情況下,Eureka 使用 hostname 進(jìn)行服務(wù)注冊(cè),以及服務(wù)信息的顯示, 如果我們相擁 IP 地址的方式,可以在配置文件中配置 eureka.instance.prefer-ip-address=true
idea中ctrl+鼠標(biāo)左鍵,點(diǎn)擊 eureka.instance.prefer-ip-address=true
進(jìn)入查看 EurekaInstanceConfigBean
會(huì)引入這個(gè)屬性
EurekaInstanceConfigBean /** * Flag to say that, when guessing a hostname, the IP address of the server should be * used in prference to the hostname reported by the OS. */ private boolean preferIpAddress = false;
preferIpAddress: 首選IP地址。 默認(rèn)false,也就是默認(rèn)不注冊(cè)ip.
肯定有地方做了判斷,在 EurekaInstanceConfigBean 搜索preferIpAddress,發(fā)現(xiàn)了 getHostName 方法, 此方法用于返回得到的hostname或者ip
@Override public String getHostName(boolean refresh) { if (refresh && !this.hostInfo.override) { this.ipAddress = this.hostInfo.getIpAddress(); this.hostname = this.hostInfo.getHostname(); } return this.preferIpAddress ? this.ipAddress : this.hostname; }
1.首先會(huì)判斷: this.hostInfo.override 屬性. 此屬性在setIpAddress方法里設(shè)置。setIpAddress方法對(duì)應(yīng)的是 eureka.instance.ip-address=
這個(gè)配置屬性。
也就是說(shuō): eureka.instance.ip-address 和 eureka.instance.prefer-ip-address = true 同時(shí)設(shè)置是優(yōu)先取 eureka.instance.ip-address 的配置
public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; this.hostInfo.override = true; }
2.preferIpAddress為false返回hostname屬性,為true返回ipAddress屬性 在EurekaInstanceConfigBean搜索hostname 會(huì)返現(xiàn)hostname 與ipAddress 可從hostInfo獲得;hostInfo從inetUtils.findFirstNonLoopbackHostInfo獲得。
public EurekaInstanceConfigBean(InetUtils inetUtils) { this.inetUtils = inetUtils; this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo(); this.ipAddress = this.hostInfo.getIpAddress(); this.hostname = this.hostInfo.getHostname(); }
重點(diǎn)就落在了這個(gè)InetUtils.findFirstNonLoopbackHostInfo方法上。
public InetAddress findFirstNonLoopbackAddress() { InetAddress result = null; try { // 記錄網(wǎng)卡最小索引 int lowest = Integer.MAX_VALUE; // 獲取所有網(wǎng)卡 for (Enumeration<NetworkInterface> nics = NetworkInterface .getNetworkInterfaces(); nics.hasMoreElements();) { NetworkInterface ifc = nics.nextElement(); if (ifc.isUp()) {//判斷網(wǎng)卡是否工作 log.trace("Testing interface: " + ifc.getDisplayName()); if (ifc.getIndex() < lowest || result == null) { lowest = ifc.getIndex(); } else if (result != null) { continue; } // @formatter:off //網(wǎng)卡不忽略列表中 if (!ignoreInterface(ifc.getDisplayName())) { for (Enumeration<InetAddress> addrs = ifc .getInetAddresses(); addrs.hasMoreElements();) { InetAddress address = addrs.nextElement(); if ( address instanceof Inet4Address//是IPV4 && !address.isLoopbackAddress()//不是回環(huán)地址(127.***) && isPreferredAddress(address)) {//有推薦網(wǎng)卡,判斷是推薦網(wǎng)卡內(nèi)的ip log.trace("Found non-loopback interface: " + ifc.getDisplayName()); result = address; } } } // @formatter:on } } } catch (IOException ex) { log.error("Cannot get first non-loopback address", ex); } if (result != null) { return result; } try { //都沒有找到使用JDK的InetAddress獲取 return InetAddress.getLocalHost(); } catch (UnknownHostException e) { log.warn("Unable to retrieve localhost"); } return null; }
此方法,會(huì)獲 取所有網(wǎng)卡,取ip地址合理、索引值最小且不在忽略列表的網(wǎng)卡的IP地址 作為結(jié)果。如果沒有找到合適的IP, 就調(diào)用 InetAddress.getLocalHost()
方法。
至此我們來(lái)總結(jié)下,關(guān)于注冊(cè)的幾種靈活配置:
- Ip注冊(cè): eureka.instance.prefer-ip-address=true
- 指定IP注冊(cè): eureka.instance.ip-address=
- 忽略網(wǎng)卡: spring.cloud.inetutils.ignored-interfaces[0]
- 推薦網(wǎng)卡: spring.cloud.inetutils.preferredNetworks[0]
- 配置本機(jī)的host文件:當(dāng)InetUtils找不到合適ip時(shí),會(huì)調(diào)用JDK的 InetAddress.getLocalHost() 。該方法會(huì)根據(jù)本機(jī)的hostname解析出對(duì)應(yīng)的ip。所以可以配置本機(jī)的hostname和 /etc/hosts 文件,直接將本機(jī)的主機(jī)名映射到有效IP地址
總結(jié)
以上所述是小編給大家介紹的springcloud注冊(cè)hostname或者ip的那些事,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例
這篇文章主要介紹了Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例,涉及構(gòu)造函數(shù)私有化等相關(guān)內(nèi)容,需要的朋友可以了解下。2017-09-09SpringBoot整合RabbitMQ實(shí)現(xiàn)延遲隊(duì)列和死信隊(duì)列
RabbitMQ的死信隊(duì)列用于接收其他隊(duì)列中的“死信”消息,所謂“死信”,是指滿足一定條件而無(wú)法被消費(fèi)者正確處理的消息,死信隊(duì)列通常與RabbitMQ的延遲隊(duì)列一起使用,本文給大家介紹了SpringBoot整合RabbitMQ實(shí)現(xiàn)延遲隊(duì)列和死信隊(duì)列,需要的朋友可以參考下2024-06-06IDEA中Web項(xiàng)目控制臺(tái)亂碼的問(wèn)題及解決方法
這篇文章主要介紹了IDEA中Web項(xiàng)目控制臺(tái)亂碼的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Feign遠(yuǎn)程調(diào)用傳遞對(duì)象參數(shù)并返回自定義分頁(yè)數(shù)據(jù)的過(guò)程解析
這篇文章主要介紹了Feign遠(yuǎn)程調(diào)用傳遞對(duì)象參數(shù)并返回自定義分頁(yè)數(shù)據(jù)的過(guò)程解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03IDEA 項(xiàng)目創(chuàng)建Mapper的xml文件的方法
這篇文章主要介紹了IDEA 項(xiàng)目創(chuàng)建Mapper的xml文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11