首先,我是前端 轉 PHP 轉 JAVA 的以為小白,文中講的不對的地方請?zhí)岢鰜恚矚g迎來噴
起因是我司使用Eureka注冊中心和 Configserver配置中心來達到多服務共享配置的問題,我好奇是如何從配置中心獲取配置后,將配置寫入消費方的。
這便引發(fā)了我4個小時追代碼的過程
Eureka
廢話不多說,首先說說Eureka是個什么東西,其實我也不知道是啥!
首先先上一張看不懂的圖片,好吧我承認,這是我看過理解最快的一張圖片了
Eureka 是 Netflix 開發(fā)的,一個基于 REST 服務的,服務注冊與發(fā)現(xiàn)的組件
它主要包括兩個組件:Eureka Server 和 Eureka Client
- Eureka Client:一個Java客戶端,用于簡化與 Eureka Server 的交互(通常就是微服務中的客戶端和服務端)
- Eureka Server:提供服務注冊和發(fā)現(xiàn)的能力(通常就是微服務中的注冊中心)
各個微服務啟動時,會通過 Eureka Client 向 Eureka Server 注冊自己,Eureka Server 會存儲該服務的信息
也就是說,每個微服務的客戶端和服務端,都會注冊到 Eureka Server,這就衍生出了微服務相互識別的話題
- 同步:每個 Eureka Server 同時也是 Eureka Client(邏輯上的)
多個 Eureka Server 之間通過復制的方式完成服務注冊表的同步,形成 Eureka 的高可用 - 識別:Eureka Client 會緩存 Eureka Server 中的信息
即使所有 Eureka Server 節(jié)點都宕掉,服務消費者仍可使用緩存中的信息找到服務提供者(筆者已親測) - 續(xù)約:微服務會周期性(默認30s)地向 Eureka Server 發(fā)送心跳以Renew(續(xù)約)信息(類似于heartbeat)
- 續(xù)期:Eureka Server 會定期(默認60s)執(zhí)行一次失效服務檢測功能
它會檢查超過一定時間(默認90s)沒有Renew的微服務,發(fā)現(xiàn)則會注銷該微服務節(jié)點
Spring Cloud 已經(jīng)把 Eureka 集成在其子項目 Spring Cloud Netflix 里面
以上都是拷貝的,說白了,Eureka做的就是接口轉發(fā)的概念
SpringCloudConfig
Spring Cloud Config 的官方介紹文檔地址如下:
https://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html#_spring_cloud_config
英語好的自己讀吧,我是懶得看
大致意思是,Spring Cloud Config 提供一種基于客戶端與服務端(C/S)模式的分布式的配置管理。我們可以把我們的配置管理在我們的應用之外(config server 端),并且可以在外部對配置進行不同環(huán)境的管理,比如開發(fā)/測試/生產(chǎn)環(huán)境隔離,并且還能夠做到實時更新配置。
現(xiàn)在要看是追源碼了!!!
首先,配置文件中找到了Eureka的配置項
spring.application.name=quickstart-sample
eureka.client.serviceUrl.defaultZone=${QUICKSTART_EUREKAS:http://${QUICKSTART_USERNAME:admin}:${QUICKSTART_PASSWORD:123123}@localhost:20000/eureka/}
spring.cloud.config.profile=framework,test
spring.cloud.config.label=development-wayne
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=configserver
spring.cloud.config.username=${QUICKSTART_USERNAME:admin}
spring.cloud.config.password=${QUICKSTART_PASSWORD:123123}
找到這些,我依舊是一頭霧水,好吧,我自閉了~~我想放棄了~
開了15分鐘小差,我決定從Maven包下手
在項目中找到spring-cloud-config-client-2.1.0.RELEASE.jar
這個JAR包,并隨便打開一個文件下載源碼(聽起來好高大上,我大PHP直接看vendor什么時候還要下載源碼,哼~~)
全局搜索serviceId
看,這里有用到誒!!
那既然有
set
那就一定有 get
,我繼續(xù)找下去,找到getServiceId()
的調用方出現(xiàn)了無意間看了眼上面,嗯~~ 我猜這應該是心跳吧,他是定時的從配置中心獲取配置。
繼續(xù)走~
在源碼的127行我發(fā)現(xiàn)了
this.config.setUri(uri)
字眼,無奈的看了下下面只剩下catch了,算了,就研究這個config吧!一樣的思維,有
set
就一定有 get
在這里我找到了最終獲取數(shù)據(jù)的地方
response = restTemplate.exchange(uri + path, HttpMethod.GET, entity,
Environment.class, args);
接下來就涉及到返回數(shù)據(jù)的地方了,轉眼到spring-cloud-config-server-2.1.0.RELEASE.jar
這個包中,我們可以看到SpringCloudConfig定義了一個名為EnvironmentController
這樣的控制器。
在控制器中我們找到了根據(jù)上面uri + path
的方式請求的入口,
@RequestMapping("/{name}/{profiles}/{label:.*}")
public Environment labelled(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label) {
if (name != null && name.contains("(_)")) {
// "(_)" is uncommon in a git repo name, but "/" cannot be matched
// by Spring MVC
name = name.replace("(_)", "/");
}
if (label != null && label.contains("(_)")) {
// "(_)" is uncommon in a git branch name, but "/" cannot be matched
// by Spring MVC
label = label.replace("(_)", "/");
}
Environment environment = this.repository.findOne(name, profiles, label);
if(!acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())){
throw new EnvironmentNotFoundException("Profile Not found");
}
return environment;
}
我這里在配置文件中設置了spring.cloud.config.server.jdbc=true
所以我這里是走的數(shù)據(jù)庫,還有其他的讀取配置方式,可自行查閱
根絕我設置的jdbc方式
上面的findOne
使用了JdbcEnvironmentRepository
中的實現(xiàn)方式
最后在第95行找到了
Map<String, String> next = (Map<String, String>) jdbc.query(this.sql,
new Object[] { app, env, label }, this.extractor);
這里的sql可通過配置spring.cloud.config.server.jdbc.sql
進行復寫
如果沒有復寫,那么就使用JdbcEnvironmentProperties
中默認的SQL語句SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
好了,configserver完成了他的任務(東西好少啊,感覺很容易就找到了)。回過頭,我們又要看消費方拿到了返回值做了些什么!!
在ConfigServicePropertySourceLocator
文件中我們看到,其實configserver做了一個攔截,在啟動的時候把配置寫了進去
在同文件104行終于找到了最后的方法,通過CompositePropertySource
將配置加載到程序中
for (PropertySource source : result.getPropertySources()) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) source
.getSource();
composite.addPropertySource(new MapPropertySource(source.getName(), map));
}
其實我想追一下
CompositePropertySource
的源碼,但是我餓~~~~~~了,
第一次寫看源碼的筆記,可能有些地方我自己懂了就跳過了,如果哪里沒寫出來,歡迎提出來