一、基本使用
1. Config-Server端
(1)pom:
- parent依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent>
- dependencyManagement
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- dependencies
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>

<?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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ConfigServer</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
(2)啟動類:增加注解@EnableConfigServer,當然@SpringBootApplication也是必不可少的

package com.luych.configServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
(3)application.yml
如果用git作配置倉庫,則:
spring: cloud: config: server: git: uri: https://gitee.com/LOVE0612/SpringCloud.git username: 562759534@qq.com password: 123@abc
- 如果希望ConfigServer啟動的時候就將配置文件clone下來,則可增加配置:spring.cloud.config.server.git.clone-on-start=true。
- 如果配置文件不在git倉庫的根目錄下, 例如:如果配置文件在git倉庫的properties目錄下,則可增加配置spring.cloud.config.server.git.search-paths=properties。當有多個目錄時逗號分隔。
- 如果需要制定git clone后在本地存儲位置,則可增加配置spring.cloud.config.server.git.baseDir。
如果用svn作配置倉庫,則:
spring: cloud: config: server: svn: uri: https://192.168.1.111/svn/SpringCloud username: luyanchao password: abc@123
- 同樣存在spring.cloud.config.server.svn.search-paths配置項,使用方法同git。
- 同樣存在spring.cloud.config.server.git.baseDir配置項,使用方法同git。
如果使用本地文件系統作配置倉庫,則:
spring:
cloud:
config:
server:
native:
search-locations: properties
profile: native
- search-locations:可以是絕對路徑也可以是classpath路徑。
- 使用本地文件系統作為配置倉庫的最大缺點在於:如果ConfigServer多點部署以保證高可用時,需要將文件系統進行共享。
如果使用多種/多個配置倉庫,則:
spring: cloud: config: server: git: uri: https://gitee.com/LOVE0612/SpringCloud.git username: 562759534@qq.com password: 1226ai0612 order: 1 svn: uri: https://192.168.1.111/svn/SpringCloud username: luyanchao password: abc@123 order: 2 native: search-locations: properties order: 3 profile: native, git, svn
- profile:設定哪些配置倉庫可用,逗號分隔
- order:配置倉庫的優先級順序,數字越低優先級越高

server: port: 51011 spring: application: name: ConfigServer cloud: config: server: git: uri: https://gitee.com/LOVE0612/SpringCloud.git search-paths: properties username: 562759534@qq.com password: 123@abc clone-on-start: true
(3)run,並測試
ConfigServer提供多種rest接口訪問配置信息,可通過run日志查看:
Mapped "{[/{name}-{profiles}.properties],methods=[GET]}" onto ... Mapped "{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}" onto ... Mapped "{[/{name}/{profiles:.*[^-].*}],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.properties],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.json],methods=[GET]}" onto ... Mapped "{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}" onto... Mapped "{[/{name}/{profiles}/{label:.*}],methods=[GET]}" onto ... Mapped "{[/{name}-{profiles}.json],methods=[GET]}" onto ... Mapped "{[/{name}/{profile}/{label}/**],methods=[GET],produces=[application/octet-stream]}" onto ... Mapped "{[/{name}/{profile}/{label}/**],methods=[GET]}" onto ... Mapped "{[/{name}/{profile}/**],methods=[GET],params=[useDefaultLabel]}" onto...
- 參數name:即ConfigClient中定義的spring.application.name的值,后續會講到
- 參數profiles:這個不需要多說,用過SpringBoot的應該都知道
- 參數label:如果是git或svn,label對應的是分之;如果是本地文件系統,label對應子目錄名,例如:classpath:properties/{label}
2. Config-Client端
(1)pom:
- parent依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent>
- dependencyManagement
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- dependencies
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies>
(2)啟動類:有@SpringBootApplication即可
(3)bootstrap.yml,注意這里不是application.yml哦
spring:
application:
name: ServiceGateway
cloud:
config:
uri: http://127.0.0.1:51011
profile: company
- spring.application.name:項目名稱
- spring.cloud.config.uri:ConfigServer的地址
- spring.cloud.config.profile:這個不需要多說,用過SpringBoot的應該都知道
根據以上配置內容,可得知最終調用ConfigServer的接口地址為:http://127.0.0.1:51011/ServiceGateway-company.yml
3. 多個Config-Client端公用配置
如果多個Config-Client中有部分配置是相同的,為了避免出現重復代碼,我們可以把這部分相同的配置抽出來單獨做一個配置文件,命名為:application.yml、application.properties,或application-*.yml、application-*.properties,然后放到Config-Server端即可。