Eureka Server高可用集群
Eureka Client會定時連接Eureka Server,獲取服務注冊表中的信息並緩存到本地,微服務在消費遠程API的時候不用每次去Server端查詢,而是使用本地緩存的數據,一般來講即使Server宕機,也不會影響微服務之間的調用,但肯定會影響Client端服務的更新,所以生產環境中,高可用的Eureka Server是必不可少的。
Eureka Server可以通過運行多個實例並相互注冊的方式來實現高可用。 Eureka Server實例會彼此增量的同步信息,確保所有節點數據一致。
集群環境下,需要保持默認值,即 true
2個Eureka Server節點高可用集群搭建步驟
1、新建子模塊 microservice-discovery-eureka-ha
2、引入依賴
<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>
3、hosts配置
linux : /etc/hosts
windows : C:\Windows\System32\drivers\etc\hosts
4、application.yml注冊兩個Eureka Server
spring: # 注冊到eureka上的微服務名稱 application: name: 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: service-url: # 將自己注冊到peer2這個Eureka上 defaultZone: http://peer2:8762/eureka spring: # 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active參數指定使用哪個配置文件啟動 profiles: peer2 #端口 server: port: 8762 eureka: instance: hostname: peer2 instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} client: service-url: #將注冊到peer1的eureka defaultZone: http://peer1:8761/eureka/
application.yml中使用連字符 ---
將配置文件分為三段,第二段和第三段分別為spring. profiles指定了名稱,該名稱表示它所在的那段內容應該在哪個profile中。
啟動測試
方法一: 在STS中配置Run Configurations
主類右鍵 Run As — Run Configurations --Spring Boot App
或者 打包成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
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
通過spring.profiles.active指定使用哪個profile啟動。
分別啟動peer1 和 peer2 , 首先啟動peer1會報錯java.net.ConnectException: Connection refused: connect, 因為peer2 還未啟動,等一會即可。
查看服務注冊中心
3個Eureka Server節點高可用集群搭建步驟
Eureka Server不向ZK必須奇數個節點,便於選舉。 Eureka Server對節點的個數只要2個以上即可
方式一
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
在公共的配置中指定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/
不在公共的配置中指定defaultZone,在peer1 peer2 peer3中相互注冊
將服務注冊到Eureka Server集群上及服務調用
使用micorservice-provider-user作為演示,修改下defaultZone的地址
啟動micorservice-provider-user,同時也修改下 消費者工程 micorservice-consumer-movie-fegin