日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用

發(fā)布時(shí)間:2025/3/21 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 導(dǎo)讀
  • 官方文檔
  • Eureka Server高可用集群概述
  • 2個(gè)Eureka Server節(jié)點(diǎn)高可用集群搭建步驟
    • Step1. 新建子模塊 microservice-discovery-eureka-ha
    • Step2. 配置hosts
    • Step3. application.yml注冊(cè)兩個(gè)Eureka Server
    • Step4. 啟動(dòng)測(cè)試
    • Step5. 查看服務(wù)注冊(cè)中心
  • 3個(gè)Eureka Server節(jié)點(diǎn)高可用集群搭建步驟
    • 方式一
    • 方式二
  • 將服務(wù)注冊(cè)到Eureka Server集群上及服務(wù)調(diào)用
  • 代碼

導(dǎo)讀

Spring Cloud【Finchley】-02服務(wù)發(fā)現(xiàn)與服務(wù)注冊(cè)Eureka + Eureka Server的搭建中我們搭建了Stand Alone版本的Eureka Server ,本片我們來(lái)搭建2個(gè)Eureka Server節(jié)點(diǎn)組成的集群 和 3個(gè)Eureka Server節(jié)點(diǎn)組成的集群 ,并將微服務(wù)注冊(cè)到Eureka Server集群上。


官方文檔

官方文檔: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#spring-cloud-eureka-server-peer-awareness


Eureka Server高可用集群概述

我們知道Eureka Client會(huì)定時(shí)連接Eureka Server,獲取服務(wù)注冊(cè)表中的信息并緩存到本地,微服務(wù)在消費(fèi)遠(yuǎn)程API的時(shí)候不用每次去Server端查詢,而是使用本地緩存的數(shù)據(jù),這樣的話,一般來(lái)講即使Server宕機(jī),也不會(huì)影響微服務(wù)之間的調(diào)用,但是肯定會(huì)影響Client端服務(wù)的更新,所以生產(chǎn)環(huán)境中,高可用的Eureka Server是必不可少的。

Eureka Server可以通過(guò)運(yùn)行多個(gè)實(shí)例并相互注冊(cè)的方式來(lái)實(shí)現(xiàn)高可用。 Eureka Server實(shí)例會(huì)彼此增量的同步信息,確保所有節(jié)點(diǎn)數(shù)據(jù)一致。

來(lái)看下Stand Alone模式的配置

registerWithEureka: false fetchRegistry: false

所以集群環(huán)境下,需要保持默認(rèn)值,即 true . 詳見(jiàn)源碼 EurekaClientConfigBean


2個(gè)Eureka Server節(jié)點(diǎn)高可用集群搭建步驟

Step1. 新建子模塊 microservice-discovery-eureka-ha

關(guān)鍵pom

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

Step2. 配置hosts

linux : /etc/hosts
windows : C:\Windows\System32\drivers\etc\hosts


Step3. application.yml注冊(cè)兩個(gè)Eureka Server

spring: # 注冊(cè)到eureka上的微服務(wù)名稱(chēng)application: name: microservice-discovery-eureka-ha --- spring:# 指定profiles為peer1profiles: peer1server:port: 8761eureka:instance:# 當(dāng)profiles為peer1,hostname是peer1 ,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)hostname: peer1instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:# 將自己注冊(cè)到peer2這個(gè)Eureka上defaultZone: http://peer2:8762/eureka/--- spring:# 指定profiles為peer2,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)profiles: peer2 # 端口 server:port: 8762 # Eureka eureka:instance:# 當(dāng)profiles為peer2,hostname是peer2hostname: peer2instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:# 將自己注冊(cè)到peer1這個(gè)Eureka上defaultZone: http://peer1:8761/eureka/

application.yml中使用連字符 --- 將配置文件分為三段,第二段和第三段分別為spring. profiles指定了名稱(chēng),該名稱(chēng)表示它所在的那段內(nèi)容應(yīng)該在哪個(gè)profile中。

第一段未指定spring. profiles,即對(duì)所有的profile生效

上述配置文件,指定了hostname ,與上一步配置的hostname保持一致,同時(shí)讓peer1 和 peer2 相互注冊(cè),即peer1注冊(cè)到peer2上


Step4. 啟動(dòng)測(cè)試

方法一: 在STS中配置Run Configurations

主類(lèi)右鍵 Run As — Run Configurations --Spring Boot App

同理 peer2

方法二: 打包成jar,運(yùn)行jar

主類(lèi)右鍵 Run As — Run Configurations – Maven Build ,通過(guò)maven clean package 組合命令,打成jar包

然后通過(guò)

java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

通過(guò)spring.profiles.active指定使用哪個(gè)profile啟動(dòng)。

分別啟動(dòng)peer1 和 peer2 , 首先啟動(dòng)peer1會(huì)報(bào)錯(cuò)java.net.ConnectException: Connection refused: connect, 因?yàn)閜eer2 還未啟動(dòng),等一會(huì)即可。

Step5. 查看服務(wù)注冊(cè)中心

訪問(wèn): http://peer1:8761/

訪問(wèn): http://peer2:8762/


3個(gè)Eureka Server節(jié)點(diǎn)高可用集群搭建步驟

Eureka Server不向ZK必須奇數(shù)個(gè)節(jié)點(diǎn),便于選舉。 Eureka Server對(duì)節(jié)點(diǎn)的個(gè)數(shù)只要2個(gè)以上即可。

步驟同上,主要看下application.yml

方式一

spring: # 注冊(cè)到eureka上的微服務(wù)名稱(chēng)application: name: microservice-discovery-eureka-ha-3nodes eureka:client:serviceUrl:defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/,http://peer3:8789/eureka/ --- spring:# 指定profiles為peer1profiles: peer1server:port: 8787eureka:instance:# 當(dāng)profiles為peer1,hostname是peer1 ,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)hostname: peer1--- spring:# 指定profiles為peer2,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)profiles: peer2 # 端口 server:port: 8788 # Eureka eureka:instance:# 當(dāng)profiles為peer2,hostname是peer2hostname: peer2 --- spring:# 指定profiles為peer3,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)profiles: peer3 # 端口 server:port: 8789 # Eureka eureka:instance:# 當(dāng)profiles為peer3,hostname是peer3hostname: peer3

在公共的配置中指定defaultZone,配置三個(gè) ,peer1 peer2 peer3中不相互注冊(cè)


方式二

spring: # 注冊(cè)到eureka上的微服務(wù)名稱(chēng)application: name: microservice-discovery-eureka-ha-3nodes --- spring:# 指定profiles為peer1profiles: peer1server:port: 8787eureka:instance:# 當(dāng)profiles為peer1,hostname是peer1 ,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)hostname: peer1prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:# 將自己注冊(cè)到peer2這個(gè)Eureka上defaultZone: http://peer2:8788/eureka/,http://peer3:8789/eureka/--- spring:# 指定profiles為peer2,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)profiles: peer2 # 端口 server:port: 8788 # Eureka eureka:instance:# 當(dāng)profiles為peer2,hostname是peer2hostname: peer2prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:# 將自己注冊(cè)到peer1這個(gè)Eureka上defaultZone: http://peer1:8787/eureka/,http://peer3:8789/eureka/--- spring:# 指定profiles為peer3,以jar包的形式啟動(dòng)的時(shí)候通過(guò)spring.profiles.active參數(shù)指定使用哪個(gè)配置文件啟動(dòng)profiles: peer3 # 端口 server:port: 8789 # Eureka eureka:instance:# 當(dāng)profiles為peer3,hostname是peer3hostname: peer3prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:# 將自己注冊(cè)到peer1這個(gè)Eureka上defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/

不在公共的配置中指定defaultZone,在peer1 peer2 peer3中相互注冊(cè)

都可以,均驗(yàn)證通過(guò)。


將服務(wù)注冊(cè)到Eureka Server集群上及服務(wù)調(diào)用

這里我們使用micorservice-provider-user作為演示,修改下defaultZone的地址

啟動(dòng)micorservice-provider-user,

同時(shí)也修改下 消費(fèi)者工程 micorservice-consumer-movie-fegin

啟動(dòng) micorservice-consumer-movie-fegin

查看Eureka Server http://peer1:8761/

訪問(wèn) http://localhost:7901/movie/1

驗(yàn)證通過(guò)


代碼

2個(gè)Eureka Server節(jié)點(diǎn)

https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha

3個(gè)Eureka Server節(jié)點(diǎn)

https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha3

總結(jié)

以上是生活随笔為你收集整理的Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。