轉自:http://www.javashuo.com/article/p-gnypxwpy-bv.htmlhtml
參考文章java
Spring Cloud 配置中心爲分佈式系統中的服務器端和客戶端提供外部化配置支持。經過Config-Server,你能夠在一個地方集中對全部環境中的應用程序的外部化配置進行管理。例如,當一個應用程序從開發環境切換到測試環境,而後再從測試環境切換到生產環境,你可使用Config-Server統一管理這些環境之間的配置,並確保應用程序在遷移時可以擁有它運行所須要的一切配置。簡而言之:Config-Server 就是用來實現配置統一管理和不一樣環境間配置的統一切換的。Config-Server 服務器的後端存儲默認使用Git,所以它很容易支持配置環境的標籤版本,同時可供多數的內容管理工具去訪問。你也能夠很容易地添加其餘的替代實現,並將它們插入到Spring配置中。git
相關產品:github
來自淘寶的Diamond:https://github.com/takeseem/diamondspring
來自百度的Disconf:https://disconf.readthedocs.io/zh_CN/latest/json
來自Springcloud的Config-Server:https://cloud.spring.io/spring-cloud-stream/bootstrap
搭建配置中心
Config-Server配置中心的工做原理以下圖所示:後端
引入依賴
新建一個maven項目,起名爲config-center,在其 pom.xml 文件中引入以下依賴:瀏覽器
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <properties> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <!-- Eureka-Client 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- Config-Server 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- SpringCloud 版本控制依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
建立啓動類服務器
新建一個Springboot應用的啓動類ConfigCenterApplication類,並在上增長@EnableConfigServer註解,用來啓用Config-Server。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigCenterApplication { public static void main(String[] args) { SpringApplication.run(ConfigCenterApplication.class, args); } }
添加配置
application.yml 添加配置以下:
server: port: 9001 spring: application: name: config-center eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
此時,因爲還沒有配置用來做爲服務器後端存儲的Git倉庫地址,若啓動應用會報以下錯誤:
*************************** APPLICATION FAILED TO START *************************** Description: Invalid config server configuration. Action: If you are using the git profile, you need to set a Git URI in your configuration. If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.
與通常的Spring Boot應用相同,默認狀況下Config-Server也經過8080端口啓動。爲了客戶端讀取配置方便,你能夠把啓動端口改成8888(客戶端默認會從http://localhost:8888/加載與服務ID相同的配置)。此外,Config-Server還經過spring.config.name=configserver(Config-Server Jar包中有一個configserver.yml 配置文件)配置爲咱們設置了一個默認配置庫,客戶端經過配置 spring.cloud.config.discovery.serviceId=configserver 即可直接使用。固然你也能夠經過application.yml來對配置中心進行配置。
建立Git倉庫
本文使用開源中國的碼雲來建立咱們的Git倉庫,固然你也能夠選擇其餘的Github或者阿里雲Git等建立本身的Git倉庫。
點擊導航欄中的「+」按鈕==>新建倉庫,填入倉庫信息,完成建立。
點擊 克隆/下載 ==>複製,將Git倉庫地址複製下來備用。
配置Git倉庫
接下來,在application.yml中添加Git倉庫配置以下:
spring: cloud: config: server: git: uri: https://gitee.com/pengjunlee/config-cloud.git username: 你的碼雲帳號 password: 你的帳號密碼
再次啓動ConfigCenterApplication,發現能夠正常啓動了。啓動完成以後,Eureka註冊中心中註冊的服務列表以下:
搭建客戶端
接下來,咱們經過對上一章《微服務下的鏈路追蹤(Sleuth+Zipkin)》中的product-service服務進行改造,來示例如何從配置中心獲取配置。
引入依賴
在product-service的pom.xml中添加配置中心客戶端依賴:
<!-- Config-Client 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
或者:
<!-- Starter-Config 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
修改配置
先將product-service中的application.yml文件更名爲bootstrap.yml(bootstrap.yml在應用上下文啓動階段加載,比application.yml早),而後再對其內容進行修改:
# 設置服務(應用)名稱 spring: application: name: product-service # 指定用於獲取配置的配置中心服務(應用)名稱 cloud: config: discovery: enabled: true serviceId: config-center profile: dev # 指定分枝版本,默認爲master label: master # 指定註冊中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
注意:若是不指定配置中心,客戶端默認會從http://localhost:8888 加載與服務ID相同的配置。
在Git倉庫建立配置文件
客戶端經過發送Http請求來從配置中心讀取配置,這些Http請求的URI遵循如下規則:
/{name}-{profiles}.properties /{name}-{profiles}.yml || /{name}-{profiles}.yaml /{label}/{name}-{profiles}.properties /{label}/{name}-{profiles}.json /{name}/{profiles}/{label:.*} /{name}-{profiles}.json /{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml /{name}/{profiles:.*[^-].*} /{name}/{profile}/{label}/** /{name}/{profile}/{label}/** /{name}/{profile}/**
其中各個參數的含義以下:
name 服務的ID,即spring.application.name的值,本例中爲 product-service;
profiles 激活的profile,經過spring.cloud.config.profile指定,本例中爲 dev;
label 分枝的版本,經過spring.cloud.config.label指定,本例中爲默認值 master;
接下來咱們須要按照上述規則,在Git倉庫的相應位置建立配置文件。根據bootstrap.yml中的配置,資源請求地址能夠爲 /master/product-service-dev.yml 。
在Git倉庫的master分枝中建立product-service-dev.yml和product-service-test.yml兩個文件,分別將服務的啓動端口指定爲8771和8772:
# product-service-dev.yml server: port: 8771 # product-service-test.yml server: port: 8772
文件建立完成以後,啓動配置中心,先在瀏覽器對兩個文件進行訪問。
此時,啓動product-service將讀取http://localhost:8888/product-service-dev.yml中的配置,即啓動端口爲8771。若將 bootstrap.yml中的spring.cloud.config.profile的值設置爲test,則將讀取http://localhost:8888/product-service-test.yml中的配置,應用的啓動端口也會相應地變爲8772,證實從配置中心讀取配置成功。
參考文章
《一篇好TM長的關於配置中心的文章》
https://cloud.spring.io/spring-cloud-stream/
https://spring.io/projects/spring-cloud-config
https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html