基于多網(wǎng)卡環(huán)境下Eureka服務注冊IP的選擇問題
多網(wǎng)卡環(huán)境下Eureka服務注冊IP選擇
問題場景
服務器上分別配置了eth0和eth1兩塊網(wǎng)卡,只有eth1的地址可供其它機器訪問,在這種情況下,服務注冊時Eureka Client會自動選擇eth0作為服務ip, 導致其它服務無法調(diào)用。
問題原因
由于官方并沒有寫明Eureka Client探測本機IP的邏輯,所以只能翻閱源代碼。Eureka Client的源碼在eureka-client模塊下,com.netflix.appinfo包下的InstanceInfo類封裝了本機信息,其中就包括了IP地址。在
Spring Cloud 環(huán)境下,Eureka Client并沒有自己實現(xiàn)探測本機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)卡,依次進行遍歷,取ip地址合理、索引值最小、已經(jīng)啟動且不在忽略列表的網(wǎng)卡的ip地址作為結(jié)果。如果仍然沒有找到合適的IP, 那么就將InetAddress.getLocalHost()做為最后的fallback方案。
解決方案
A.忽略指定網(wǎng)卡
spring.cloud.inetutils.gnored-interfaces[0]=eth0 # 忽略eth0, 支持正則表達式
因通過配置application.properties讓應用忽略無效的網(wǎng)卡。
B.配置host
當網(wǎng)查遍歷邏輯都沒有找到合適ip時會走JDK的InetAddress.getLocalHost()。該方法會返回當前主機的hostname, 然后會根據(jù)hostname解析出對應的ip。因此第二種方案就是配置本機的hostname和/etc/hosts文件,直接將本機的主機名映射到有效IP地址。
C.手工指定IP
# 指定此實例的ip eureka.instance.ip-address= # 注冊時使用ip而不是主機名 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注冊服務
在將微服務放入docker部署在多個云服務器上的時候,發(fā)現(xiàn)eureka里顯示的是機器名,然后弄了個spring boot admin監(jiān)控平臺,發(fā)現(xiàn)它就找不到各個微服務對應的主機了。
在網(wǎng)上查得eureka.instance.prefer-ip-address=true,使用這條配置eureka里顯示的就是ip地址了,但是依然不夠的,在監(jiān)控平臺里面還是連接不上。
還需要配置instance-和hostname才能使客戶端訪問到實例
效果應該是這樣,點擊ip后能訪問到相應內(nèi)容

eureka服務端配置
server.port=8666
spring.application.name=eureka-server
#服務注冊中心實例的主機名
eureka.instance.hostname=xxx.xxx.xxx.67
#留存的服務示例低于多少比例進入保護模式
eureka.server.renewal-percent-threshold=0.5
#是否開啟保護模式
eureka.server.enable-self-preservation=true
#是否向服務注冊中心注冊自己
eureka.client.register-with-eureka=false
#是否啟用獲取服務注冊信息,因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節(jié)點的數(shù)據(jù),故而設為false
eureka.client.fetch-registry=false
#注冊和查詢都需要依賴該地址,多個以逗號分隔
eureka.client.serviceUrl.defaultZone=http://admin:password@xxx.xxx.xxx.67:8666/eureka/
#使用ip替代實例名
eureka.instance.prefer-ip-address=true
#設置實例的ID為ip:port
eureka.instance.instance-id=xxx.xxx.xxx.67:${server.port}
#這里使用spring security對注冊中心做一個基礎的用戶名密碼登錄
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}來獲取本機的ip,因為使用docker后,發(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
#設置實例的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服務端配置
spring.application.name=admin-monitor server.port=7001 #為了安全,以后可以把管理端口設置一下 #management.port=7002 #現(xiàn)在測試環(huán)境就關閉了身份認證,真實環(huán)境還是需要它的 management.security.enabled=false
客戶端配置
#關閉身份認證,以免發(fā)生401錯誤 management.security.enabled=false #admin監(jiān)控配置,連接到服務端 spring.boot.admin.url=http://xxx.xxx.xxx.96:7001 #在spring boot admin里以ip的形式注冊顯示 spring.boot.admin.client.prefer-ip=true
這里比較關鍵的一步就是在客戶端里配置spring.boot.admin.client.prefer-ip=true,這樣spring boot admin就能通過ip來訪問各個微服務端點,然后收集它們的信息,從而來監(jiān)控各個微服務了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
這篇文章主要介紹了SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
MyBatis #{}和${} |與數(shù)據(jù)庫連接池使用詳解
本文將為大家說說關于 #{} 和 ${},這個是 MyBatis 在面試中最常問的面試題,以及數(shù)據(jù)庫連接池相關的知識,感興趣的朋友跟隨小編一起看看吧2024-01-01
Java mutable對象和immutable對象的區(qū)別說明
這篇文章主要介紹了Java mutable對象和immutable對象的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
java8 stream的多字段排序?qū)崿F(xiàn)(踩坑)
這篇文章主要介紹了java8 stream的多字段排序?qū)崿F(xiàn)(踩坑),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

