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 

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM