《Spring Cloud Netflix》-- 服務註冊和服務發現-Eureka的服務認證和集群
一、 Eureka的服務認證
1、 服務端添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、 服務端添加配置
# 服務認證
security.basic.enabled=true security.user.name=roncoo security.user.password=123456
3、 修改服務端和客戶端的配置
eureka.client.serviceUrl.defaultZone=http://roncoo:[email protected]:8761/eureka/
建議:
能在內網的,盡量在內網,這樣既可以保障安全,也能降低復雜度。
二、 Eureka的集群
說明:啟動3個註冊中心,我們將roncoo1、roncoo2、roncoo3各自都將serviceUrl指向另外兩個節點,即是roncoo1、roncoo2、roncoo3是兩兩互相註冊的。
步驟:
1、設置hosts:C:\Windows\System32\drivers\etc\hosts;
# Cluster hosts 127.0.0.1roncoo1 127.0.0.1roncoo2 127.0.0.1roncoo3
2、修改配置
每個服務都需要相應的修改,服務-01
spring.application.name=spring-cloud-server-01 eureka.instance.hostname=roncoo1 eureka.client.serviceUrl.defaultZone=http://roncoo:[email protected]:8762/eureka/,http://roncoo:[email protected]:8763/eureka/
同理:服務-02,服務-03都做相對應的修改
3、配置修改
#這裏是為了測試,生產應該保持默認值:30s
eureka.instance.lease-renewal-interval-in-seconds=5
#剔除失效服務的檢測時間,默認值:60000,即60s
eureka.server.eviction-interval-timer-in-ms=5000
另一種集群方式(不可用):
說明:註冊中心1指向註冊中心2, 註冊中心2指向註冊中心3,那麽當服務向註冊中心1註冊時,註冊中心2中會有該服務的註冊信息,但是註冊中心3中沒有。
結論:服務註冊信息不會被二次傳播。
證實:PeerAwareInstanceRegistryImpl類
// If it is a replication already, do not replicate again as this will create a poison replication
如果該註冊信息是其它Eureka同步過的則不會再繼續傳播給自己的peer節點
// If the url represents this host, do not replicate to yourself.
如果是本機的,不要向自己發同步請求
參考微服務視頻內容:Spring Cloud 第一季
《Spring Cloud Netflix》-- 服務註冊和服務發現-Eureka的服務認證和集群