首先,我是前端 轉(zhuǎn) PHP 轉(zhuǎn) JAVA 的以為小白,文中講的不對的地方請?zhí)岢鰜恚矚g迎來噴
起因是我司使用Eureka注冊中心和 Configserver配置中心來達(dá)到多服務(wù)共享配置的問題娃属,我好奇是如何從配置中心獲取配置后爆存,將配置寫入消費(fèi)方的蛉顽。
這便引發(fā)了我4個小時追代碼的過程
Eureka
廢話不多說,首先說說Eureka是個什么東西先较,其實(shí)我也不知道是啥携冤!
首先先上一張看不懂的圖片,好吧我承認(rèn)闲勺,這是我看過理解最快的一張圖片了
Eureka 是 Netflix 開發(fā)的曾棕,一個基于 REST 服務(wù)的,服務(wù)注冊與發(fā)現(xiàn)的組件
它主要包括兩個組件:Eureka Server 和 Eureka Client
- Eureka Client:一個Java客戶端菜循,用于簡化與 Eureka Server 的交互(通常就是微服務(wù)中的客戶端和服務(wù)端)
- Eureka Server:提供服務(wù)注冊和發(fā)現(xiàn)的能力(通常就是微服務(wù)中的注冊中心)
各個微服務(wù)啟動時翘地,會通過 Eureka Client 向 Eureka Server 注冊自己,Eureka Server 會存儲該服務(wù)的信息
也就是說癌幕,每個微服務(wù)的客戶端和服務(wù)端衙耕,都會注冊到 Eureka Server,這就衍生出了微服務(wù)相互識別的話題
- 同步:每個 Eureka Server 同時也是 Eureka Client(邏輯上的)
多個 Eureka Server 之間通過復(fù)制的方式完成服務(wù)注冊表的同步序芦,形成 Eureka 的高可用 - 識別:Eureka Client 會緩存 Eureka Server 中的信息
即使所有 Eureka Server 節(jié)點(diǎn)都宕掉臭杰,服務(wù)消費(fèi)者仍可使用緩存中的信息找到服務(wù)提供者(筆者已親測) - 續(xù)約:微服務(wù)會周期性(默認(rèn)30s)地向 Eureka Server 發(fā)送心跳以Renew(續(xù)約)信息(類似于heartbeat)
- 續(xù)期:Eureka Server 會定期(默認(rèn)60s)執(zhí)行一次失效服務(wù)檢測功能
它會檢查超過一定時間(默認(rèn)90s)沒有Renew的微服務(wù),發(fā)現(xiàn)則會注銷該微服務(wù)節(jié)點(diǎn)
Spring Cloud 已經(jīng)把 Eureka 集成在其子項(xiàng)目 Spring Cloud Netflix 里面
以上都是拷貝的谚中,說白了渴杆,Eureka做的就是接口轉(zhuǎn)發(fā)的概念
SpringCloudConfig
Spring Cloud Config 的官方介紹文檔地址如下:
https://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html#_spring_cloud_config
英語好的自己讀吧,我是懶得看
大致意思是宪塔,Spring Cloud Config 提供一種基于客戶端與服務(wù)端(C/S)模式的分布式的配置管理磁奖。我們可以把我們的配置管理在我們的應(yīng)用之外(config server 端),并且可以在外部對配置進(jìn)行不同環(huán)境的管理某筐,比如開發(fā)/測試/生產(chǎn)環(huán)境隔離比搭,并且還能夠做到實(shí)時更新配置。
現(xiàn)在要看是追源碼了D咸堋I砼怠蜜托!
首先,配置文件中找到了Eureka的配置項(xiàng)
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包下手
在項(xiàng)目中找到spring-cloud-config-client-2.1.0.RELEASE.jar
這個JAR包蜂挪,并隨便打開一個文件下載源碼(聽起來好高大上,我大PHP直接看vendor什么時候還要下載源碼嗓化,哼~~)
全局搜索serviceId
看棠涮,這里有用到誒!刺覆!
那既然有
set
那就一定有 get
严肪,我繼續(xù)找下去,找到getServiceId()
的調(diào)用方出現(xiàn)了無意間看了眼上面谦屑,嗯~~ 我猜這應(yīng)該是心跳吧诬垂,他是定時的從配置中心獲取配置。
繼續(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);
接下來就涉及到返回?cái)?shù)據(jù)的地方了喉磁,轉(zhuǎn)眼到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;
}
我這里在配置文件中設(shè)置了spring.cloud.config.server.jdbc=true
所以我這里是走的數(shù)據(jù)庫涝焙,還有其他的讀取配置方式,可自行查閱
根絕我設(shè)置的jdbc方式
上面的findOne
使用了JdbcEnvironmentRepository
中的實(shí)現(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
進(jìn)行復(fù)寫
如果沒有復(fù)寫孕暇,那么就使用JdbcEnvironmentProperties
中默認(rèn)的SQL語句SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
好了仑撞,configserver完成了他的任務(wù)(東西好少啊,感覺很容易就找到了)妖滔∷硐回過頭,我們又要看消費(fèi)方拿到了返回值做了些什么W帷沮翔!
在ConfigServicePropertySourceLocator
文件中我們看到,其實(shí)configserver做了一個攔截曲秉,在啟動的時候把配置寫了進(jìn)去
在同文件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));
}
其實(shí)我想追一下
CompositePropertySource
的源碼疲牵,但是我餓~~~~~~了,
第一次寫看源碼的筆記,可能有些地方我自己懂了就跳過了榆鼠,如果哪里沒寫出來纲爸,歡迎提出來