微服務以單個接口爲顆粒度,一個接口可能就是一個項目,若是每一個項目都包含一個配置文件,一個系統可能有幾十或上百個小項目組成,那配置文件也會有好多,對後續修改維護也是比較麻煩,就和前面的服務註冊同樣,服務註冊與發現是將服務從分散到中心化,而今天的配置中心是將配置文件從分散到中心化,這樣便於後續維護。本篇主要以git爲例學習使用Spring Cloud Config配置中心。html
1、配置中心介紹java
在咱們瞭解spring cloud config以前,我能夠想一想一個配置中心提供的核心功能應該有什麼git
Spring Cloud Config能夠完美的支持以上全部的需求。github
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。Spring cloud使用git或svn存放配置文件,默認狀況下使用git,咱們先以git爲例作一套示例。web
2、Server 端spring
1.準備配置文件apache
爲了演示spring cloud config的使用,這裏在github()上建立了config-repo(https://github.com/ywcui/config-repo)倉庫.而後在該倉庫下建立了3個配置文件neo-config-dev.properties、neo-config-pro.properties、neo-config-test.properties,每一個配置文件都設置了屬性neo.hello,value分別對應i am dev,i am pro,i am test。bootstrap
2.建立Spring Cloud Config Server瀏覽器
建立Spring Boot項目並引入Spring Cloud Config Server,具體pom.xml以下:tomcat
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringCloudConfigServer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringCloudConfigServer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.設置配置文件
在配置文件中作以下配置:
server.port=8001
spring.application.name=spring-cloud-config-server
spring.cloud.config.server.git.uri=https://github.com/ywcui/config-repo
spring.cloud.config.server.git.search-paths=
spring.cloud.config.server.git.username=用戶名
spring.cloud.config.server.git.password=密碼
Spring Cloud Config也提供本地存儲配置的方式。咱們只須要設置屬性spring.profiles.active=native,Config Server會默認從應用的src/main/resource目錄下檢索配置文件。也能夠經過spring.cloud.config.server.native.searchLocations=file:E:/properties/屬性來指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,可是爲了支持更好的管理內容和版本控制的功能,仍是推薦使用git的方式。
4.啓動類設置
只需在啓動類中添加@EnableConfigServer便可。
5.測試
首先咱們先要測試server端是否能夠讀取到github上面的配置信息,直接訪問:http://localhost:8001/neo-config/test
返回信息以下:
若是直接查看配置文件中的配置信息可訪問:http://localhost:8001/neo-config-test.properties,返回:neo.hello: i am test
倉庫中的配置文件會被轉換成web接口,訪問能夠參照如下的規則:
3、Client端
1.建立建立Spring Cloud Config Client
建立Spring Boot項目並引入Spring Cloud Config Client,具體pom.xml以下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringCloudConfigClient</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringCloudConfigClient</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.設置配置文件
準備application.properties、bootstrap.properties兩個配置文件
application.properties:
spring.application.name=spring-cloud-config-client
server.port=8002
bootstrap.properties:
spring.cloud.config.name=neo-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
spring.application.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。
3.建立HelloController
在HelloController中使用@Value注入屬性neo.hello。
package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${neo.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
4.測試
在瀏覽器中輸入http://localhost:8002/hello,則顯示下圖所示。
參考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html