目錄
一、簡介
這篇文章簡單總結如何搭建配置中心
二、服務端
pom配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
bootstrap.yml或application.yml文件配置,一般建議把spring.application.name這種類似的信息配置到bootstrap中,git信息可以配置到application.yml文件中
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/wulinfeng2/serverConfig
username: xxx #如果企業是付費用戶
password: xxx #如果企業是付費用戶
searchPaths: aciv-cf #項目根路徑
clone-on-start: true #啓動的時候拉取配置文件,而不是需要的時候,這樣可能使啓動變慢,第一次加載配置變快
force-pull: true #配置文件在本地會有一個備份,設置爲true,始終從服務端獲取,防止髒數據
在啓動類上加上@EnableConfigServer註解
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
三、客戶端
pom配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
bootstrap.yml文件配置,注意必須將以下配置配置到bootstrap.yml中,不能配置到application.yml中。bootstrap.yml在application.yml之前加載,用來加載外部資源。
spring:
application:
name: config-demo
cloud:
config:
uri: http://@localhost:8868/manage/serverConfig
name: demo #{描述}-{環境}.yml中的描述,可以配置多個
label: master #github的label,用來做版本控制
fail-fast: true #如果不能連接到配置中心,則無法啓動
retry:
max-attempts: 6 #最大重試次數
request-read-timeout: 6000 #讀取配置超時時間
profiles:
active: dev
在啓動類上加上@EnableDiscoveryClient註解
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}