基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP的選擇問題
多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP選擇
問題場景
服務(wù)器上分別配置了eth0和eth1兩塊網(wǎng)卡,只有eth1的地址可供其它機(jī)器訪問,在這種情況下,服務(wù)注冊時Eureka Client會自動選擇eth0作為服務(wù)ip, 導(dǎo)致其它服務(wù)無法調(diào)用。
問題原因
由于官方并沒有寫明Eureka Client探測本機(jī)IP的邏輯,所以只能翻閱源代碼。Eureka Client的源碼在eureka-client模塊下,com.netflix.appinfo包下的InstanceInfo類封裝了本機(jī)信息,其中就包括了IP地址。在
Spring Cloud 環(huán)境下,Eureka Client并沒有自己實(shí)現(xiàn)探測本機(jī)IP的邏輯,而是交給Spring的InetUtils工具類的findFirstNonLoopbackAddress()方法完成的:
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()) { ? ? ? ? ? ? ? ? ? ? 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())) { // 是否是被忽略的網(wǎng)卡 ? ? ? ? ? ? ? ? ? ? ? ? for (Enumeration<InetAddress> addrs = ifc ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .getInetAddresses(); addrs.hasMoreElements();) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? InetAddress address = addrs.nextElement(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (address instanceof Inet4Address ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? && !address.isLoopbackAddress() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? && !ignoreAddress(address)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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 { ? ? ? ? ? ? return InetAddress.getLocalHost(); // 如果以上邏輯都沒有找到合適的網(wǎng)卡,則使用JDK的InetAddress.getLocalhost() ? ? ? ? } ? ? ? ? catch (UnknownHostException e) { ? ? ? ? ? ? log.warn("Unable to retrieve localhost"); ? ? ? ? }? ? ? ? ? return null; ? ? }
通過源碼可以看出,該工具類會獲取所有網(wǎng)卡,依次進(jìn)行遍歷,取ip地址合理、索引值最小、已經(jīng)啟動且不在忽略列表的網(wǎng)卡的ip地址作為結(jié)果。如果仍然沒有找到合適的IP, 那么就將InetAddress.getLocalHost()做為最后的fallback方案。
解決方案
A.忽略指定網(wǎng)卡
spring.cloud.inetutils.gnored-interfaces[0]=eth0 # 忽略eth0, 支持正則表達(dá)式
因通過配置application.properties讓應(yīng)用忽略無效的網(wǎng)卡。
B.配置host
當(dāng)網(wǎng)查遍歷邏輯都沒有找到合適ip時會走JDK的InetAddress.getLocalHost()。該方法會返回當(dāng)前主機(jī)的hostname, 然后會根據(jù)hostname解析出對應(yīng)的ip。因此第二種方案就是配置本機(jī)的hostname和/etc/hosts文件,直接將本機(jī)的主機(jī)名映射到有效IP地址。
C.手工指定IP
# 指定此實(shí)例的ip eureka.instance.ip-address= # 注冊時使用ip而不是主機(jī)名 eureka.instance.prefer-ip-address=true
D.啟動時指定IP
java -jar -Dspring.cloud.inetutils.preferred-networks=192.168.20.123
E.禁用eth0
查看網(wǎng)卡的連接信息
[root@localhost ~]# nmcli con sh NAME ? ? ? ? UUID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?TYPE ? ? ? ? ? ?DEVICE? System eth0 ?5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ?802-3-ethernet ?eth0 ??
禁用eth0
[root@localhost ~]# ifdown eth0 Device 'eth0' successfully disconnected.
啟用eth0
[root@localhost ~]# ifup eth0
Eureka中使用IP注冊服務(wù)
在將微服務(wù)放入docker部署在多個云服務(wù)器上的時候,發(fā)現(xiàn)eureka里顯示的是機(jī)器名,然后弄了個spring boot admin監(jiān)控平臺,發(fā)現(xiàn)它就找不到各個微服務(wù)對應(yīng)的主機(jī)了。
在網(wǎng)上查得eureka.instance.prefer-ip-address=true,使用這條配置eureka里顯示的就是ip地址了,但是依然不夠的,在監(jiān)控平臺里面還是連接不上。
還需要配置instance-和hostname才能使客戶端訪問到實(shí)例
效果應(yīng)該是這樣,點(diǎn)擊ip后能訪問到相應(yīng)內(nèi)容
eureka服務(wù)端配置
server.port=8666 spring.application.name=eureka-server #服務(wù)注冊中心實(shí)例的主機(jī)名 eureka.instance.hostname=xxx.xxx.xxx.67 #留存的服務(wù)示例低于多少比例進(jìn)入保護(hù)模式 eureka.server.renewal-percent-threshold=0.5 #是否開啟保護(hù)模式 eureka.server.enable-self-preservation=true #是否向服務(wù)注冊中心注冊自己 eureka.client.register-with-eureka=false #是否啟用獲取服務(wù)注冊信息,因?yàn)檫@是一個單點(diǎn)的Eureka Server,不需要同步其他的Eureka Server節(jié)點(diǎn)的數(shù)據(jù),故而設(shè)為false eureka.client.fetch-registry=false #注冊和查詢都需要依賴該地址,多個以逗號分隔 eureka.client.serviceUrl.defaultZone=http://admin:password@xxx.xxx.xxx.67:8666/eureka/ #使用ip替代實(shí)例名 eureka.instance.prefer-ip-address=true #設(shè)置實(shí)例的ID為ip:port eureka.instance.instance-id=xxx.xxx.xxx.67:${server.port} #這里使用spring security對注冊中心做一個基礎(chǔ)的用戶名密碼登錄 security.basic.enabled=true security.user.name=admin security.user.password=password
注意到:
eureka.instance.hostname=xxx.xxx.xxx.67 eureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}
這里我直接手工指定了ip,而不是通過${spring.cloud.client.ipAddress}來獲取本機(jī)的ip,因?yàn)槭褂胐ocker后,發(fā)現(xiàn)獲取的ip是docker0這張網(wǎng)卡上分配的ip,以172.16.xxx.xxx開頭的ip,要使docker綁定外網(wǎng)ip網(wǎng)上也有很多資料,這里先簡化操作,就直接手工指定ip了哈。。
客戶端配置
eureka.client.service-url.defaultZone=http://admin:password@xxx.xxx.xxx.67:8666/eureka/ eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 eureka.client.healthcheck.enable=true eureka.instance.hostname=xxx.xxx.xxx.67 #設(shè)置實(shí)例的ID為ip:port eureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}
注意客戶端也要寫上eureka.instance.instance-id和eureka.instance.hostname
這樣在eureka上就顯示的是ip地址了
要使spring boot admin正常工作,還需在spring boot admin上配置
admin服務(wù)端配置
spring.application.name=admin-monitor server.port=7001 #為了安全,以后可以把管理端口設(shè)置一下 #management.port=7002 #現(xiàn)在測試環(huán)境就關(guān)閉了身份認(rèn)證,真實(shí)環(huán)境還是需要它的 management.security.enabled=false
客戶端配置
#關(guān)閉身份認(rèn)證,以免發(fā)生401錯誤 management.security.enabled=false #admin監(jiān)控配置,連接到服務(wù)端 spring.boot.admin.url=http://xxx.xxx.xxx.96:7001 #在spring boot admin里以ip的形式注冊顯示 spring.boot.admin.client.prefer-ip=true
這里比較關(guān)鍵的一步就是在客戶端里配置spring.boot.admin.client.prefer-ip=true,這樣spring boot admin就能通過ip來訪問各個微服務(wù)端點(diǎn),然后收集它們的信息,從而來監(jiān)控各個微服務(wù)了
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
這篇文章主要介紹了SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08MyBatis #{}和${} |與數(shù)據(jù)庫連接池使用詳解
本文將為大家說說關(guān)于 #{} 和 ${},這個是 MyBatis 在面試中最常問的面試題,以及數(shù)據(jù)庫連接池相關(guān)的知識,感興趣的朋友跟隨小編一起看看吧2024-01-01Java mutable對象和immutable對象的區(qū)別說明
這篇文章主要介紹了Java mutable對象和immutable對象的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java8 stream的多字段排序?qū)崿F(xiàn)(踩坑)
這篇文章主要介紹了java8 stream的多字段排序?qū)崿F(xiàn)(踩坑),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03