随着微服務數量增加,為了使生産、測試和運維等各環境能協調統一,即使在環境切換時不會因為微服務的數量而進行大量的修改配置,于是就出現了配置中心。
Config
作用
随着微服務數量的不斷增多,為了使開發者可以管理不同環境(開發、測試、生産)下的配置,一個應用在環境遷移後有完整的配置使程式正常運作
開源配置中心
- 攜程 Apllo
- 百度 Disconf
- 淘寶 Diamond
配置方式
- 本地檔案系統
- SVN
- Git(預設方式)
Git配置倉庫
1. 登入
github

當然,沒有賬号的需要申請一個
2. 建立倉庫
完善倉庫資訊
建立好的倉庫文檔會顯示用戶端的相關操作指令
3. 下載下傳安裝用戶端
下載下傳路徑
下載下傳好後一直next安裝就好,隻不過需要注意的是安裝路徑選擇英文路徑
4. 連接配接倉庫
啟動**git-bash.exe**
//綁定使用者
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
5. 關聯git倉庫
git remote add origin [email protected]:ring-finger/SpringCloud-Config.git
6. 操作指令
//把這個目錄變成Git可以管理的倉庫
git init
//添加
git add .
//送出日志
git commit -m "送出測試"
//送出檔案
git push -u origin master
//更新檔案
git push -u origin master
Config服務端
1. 建立一個 SpringBoot
項目,我這裡命名為ConfigServer
SpringBoot
2. 在pom.xml檔案中添加依賴
<!--config配置中心服務端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3. 修改配置檔案
server.port=8091
spring.application.name=config-server
#配置的git倉庫的位址
spring.cloud.config.server.git.uri=https://github.com/ring-finger/SpringCloud-Config
# git倉庫位址下的相對位址,可以配置多個,用,分割
spring.cloud.config.server.git.search-paths=
spring.cloud.config.server.git.username=ring-finger
spring.cloud.config.server.git.password=githubzw64
# 本地存儲的配置方式,預設檢索src/main/resource路徑下
#spring.profiles.active=native
# 制定本地配置檔案的路徑
#spring.cloud.config.server.native.searchLocations=file:E:/properties/
4.添加注解 @EnableConfigServer
@EnableConfigServer
@EnableConfigServer//激活對配置中心的支援
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Config用戶端
1. 建立一個 SpringBoot
項目,我這裡命名為ConfigClient
SpringBoot
2. 在pom.xml檔案中添加依賴
<!--配置中心用戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3. 修改配置檔案
bootstrap.xml
- springcloud config 的相關配置要放在bootstrap.xml才有效
- application.properties中添加項目名和端口配置
spring:
profiles: dev
cloud:
config:
uri: http://localhost:8091 # 配置中心的具體位址,即 config-server
label: master # 對應 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存儲,則該參數無用
profile: dev # 對應 {profile} 部分
name: config-client # 對應 {application} 部分
4. 擷取配置參數
- github中參數格式
girl:
name: "格式故事"
age: 18
label: "master"
- 配置檔案的對應實體類
@Data
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String name;
private int age;
private String label;
}
- 讀取配置檔案
@RestController
public class ClientController {
@Autowired
private GirlProperties girlProperties;
@GetMapping("/hello")
public String getProperties(){
return girlProperties.toString();
}
}
連接配接測試
- 通路接口
,使用的是用戶端的porthttp://localhost:8092/hello
SpringCloud搭建Config配置中心的用戶端和服務端及配置倉庫github的使用
服務化與高可用
- 服務化其實就是把配置中心
的服務端和用戶端當作config
的用戶端注冊到eureka
的服務中eureka
- 高可用則是通過配置多個(一般兩個)
的服務端,保證服務即使當機時,仍有備用伺服器提供支撐config
- 具體實作請參考之前寫的
eureka
的配置
SpringCloud搭建Eurake注冊中心、提供者和消費者代碼示例
服務重新整理
-
手動重新整理的實作方式可以參考之前的文章
SpringCloud實作Config配置中心的手動重新整理