六、springcloud之配置中心Config
一、配置中心提供的核心功能
Spring Cloud Config為服務端和客戶端提供了分布式系統的外部化配置支持。配置服務器為各應用的所有環境提供了一個中心化的外部配置。它實現了對服務端和客戶端對Spring Environment和PropertySource抽象的映射
Spring Cloud Config項目是一個解決分布式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client通過接口獲取數據、并依據此數據初始化自己的應用。
Spring cloud使用git或svn存放配置文件,當然他也提供本地化文件系統的存儲方式,默認情況下使用git。
二、構建Config Server
分為三步:
1.pom.xml中引入spring-cloud-config-server依賴:
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency> </dependencies>2.啟動類添加@EnableConfigServer注解,激活對配置中心的支持
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);} }3.配置文件
Git配置:(開發,測試,生產三份文件)
server:port: 8001 spring:application:name: spring-cloud-config-servercloud:config:server:git:uri: https或ssh # 配置git倉庫的地址search-paths: # git倉庫地址下的相對地址,可以配置多個,用,分割。username: # git倉庫的賬號password: # git倉庫的密碼svn配置: (和git版本稍有區別,需要顯示聲明subversion.)
server:port: 8001spring:cloud:config:server:svn:uri: username:password:default-label: trunk profiles:active: subversionapplication:name: spring-cloud-config-server? 本地存儲配置的方式:(只需設置屬性)
spring.profiles.active=native,
Config Server會默認從應用的src/main/resource目錄下檢索配置文件。也可以通過spring.cloud.config.server.native.searchLocations=file:F:/properties/屬性來指定配置文件的位置。
雖然Spring Cloud Config提供了這樣的功能,但是為了支持更好的管理內容和版本控制的功能,還是推薦使用git的方式。
?
4.測試:
github創建了一個springcloudconfigtest目錄作為配置倉庫,并根據不同環境新建了下面四個配置文件:
- config-test-dev.properties
- config-test-test.properties
- config-test-prod.properties
為每個配置文件分別設置了testproperties屬性賦予不同的值
如:?hello im dev/test/pro?
測試server端是否可以讀取到github上面的配置信息,直接訪問:http://localhost:8001/config-test/dev
{"name": "config-test","profiles": ["dev"],"label": null,"version": "","state": null,"propertySources": [{"name": "{git/svn/本地配置地址}dev.yml","source": {"debug": true,"server.port": 8001,"testproperties": "hello im dev" ...//數據源,redis等配置}}] }上述的返回的信息包含了配置文件的位置、版本、配置文件的名稱以及配置文件中的具體內容,說明server端已經成功獲取了git倉庫的配置信息。
直接查看配置文件中的配置信息可訪問:http://localhost:8001/config-test-dev.properties,返回:testproperties: hello im dev
倉庫中的配置文件會被轉換成web接口,訪問可以參照以下的規則:
-
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的url會映射{application}-{profile}.properties對應的配置文件,{label}對應git上不同的分支,默認為master。
如config-test-dev.properties,它的application是config-test,profile是dev。client會根據填寫的參數來選擇讀取對應的配置。
修改配置文件中的配置,并提交,server會自動更新提交的配置
三、微服務客戶端(獲取server的配置)
1.添加依賴:spring-cloud-starter-config
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> </dependencies>引入spring-boot-starter-web包方便web測試
2.創建啟動類
啟動類只需要@SpringBootApplication注解就可以
@SpringBootApplication public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);} }3.配置文件
需要配置兩個配置文件,application.yml和bootstrap.yml
application.yml如下:
spring:application:name: spring-cloud-config-client server:port: 8002bootstrap.properties如下:
spring:cloud:config:name: "config-test"lab: masteruri: http://localhost:8001/profile: dev
說明:
- spring.cloud.config.name:對應{application}部分
- spring.cloud.config.profile:對應{profile}部分
- spring.cloud.config.label:對應git的分支。如果配置中心使用的是本地存儲,則該參數無用
- spring.cloud.config.uri:配置中心的具體地址
- spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于擴展為高可用配置集群。
特別注意:上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部分內容才能被正確加載。因為config的相關配置會先于application.properties,而bootstrap.properties的加載也是先于application.properties。
測試:
使用@Value注解來獲取server端參數的值
@Value("${server端的參數}")private String hello;遇到的問題:此時在修改server端的配置文件的參數值,server可以讀取到修改后的配置,但是client端讀取的值還是舊的
解決方案:/refresh
Spring Cloud Config分服務端和客戶端,服務端負責將git(svn)中存儲的配置文件發布成REST接口,客戶端可以從服務端REST接口獲取配置。但客戶端并不能主動感知到配置的變化,從而主動去獲取新的配置。客戶端如何去主動獲取新的配置信息呢,springcloud已經給我們提供了解決方案,每個客戶端通過POST方法觸發各自的/refresh。
1.添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套監控的功能,可以監控程序在運行時狀態,其中就包括/refresh的功能。
2.開啟更新機制
需要給加載變量的類上面加載@RefreshScope,在客戶端執行/refresh的時候就會更新此類下面的變量值。
?
@RestController @RefreshScope // 使用該注解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。3.springboot 1.5.X 以上默認開通了安全認證,所以需要在配置文件application.properties添加以下配置
management.security.enabled=falsetip:可以使用cmd,進行post訪問
curl -X POST http://localhost:8002/refresh
每次手動刷新客戶端也很麻煩,有沒有什么辦法只要提交代碼就自動調用客戶端來更新呢,github的webhook是一個好的辦法。
webhook
參見:http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html
參考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html
http://blog.didispace.com/springcloud4/
?
轉載于:https://www.cnblogs.com/soul-wonder/p/9214944.html
總結
以上是生活随笔為你收集整理的六、springcloud之配置中心Config的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 类中的构造方法实例化的时候自动执行
- 下一篇: Android初学第86天