springcloud —— 搭建高可用註冊中心

##用途
在實際生產環境下單點的註冊中心是存在缺陷的,當該節點宕機後,依賴他服務的微服務將出現故障,所以就需要一個高可用的註冊中心來彌補這種缺陷。Eureka通過“夥伴”機制實現高可用。每一臺Eureka都需要在配置中指定另一個Eureka的地址作爲夥伴,Eureka啓動時會向自己的夥伴節點獲取當前已經存在的註冊列表, 這樣在向Eureka集羣中新加機器時就不需要擔心註冊列表不完整的問題。
##項目架構
這裏寫圖片描述

##配置文件
###總配置application.properties

spring.profiles.active=slave

##使用ip地址的形式定義註冊中心的地址
eureka.instance.prefer-ip-address=true
#禁用自我保護模式
eureka.server.enable-self-preservation=false

###eureka server 啓用安全驗證
security.user.name=admin
security.user.password=123456
security.basic.enabled=true

#不向註冊中心註冊自己
#eureka.client.register-with-eureka=false
#註冊中心的職責就是去維護服務實例,不需要去檢索服務
#eureka.client.fetch-registry=false

##服務續約任務的調用間隔時間默認爲30s
#eureka.instance.lease-renewal-interval-in-seconds=3
###定義服務失效的時間默認是90s
#eureka.instance.lease-expiration-duration-in-seconds=540

###註冊中心1的配置application-slave.properties

spring.application.name=springcloud-eureka-server
server.port=8766

eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8765/eureka/
eureka.instance.status-page-url-path=/

###註冊中心2的配置application-master.properties

spring.application.name=springcloud-eureka-server
server.port=8765

eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8766/eureka/
eureka.instance.status-page-url-path=/

##啓動
首先先啓動第一個服務,此時把總配置application.properties中的active設置成爲slave。然後啓動啓動類,此時會報錯,不過沒關係。再啓動第二個。

spring.profiles.active=slave
#spring.profiles.active=master

啓動第二個服務的時候,此時把總配置application.properties中的active設置成爲master。然後啓動啓動類。

#spring.profiles.active=slave
spring.profiles.active=master

此時就實現了高可用的註冊中心了。
這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章