您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊(cè)IP怎么選擇”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
服務(wù)器上分別配置了eth0和eth2兩塊網(wǎng)卡,只有eth2的地址可供其它機(jī)器訪問,在這種情況下,服務(wù)注冊(cè)時(shí)Eureka Client會(huì)自動(dòng)選擇eth0作為服務(wù)ip, 導(dǎo)致其它服務(wù)無(wú)法調(diào)用。
由于官方并沒有寫明Eureka Client探測(cè)本機(jī)IP的邏輯,所以只能翻閱源代碼。Eureka Client的源碼在eureka-client模塊下,com.netflix.appinfo包下的InstanceInfo類封裝了本機(jī)信息,其中就包括了IP地址。在
Spring Cloud 環(huán)境下,Eureka Client并沒有自己實(shí)現(xiàn)探測(cè)本機(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; }
通過(guò)源碼可以看出,該工具類會(huì)獲取所有網(wǎng)卡,依次進(jìn)行遍歷,取ip地址合理、索引值最小、已經(jīng)啟動(dòng)且不在忽略列表的網(wǎng)卡的ip地址作為結(jié)果。如果仍然沒有找到合適的IP, 那么就將InetAddress.getLocalHost()做為最后的fallback方案。
A.忽略指定網(wǎng)卡
spring.cloud.inetutils.gnored-interfaces[0]=eth0 # 忽略eth0, 支持正則表達(dá)式
因通過(guò)配置application.properties讓應(yīng)用忽略無(wú)效的網(wǎng)卡。
B.配置host
當(dāng)網(wǎng)查遍歷邏輯都沒有找到合適ip時(shí)會(huì)走JDK的InetAddress.getLocalHost()。該方法會(huì)返回當(dāng)前主機(jī)的hostname, 然后會(huì)根據(jù)hostname解析出對(duì)應(yīng)的ip。因此第二種方案就是配置本機(jī)的hostname和/etc/hosts文件,直接將本機(jī)的主機(jī)名映射到有效IP地址。
C.手工指定IP
# 指定此實(shí)例的ipeureka.instance.ip-address=# 注冊(cè)時(shí)使用ip而不是主機(jī)名eureka.instance.prefer-ip-address=true
D.啟動(dòng)時(shí)指定IP
java -jar -Dspring.cloud.inetutils.preferred-networks=192.168.20.123
E.禁用eth0
查看網(wǎng)卡的連接信息
[root@localhost ~]# nmcli con shNAME UUID TYPE DEVICE System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0
禁用eth0
[root@localhost ~]# ifdown eth0Device 'eth0' successfully disconnected.
啟用eth0
[root@localhost ~]# ifup eth0
在將微服務(wù)放入docker部署在多個(gè)云服務(wù)器上的時(shí)候,發(fā)現(xiàn)eureka里顯示的是機(jī)器名,然后弄了個(gè)spring boot admin監(jiān)控平臺(tái),發(fā)現(xiàn)它就找不到各個(gè)微服務(wù)對(duì)應(yīng)的主機(jī)了。
在網(wǎng)上查得eureka.instance.prefer-ip-address=true,使用這條配置eureka里顯示的就是ip地址了,但是依然不夠的,在監(jiān)控平臺(tái)里面還是連接不上。
還需要配置instance-和hostname才能使客戶端訪問到實(shí)例
效果應(yīng)該是這樣,點(diǎn)擊ip后能訪問到相應(yīng)內(nèi)容
server.port=8666spring.application.name=eureka-server#服務(wù)注冊(cè)中心實(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ù)注冊(cè)中心注冊(cè)自己eureka.client.register-with-eureka=false#是否啟用獲取服務(wù)注冊(cè)信息,因?yàn)檫@是一個(gè)單點(diǎn)的Eureka Server,不需要同步其他的Eureka Server節(jié)點(diǎn)的數(shù)據(jù),故而設(shè)為falseeureka.client.fetch-registry=false#注冊(cè)和查詢都需要依賴該地址,多個(gè)以逗號(hào)分隔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:porteureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}#這里使用spring security對(duì)注冊(cè)中心做一個(gè)基礎(chǔ)的用戶名密碼登錄security.basic.enabled=truesecurity.user.name=adminsecurity.user.password=password
注意到:
eureka.instance.hostname=xxx.xxx.xxx.67eureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}
這里我直接手工指定了ip,而不是通過(guò)${spring.cloud.client.ipAddress}來(lái)獲取本機(jī)的ip,因?yàn)槭褂胐ocker后,發(fā)現(xiàn)獲取的ip是docker0這張網(wǎng)卡上分配的ip,以172.16.xxx.xxx開頭的ip,要使docker綁定外網(wǎng)ip網(wǎng)上也有很多資料,這里先簡(jiǎn)化操作,就直接手工指定ip了哈。。
eureka.client.service-url.defaultZone=http://admin:password@xxx.xxx.xxx.67:8666/eureka/eureka.instance.lease-renewal-interval-in-seconds=5eureka.instance.lease-expiration-duration-in-seconds=10eureka.client.healthcheck.enable=trueeureka.instance.hostname=xxx.xxx.xxx.67#設(shè)置實(shí)例的ID為ip:porteureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}
注意客戶端也要寫上eureka.instance.instance-id和eureka.instance.hostname
這樣在eureka上就顯示的是ip地址了
要使spring boot admin正常工作,還需在spring boot admin上配置
spring.application.name=admin-monitorserver.port=7001#為了安全,以后可以把管理端口設(shè)置一下#management.port=7002#現(xiàn)在測(cè)試環(huán)境就關(guān)閉了身份認(rèn)證,真實(shí)環(huán)境還是需要它的management.security.enabled=false
#關(guān)閉身份認(rèn)證,以免發(fā)生401錯(cuò)誤management.security.enabled=false#admin監(jiān)控配置,連接到服務(wù)端spring.boot.admin.url=http://xxx.xxx.xxx.96:7001#在spring boot admin里以ip的形式注冊(cè)顯示spring.boot.admin.client.prefer-ip=true
這里比較關(guān)鍵的一步就是在客戶端里配置spring.boot.admin.client.prefer-ip=true,這樣spring boot admin就能通過(guò)ip來(lái)訪問各個(gè)微服務(wù)端點(diǎn),然后收集它們的信息,從而來(lái)監(jiān)控各個(gè)微服務(wù)了
“基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊(cè)IP怎么選擇”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。