本文主要記錄一些總結,入門教程可以參考其他資料。本文基于Greenwich.SR5版本
一 叢集搭建
正常情況下,我們肯定是要搭建叢集的。搭建叢集時,注意defaultZone配置項的位址,不能使用localhost。否則Eureka管理界面就不會有下面的資訊
server1
server:
port: 8001 #指定運作端口
spring:
application:
name: eureka-server #指定服務名稱
eureka:
instance:
hostname: server1 #指定主機位址
client:
# fetch-registry: false #指定是否要從注冊中心擷取服務(注冊中心不需要開啟)
# register-with-eureka: false #指定是否要注冊到注冊中心(注冊中心不需要開啟)
serviceUrl:
defaultZone: http://server2:8002/eureka/
server:
enable-self-preservation: false #關閉保護模式
server2
server:
port: 8002 #指定運作端口
spring:
application:
name: eureka-server #指定服務名稱
eureka:
instance:
hostname: server2 #指定主機位址
client:
# fetch-registry: false #指定是否要從注冊中心擷取服務(注冊中心不需要開啟)
# register-with-eureka: false #指定是否要注冊到注冊中心(注冊中心不需要開啟)
service-url:
defaultZone: http://server1:8001/eureka/
server:
enable-self-preservation: false #關閉保護模式
二 安全認證
有時,我們想要提高服務的安全性。則可以對Eureka伺服器添加SpringSecurity。
需要添加Spring security的配置類,以取消csrf檢查
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
節點出現在 unavailable-replicas 下的問題
參考:https://blog.csdn.net/liupeifeng3514/article/details/85273961
三 zone和region
這個我沒有實際用過,先記錄下。一般情況下用不到,隻有在叢集很大,并且服務部署在不同的實體機房時可以使用這個配置來優化。
eureka分區的深入講解,region和zone的使用
四 用戶端配置
建立一個服務eureka用戶端(
也就是服務提供者
)
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
配置檔案
server:
port: 8102
servlet:
context-path: /eurekaClient
spring:
application:
name: eureka-client
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://user:[email protected]:8001/eureka/,http://user:[email protected]:8002/eureka/ #同時注冊到兩個注冊中心
五 優雅地關閉
有時服務已經關閉了,但eureka伺服器不能馬上感應到。預設情況下,最多會有90s延遲.
客戶都安使用actuator的shutdown指令。
用戶端配置添加
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: shutdown,health,info
指令
問題
生産環境下,這些端點肯定是要鑒權的。那如何鑒權通過是個問題。目前想法,可能需要
自定義SpringSecurity的部分邏輯
。
參考
- Spring Cloud Eureka:服務注冊與發現