這篇文章將為大家詳細講解有關springcloud注冊hostname或者ip的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
SpringCloud簡介
Spring cloud是一個基于Spring Boot實現的服務治理工具包,在微服務架構中用于管理和協調服務的
微服務:就是把一個單體項目,拆分為多個微服務,每個微服務可以獨立技術選型,獨立開發,獨立部署,獨立運維.并且多個服務相互協調,相互配合,最終完成用戶的價值.
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙地簡化了分布式系統基礎設施的開發,如服務發現注冊、配置中心、消息總線、負載均衡、斷路器、數據監控等,都可以用Spring Boot的開發風格做到一鍵啟動和部署

五大重要組件
服務發現——Netflix Eureka
客服端負載均衡——Netflix Ribbon/Feign
服務網關——Netflix Zuul
斷路器——Netflix Hystrix
分布式配置——Spring Cloud Config
默認情況下,Eureka 使用 hostname 進行服務注冊,以及服務信息的顯示, 如果我們相擁 IP 地址的方式,可以在配置文件中配置 eureka.instance.prefer-ip-address=true
idea中ctrl+鼠標左鍵,點擊 eureka.instance.prefer-ip-address=true 進入查看 EurekaInstanceConfigBean 會引入這個屬性
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地址。 默認false,也就是默認不注冊ip.
肯定有地方做了判斷,在 EurekaInstanceConfigBean 搜索preferIpAddress,發現了 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.首先會判斷: this.hostInfo.override 屬性. 此屬性在setIpAddress方法里設置。setIpAddress方法對應的是 eureka.instance.ip-address= 這個配置屬性。
也就是說: eureka.instance.ip-address 和 eureka.instance.prefer-ip-address = true 同時設置是優先取 eureka.instance.ip-address 的配置
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
this.hostInfo.override = true;
}2.preferIpAddress為false返回hostname屬性,為true返回ipAddress屬性 在EurekaInstanceConfigBean搜索hostname 會返現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();
}重點就落在了這個InetUtils.findFirstNonLoopbackHostInfo方法上。
public InetAddress findFirstNonLoopbackAddress() {
InetAddress result = null;
try {
// 記錄網卡最小索引
int lowest = Integer.MAX_VALUE;
// 獲取所有網卡
for (Enumeration<NetworkInterface> nics = NetworkInterface
.getNetworkInterfaces(); nics.hasMoreElements();) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {//判斷網卡是否工作
log.trace("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
}
else if (result != null) {
continue;
}
// @formatter:off
//網卡不忽略列表中
if (!ignoreInterface(ifc.getDisplayName())) {
for (Enumeration<InetAddress> addrs = ifc
.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
if (
address instanceof Inet4Address//是IPV4
&& !address.isLoopbackAddress()//不是回環地址(127.***)
&& isPreferredAddress(address)) {//有推薦網卡,判斷是推薦網卡內的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;
}此方法,會獲 取所有網卡,取ip地址合理、索引值最小且不在忽略列表的網卡的IP地址 作為結果。如果沒有找到合適的IP, 就調用 InetAddress.getLocalHost() 方法。
至此我們來總結下,關于注冊的幾種靈活配置:
Ip注冊: eureka.instance.prefer-ip-address=true
指定IP注冊: eureka.instance.ip-address=
忽略網卡: spring.cloud.inetutils.ignored-interfaces[0]
推薦網卡: spring.cloud.inetutils.preferredNetworks[0]
配置本機的host文件:當InetUtils找不到合適ip時,會調用JDK的 InetAddress.getLocalHost() 。該方法會根據本機的hostname解析出對應的ip。所以可以配置本機的hostname和 /etc/hosts 文件,直接將本機的主機名映射到有效IP地址
關于“springcloud注冊hostname或者ip的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。