SpringCloud配置中心搭建與使用(本地儲存配置)
轉自:“https://msd.misuland.com/pd/3107373619924174238
本文涉及要點:
- SpringCloud Config Center作為配置中心
- 本地儲存配置檔案(還有svn,git方式等)
- 配置中心config的優先順序問題
- 基於HTTP Basic的使用者認證
SpringBoot版本:2.0.4.RELEASE
SpringCloud版本:Finchley.SR1
application.name:
服務端:config-center
客戶端:static-configer
依賴
服務端:
- pom.xml配置:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
客戶端:
- pom.xml配置:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
配置
服務端:
- SpringBoot啟動類新增@EnableConfigServer註解
- bootstrap.yml配置:
spring:
application:
name: config-center
profiles:
#native表示本地方式
active: native
cloud:
config:
server:
native:
# 配置檔案存放路徑
search-locations: classpath:/cnf
客戶端:
- bootstrap.yml配置:
spring:
application:
name: static-configer
profiles:
active: qas
# 配置中心存放配置檔案格式:${application.name}-${profiles.active}.myl
# 例如static-configer-dev.yml、static-configer-qas.yml
# 通過上述兩個配置去配置中心讀取對應的配置檔案
cloud:
config:
# uri 配置中心地址
uri: http://localhost:8000
fail-fast: true
檔案
服務端:
在src/main/resource/新增cnf資料夾,不推薦使用config作為名稱,因為config會作為配置中心本身自己的配置資料夾
並在cnf中新增三個配置檔案static-configer-dev.yml、static-configer-prd.yml、static-configer-qas.yml,分別填寫不同配置
配置效果與寫在本地一致
啟動
先啟動服務端,後啟動客戶端
客戶端日誌列印瞭如下日誌表示配置成功:
Fetching config from server at : http://localhost:8000
Located environment: name=static-configer, profiles=[qas], label=null, version=null, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='classpath:/config/static-configer-qas.yml'}]}
The following profiles are active: qas
訪問
無論配置檔案是什麼格式,yml或者properties
瀏覽器訪問http://localhost:8000/static-configer-dev.yml({spring.application.name}-spring.application.name−{profile})能輸出yml格式的配置
spring:
cloud:
config:
override-none: true
訪問 http://localhost:8000/static-configer-dev.properties 就能輸出properties格式的配置
spring.cloud.config.override-none: true
或者:http://localhost:8000/static-configer/dev ({spring.application.name}/spring.application.name/{profile})
{"name":"static-configer","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cnf/application.yml","source":{"spring.cloud.config.override-none":true}}]}
優先順序
關鍵配置:
注意:這段配置是寫在Config Server的配置檔案中才會生效,Config Client則會報錯
#預設情況下:
spring:
cloud:
config:
# 配置中心配置覆蓋Java執行時引數。
# 值為true時:java -jar -Dserver.port=6666 myapp.jar 指定埠將被遠端配置覆蓋掉
# 外部屬性覆蓋系統屬性,系統屬性:JVM -Dparam
overrideSystemProperties: true
allowOverride: true
override-none: false
原始碼如下:
//org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties.java
/**
* Flag to indicate that the external properties should override system properties.
* Default true.
*/
private boolean overrideSystemProperties = true;
/**
* Flag to indicate that {@link #isOverrideSystemProperties()
* systemPropertiesOverride} can be used. Set to false to prevent users from changing
* the default accidentally. Default true.
*/
private boolean allowOverride = true;
/**
* Flag to indicate that when {@link #setAllowOverride(boolean) allowOverride} is
* true, external properties should take lowest priority, and not override any
* existing property sources (including local config files). Default false.
*/
private boolean overrideNone = false;
優先順序預設情況下:
配置中心>本地配置
overrideNone:
- 預設false
- allowOverride=true時才生效
- true:ConfigCenter最低優先順序,不覆蓋任何現有屬性源(包括本地配置檔案)
- 也就是說設定為true,Client Server之外的任何配置優先順序都大於對應的Client Server config
allowOverride:
- 預設:true
- 用來控制systemPropertiesOverride是否生效
- 設定為false以防止使用者意外更改預設值(預設值:配置中心配置的值)
- 設定為false,overrideSystemProperties的值為預設值true,修改無效
overrideSystemProperties:
- 預設:true
- 用以控制ConfigCenter是否覆蓋【命令列指定】的屬性(“-D”指定、“–”字首指定)
配置中心公共配置
比如,serverA-dev.yml、serverB-dev.yml如果兩個服務都需要使用同一個服務發現(Eureka)的配置,那就要在不同的配置檔案中配置多遍,萬一Eureka地址改了,這樣的話,每個配置檔案都要修改一遍,非常難以維護
與一般服務一樣,指定的配置檔案存放路徑下(classpath:/cnf)application.yml(properties)將會被當做所有服務的公共配置
application-profile(dev\qas\prd).yml會被作為對應的環境的公共檔案
例如存在application-dev.yml、application-qas.yml、application-prd.yml
編譯profile=dev環境時application-dev.yml中的配置會被應用到所有服務。
基於HTTP Basic的使用者認證
服務端新增配置:
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml:
spring:
security:
user:
name: admin
password: admin123
客戶端新增配置:
方式一:
spring:
clould:
config:
username: admin
password: admin123
方式二:
spring:
clould:
config:
uri: http://admin:admin123@localhost:8080/
如果方式一和方式二合併:
spring.cloud.config.password和spring.cloud.config.username的優先順序更高,URL中包含的賬號和密碼將無效。
spring:
clould:
config:
uri: http://admin:admin123@localhost:8080/
username: admin2
password: admin456