1. 前言
Config Server負責跟配置保存方通信例如git,將遠程服務器上的配置克隆到本地。
2. git建立配置文件
springcloudconfig\shared目錄下建立
- config-client-dev.yml
server:
port: 9900
foo: dev foo version
- config-client-test.yml
server:
port: 9900
foo: test foo version
新建release1.0分支
增加label參數,下爲開發,測試一致
server:
port: 9900
foo: dev foo version
label: release1.0
3. 搭建Config Server
3.1 pom依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
3.2 yml配置
server:
port: 9800
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/wenrongyao/springcloudconfig/
search-paths: shared
username: # 公共倉庫可以不寫
password: # 公共倉庫可以不寫
3.3 主啓動類
@SpringBootApplication
@EnableConfigServer
public class ConfigServer9800Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer9800Application.class, args);
}
}
4. git文件的訪問方式
- /{application}/{profile}[/{label}]
- /{label}/{application}-{profile}.yml
用我們前面建立的兩個配置文件來做說明:
- config-client-dev.yml
- config-client-test.yml
config-client指的是{application}部分
dev/test指的是{profile部分}
{label}值得是git的版本號
eg:下面兩者都可以訪問配置文件,當label不寫時默認爲master
http://localhost:9800/config-client/dev/release1.0
http://localhost:9800/release1.0/config-client-dev.yml