最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP的選擇問題

 更新時間:2022年03月09日 11:25:29   作者:取個昵稱好難啊Elaine  
這篇文章主要介紹了基于多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP的選擇問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

多網(wǎng)卡環(huán)境下Eureka服務(wù)注冊IP選擇

問題場景

服務(wù)器上分別配置了eth0和eth1兩塊網(wǎng)卡,只有eth1的地址可供其它機器訪問,在這種情況下,服務(wù)注冊時Eureka Client會自動選擇eth0作為服務(wù)ip, 導(dǎo)致其它服務(wù)無法調(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讓應(yīng)用忽略無效的網(wǎng)卡。

B.配置host

當網(wǎng)查遍歷邏輯都沒有找到合適ip時會走JDK的InetAddress.getLocalHost()。該方法會返回當前主機的hostname, 然后會根據(jù)hostname解析出對應(yīng)的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注冊服務(wù)

在將微服務(wù)放入docker部署在多個云服務(wù)器上的時候,發(fā)現(xiàn)eureka里顯示的是機器名,然后弄了個spring boot admin監(jiān)控平臺,發(fā)現(xiàn)它就找不到各個微服務(wù)對應(yīng)的主機了。

在網(wǎng)上查得eureka.instance.prefer-ip-address=true,使用這條配置eureka里顯示的就是ip地址了,但是依然不夠的,在監(jiān)控平臺里面還是連接不上。

還需要配置instance-和hostname才能使客戶端訪問到實例

效果應(yīng)該是這樣,點擊ip后能訪問到相應(yīng)內(nèi)容

eureka服務(wù)端配置

server.port=8666
spring.application.name=eureka-server
#服務(wù)注冊中心實例的主機名
eureka.instance.hostname=xxx.xxx.xxx.67
#留存的服務(wù)示例低于多少比例進入保護模式
eureka.server.renewal-percent-threshold=0.5
#是否開啟保護模式
eureka.server.enable-self-preservation=true
#是否向服務(wù)注冊中心注冊自己
eureka.client.register-with-eureka=false
#是否啟用獲取服務(wù)注冊信息,因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節(jié)點的數(shù)據(jù),故而設(shè)為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
#設(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}來獲取本機的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
#設(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)閉了身份認證,真實環(huán)境還是需要它的
management.security.enabled=false

客戶端配置

#關(guā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ù)端點,然后收集它們的信息,從而來監(jiān)控各個微服務(wù)了

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

泰安市| 台前县| 上杭县| 广水市| 东港市| 通山县| 吴忠市| 宾川县| 琼中| 库尔勒市| SHOW| 东宁县| 高平市| 宝鸡市| 清原| 长垣县| 西乌珠穆沁旗| 六枝特区| 永顺县| 穆棱市| 玛沁县| 汉阴县| 习水县| 赣榆县| 吐鲁番市| 丹寨县| 竹溪县| 晋州市| 安阳县| 积石山| 青浦区| 泸水县| 康平县| 巴东县| 循化| 锡林浩特市| 恩施市| 保亭| 讷河市| 武鸣县| 天全县|