微服務意味着要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態的配置管理設施是必不可少的。
SpringCloud提供了ConfigServer來解決這個問題,我們每一個微服務自己帶着一個application.yml, 上百個配置文件的管理就很麻煩了,所以有了Config配置中心,一處配置,處處生效
SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一個中心化的外部配置。
SpringCloud Config分爲服務端和客戶端兩部分。
服務端:也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口.
客戶端:則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容.
配置中心作用:
- 集中管理配置文件
- 不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release
- 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息
- 當配置發生變動時,服務不需要重啓即可感知到配置的變化並應用新的配置
- 將配置信息以REST接口的形式暴露,post、curl訪問刷新均可....
由於SpringCloud Config默認使用Git來存儲配置文件(也有其它方式,比如支持SVN和本地文件),但最推薦的還是Git,而且使用的是http/https訪問的形式。
下面爲配置中心的搭建:
服務端搭建:
1、先在git上創建倉庫,然後將本地倉庫初始化,創建config-dev.yml等文件(注意一定爲utf-8格式,vim中可以通過:set fileencoding來查看和設置utf-8格式),上傳到github上,如果此步驟有疑問,可參考這篇文章上傳本地項目到github,config-dev文本
config:
info: master,this is config-dev.yml version:3
2、構建pom文件,主要是config-server的配置文件
<dependencies>
<!--springcloud 的config配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web-->
<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>
<!--一般基礎通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3、構建application.yml文件,設置端口號,註冊進註冊中心,設置git倉庫和目錄及讀取分支等
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
# GitHub上面git倉庫地址
uri: https://github.com/demonruin/springcloud-config #git倉庫那個 https地址
# uri: [email protected]:demonruin/springcloud-config.git #ssh地址啓動會報錯,reject HostKey: github.com,所以使用https即可
# 搜索目錄
search-paths:
- springcloud-config
#讀取分支
label: master
eureka:
client:
#表示是否將自己註冊進EurekaServer默認爲true。
register-with-eureka: true
#是否從EurekaServer抓取已有的註冊信息,默認爲true。單節點無所謂,集羣必須設置爲true才能配合ribbon使用負載均衡
fetchRegistry: true
service-url:
#單機
defaultZone: http://localhost:7001/eureka
# 集羣
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
4、構建主啓動類,加上註解@enableConfigServer
package com.king.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* created by king on 2020/4/27 10:44 上午
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
5、啓動註冊中心7001,然後啓動配置中心3344項目,進行url訪問進行驗證讀取遠程倉庫的配置數據,此處讀取規則可以有很多種,如果讀取的信息在git倉庫中沒有,會讀取爲空{}:官網提供的格式springcloud config官網:
http://localhost:3344/springcloud-config/config-dev
{
name: "springcloud-config",
profiles: [
"config-dev"
],
label: null,
version: "10a87420ab98c60cd3961622f58b615593ee8712",
state: null,
propertySources: [ ]
}
http://localhost:3344/config-dev.yml
config:
info: master,this is config-dev.yml version:3
http://localhost:3344/config/dev/master
{
name: "config",
profiles: [
"dev"
],
label: "master",
version: "10a87420ab98c60cd3961622f58b615593ee8712",
state: null,
propertySources: [
{
name: "https://github.com/demonruin/springcloud-config/config-dev.yml",
source: {
config.info: "master,this is config-dev.yml version:3"
}
}
]
}
此時代表 成功實現了用SpringCloud Config 通過GitHub獲取配置信息,完成了服務端的配置中心的搭建!
客戶端搭建:
搭建之前引入一個bootstrap.yml文件:
- applicaiton. yml是用戶級的資源配置項
- bootstrap. yml是系統級的,優先級更加高
Spring Cloud會創建一個"Bootstrap Context",作爲Spring應用的Application Context'的父上下文。初始化的時候," Bootstrap Context"負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的"Environment"。
"Bootstrap"屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。Bootstrap context和Application Context有着不同的約定,所以新增了一個bootstrap.ymI文件, 保證Bootstrap Context和Application Context配置的分離。
要將Client模塊下的application.yml文件改爲bootstrap.yml,這是很關鍵的,
因爲bootstrap.yml是比application.yml先加載的。bootstrap.ymI優先級高 於application.yml
1、構建pom文件,添加依賴配置中心客戶端的依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.king.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、構建bootstrap.yml文件
server:
port: 3355
spring:
application:
name: cloud-config-client
cloud:
#config 客戶端配置
config:
label: master # 分支名稱
name: config # 配置文件名稱
profile: dev # 讀取後綴名稱 上述3個綜合: master分支上config-dev.yml的配置文件被讀取http://localhost:3344/master/config-dev.yml
uri: http://localhost:3344/ #配置中心地址
eureka:
client:
#表示是否將自己註冊進EurekaServer默認爲true。
register-with-eureka: true
#是否從EurekaServer抓取已有的註冊信息,默認爲true。單節點無所謂,集羣必須設置爲true才能配合ribbon使用負載均衡
fetchRegistry: true
service-url:
#單機
defaultZone: http://localhost:7001/eureka
# 集羣
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
3、構建主啓動類
package com.king.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* created by king on 2020/4/27 1:58 下午
*/
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
4、構建測試controller,來請求配置中心服務端來獲取配置信息
package com.king.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* created by king on 2020/4/27 2:04 下午
*/
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping(value = "/configInfo")
public String getValue(){
return configInfo;
}
}
5、通過服務端的5步測試可以得知服務端服務正常,此時通過請求url來驗證客戶端也能正常請求:
http://localhost:3355/configInfo
master,this is config-dev.yml version:3
此時代表客戶端能通過服務端進行訪問github上的配置了,表示客戶端也搭建完成~
注意:
但是會有一個問題產生,Linux運維修改GitHub上的配置文件內容做調整,刷新3344,發現ConfigServer配置中心立刻響應,刷新3355,發現ConfigServer客戶端沒有任何響應,3355沒有變化除非自己重啓或者重新加載,難道每次運維修改配置文件,客戶端都需要重啓??針對此問題,需要對客戶端進行下述手動配置
1、在pom中添加actuator監控依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、在bootstrap.yml文件中添加配置
#暴露監控端點
management:
endpoints:
web:
exposure:
include: "*"
3、在controller上加入@RefreshScope註解
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping(value = "/configInfo")
public String getValue(){
return configInfo;
}
}
4、此時修改完github上的文件後,還是會有上述問題,此時需要在修改完github文件後,再手動訪問一次url刷新
curl -X POST "http://localhost:3355/actuator/refresh"然後響應成功["config.client.version","config.info"]
5、此時再訪問,客戶端訪問到的就是最新修改的文件數據了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
可能到此還有疑問,難道每次修改完配置,都需要一個curl的去請求嗎?如果太多的微服務配置需要修改,雖然可以寫個批處理文件去執行,但是也總歸是麻煩啊,對於手動動態刷新配置的情況,後面就出來了 springcloud bus 消息總線,通過消息總線即可實現廣播功能,一處修改,處處生效~請參閱接下來的這篇spingcloud bus消息總線配置