一、基本使用
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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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也是必不可少的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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:配置仓库的优先级顺序,数字越低优先级越高
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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端即可。