Spring CloudFinchley】-13 Eureka Server HA高可用 2個/3個節點的搭建及服務注冊調用

      網友投稿 1003 2025-04-01

      文章目錄

      導讀

      官方文檔

      Eureka Server高可用集群概述

      2個Eureka Server節點高可用集群搭建步驟

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

      Step2. 配置hosts

      Step3. application.yml注冊兩個Eureka Server

      Step4. 啟動測試

      Step5. 查看服務注冊中心

      3個Eureka Server節點高可用集群搭建步驟

      方式一

      方式二

      將服務注冊到Eureka Server集群上及服務調用

      代碼

      導讀

      spring Cloud【Finchley】-02服務發現與服務注冊Eureka + Eureka Server的搭建中我們搭建了Stand Alone版本的Eureka Server ,本片我們來搭建2個Eureka Server節點組成的集群 和 3個Eureka Server節點組成的集群 ,并將微服務注冊到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會定時連接Eureka Server,獲取服務注冊表中的信息并緩存到本地,微服務在消費遠程API的時候不用每次去Server端查詢,而是使用本地緩存的數據,這樣的話,一般來講即使Server宕機,也不會影響微服務之間的調用,但是肯定會影響Client端服務的更新,所以生產環境中,高可用的Eureka Server是必不可少的。

      Eureka Server可以通過運行多個實例并相互注冊的方式來實現高可用。 Eureka Server實例會彼此增量的同步信息,確保所有節點數據一致。

      來看下Stand Alone模式的配置

      registerWithEureka: false fetchRegistry: false

      1

      2

      所以集群環境下,需要保持默認值,即 true . 詳見源碼 EurekaClientConfigBean

      2個Eureka Server節點高可用集群搭建步驟

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

      關鍵pom

      org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-actuator

      1

      2

      3

      4

      5

      6

      7

      8

      9

      Step2. 配置hosts

      linux : /etc/hosts

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

      Step3. application.yml注冊兩個Eureka Server

      spring: # 注冊到eureka上的微服務名稱 application: name: microservice-discovery-eureka-ha --- spring: # 指定profiles為peer1 profiles: peer1 server: port: 8761 eureka: instance: # 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 hostname: peer1 instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: # 將自己注冊到peer2這個Eureka上 defaultZone: http://peer2:8762/eureka/ --- spring: # 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer2 # 端口 server: port: 8762 # Eureka eureka: instance: # 當profiles為peer2,hostname是peer2 hostname: peer2 instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: # 將自己注冊到peer1這個Eureka上 defaultZone: http://peer1:8761/eureka/

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      Spring Cloud【Finchley】-13 Eureka Server HA高可用 2個/3個節點的搭建及服務注冊調用

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

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

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

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

      Step4. 啟動測試

      方法一: 在STS中配置Run Configurations

      主類右鍵 Run As — Run Configurations --Spring Boot App

      同理 peer2

      方法二: 打包成jar,運行jar

      主類右鍵 Run As — Run Configurations – Maven Build ,通過maven clean package 組合命令,打成jar包

      然后通過

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

      1

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

      1

      通過spring.profiles.active指定使用哪個profile啟動。

      分別啟動peer1 和 peer2 , 首先啟動peer1會報錯java.net.ConnectException: Connection refused: connect, 因為peer2 還未啟動,等一會即可。

      Step5. 查看服務注冊中心

      訪問: http://peer1:8761/

      訪問: http://peer2:8762/

      3個Eureka Server節點高可用集群搭建步驟

      Eureka Server不向ZK必須奇數個節點,便于選舉。 Eureka Server對節點的個數只要2個以上即可。

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

      方式一

      spring: # 注冊到eureka上的微服務名稱 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為peer1 profiles: peer1 server: port: 8787 eureka: instance: # 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 hostname: peer1 --- spring: # 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer2 # 端口 server: port: 8788 # Eureka eureka: instance: # 當profiles為peer2,hostname是peer2 hostname: peer2 --- spring: # 指定profiles為peer3,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer3 # 端口 server: port: 8789 # Eureka eureka: instance: # 當profiles為peer3,hostname是peer3 hostname: peer3

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

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

      方式二

      spring: # 注冊到eureka上的微服務名稱 application: name: microservice-discovery-eureka-ha-3nodes --- spring: # 指定profiles為peer1 profiles: peer1 server: port: 8787 eureka: instance: # 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 hostname: peer1 prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: # 將自己注冊到peer2這個Eureka上 defaultZone: http://peer2:8788/eureka/,http://peer3:8789/eureka/ --- spring: # 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer2 # 端口 server: port: 8788 # Eureka eureka: instance: # 當profiles為peer2,hostname是peer2 hostname: peer2 prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: # 將自己注冊到peer1這個Eureka上 defaultZone: http://peer1:8787/eureka/,http://peer3:8789/eureka/ --- spring: # 指定profiles為peer3,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer3 # 端口 server: port: 8789 # Eureka eureka: instance: # 當profiles為peer3,hostname是peer3 hostname: peer3 prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: serviceUrl: # 將自己注冊到peer1這個Eureka上 defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

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

      都可以,均驗證通過。

      將服務注冊到Eureka Server集群上及服務調用

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

      啟動micorservice-provider-user,

      同時也修改下 消費者工程 micorservice-consumer-movie-fegin

      啟動 micorservice-consumer-movie-fegin

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

      訪問 http://localhost:7901/movie/1

      驗證通過

      代碼

      2個Eureka Server節點

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

      3個Eureka Server節點

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

      Spring Spring Cloud

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:Spring CloudFinchley】-14 微服務網關Zuul的搭建與使用
      下一篇:WPS2016怎么制作數據透視表?
      相關文章
      亚洲高清免费在线观看| 亚洲免费在线视频观看| 亚洲精品国产美女久久久| 亚洲综合一区国产精品| 亚洲中文字幕在线无码一区二区| 亚洲电影免费观看| 亚洲不卡中文字幕无码| 亚洲成av人片天堂网| 国产成人亚洲精品青草天美| 人人狠狠综合久久亚洲88| 亚洲国产精品无码久久久秋霞2| 亚洲高清专区日韩精品| 亚洲AV日韩AV鸥美在线观看| 亚洲国产高清人在线| 亚洲综合激情九月婷婷| 亚洲视频在线不卡| 亚洲人6666成人观看| 亚洲一区二区三区不卡在线播放| 亚洲乱码无限2021芒果| 一本天堂ⅴ无码亚洲道久久| 亚洲色偷偷综合亚洲av78| 亚洲av无码兔费综合| 伊在人亚洲香蕉精品区麻豆| 亚洲国产精品自产在线播放 | 亚洲国产综合久久天堂| 中文字幕第13亚洲另类| 国产AV无码专区亚洲AV毛网站| 亚洲AV无码国产精品麻豆天美| 91精品国产亚洲爽啪在线观看| 中文字幕亚洲激情| 亚洲午夜久久久久久久久久| 亚洲AV无码精品色午夜果冻不卡 | 亚洲精品永久www忘忧草| 亚洲AV无码专区在线亚| 亚洲伊人精品综合在合线| 国产精品国产亚洲精品看不卡| 亚洲综合精品一二三区在线| 亚洲一区二区三区免费观看| 亚洲日韩精品国产3区| 亚洲国产精久久久久久久| 亚洲乱码中文字幕手机在线 |