项目模板
客户端使用
spring boot项目目录结构模板
├ {projectRoot}
├ pom.xml
├ test
│ └...
└ src
├ main
├ java
│ └ com...
└ resources
├ bootstrap.yml
├ logback-spring.yml
└ application-local.yml
要在应用程序中使用这些功能,只需将其构建为依赖于spring-cloud-config-client的Spring Boot应用程序。
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEAS</spring-cloud.version>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
通常的bootstrap.yml配置方式
bootstrap.yml
spring:
profiles:
active: ${PROFILE:local}
application:
name: my-application
cloud:
config:
uri: http://config-server:8080
profile: ${spring.profiles.active}
username: yourusername
password: yourpassword
---
spring:
profiles: test,dev
cloud:
config:
uri: http://config-server:8080
— YAML可以在同一个文件中,使用—表示一个文档的开始;
git配置仓库目录结构
├ {config-files-project}
├ my-application
│ ├ application-dev.yml
│ ├ application-test.yml
│ └ application-prod.yml
├ my-application1
├ application-dev.yml
├ application-test.yml
└ application-prod.yml
运行机制
如果springboot工程引入了spring-cloud-config-client,服务启动后,configclient 默认会先访问bootstrap.yml,获取application.yml 配置。
环境库
configclient会根据环境变量加载不同的配置文件,bootstrap.yml通过定义spring.profiles.active选择不同的环境。上述模板配置spring.profiles.active=${PROFILE:local}定义了环境变量为%PROFILE%,默认为local。[参考][1]
本地开发
当我们在本地开发环境启动springboot服务没有传任何环境变量时,会启用local配置,也就是使用本地的application-local.yml。
开发联调、测试环境部署
在测试环境、生产环境的配置文件一般都会不同于本地开发,这时我们可以通过启动变量选择不同的配置文件。
java -jar app.jar --spring.profiles.active=dev
参数 spring.profiles.active=dev将替换bootstrap.yml中的spring.profiles.active。这时应用将使用application-dev.yml配置文件,由于本地没有application-dev.yml文件,spring-cloud-config-client将从config-server获取配置文件。
spring-cloud-config-server
spring-cloud-config-server只是一个普通的spring boot web程序,真实的配置文件保存在git仓库中(使用本地文件系统进行git存储库仅用于测试)。完整的配置文件获取顺序为:
application -> config-server -> git repository
所以我们在gitlab或者BitBucket上必须有一个配置文件工程config-files-project,比如我们可以命名为config-files-test。spring-cloud-config-server基于目录结构和文件名获取正确的配置文件。
spring-cloud-config-server默认从git上获取资源的格式如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
为了实现上文一个工程一个目录的配置,我们需要在spring-cloud-config-server中做如下配置:
spring:
profiles: test
cloud:
config:
server:
git:
uri: https://gitlab.company.com/config-files-test/{application}
spring-cloud-config-client配置
spring.cloud.config.uri定义了spring config server的位置,spring-cloud-config-client根据三个变量从config server获取变量:
{application}
映射到客户端的“spring.application.name”;{profile}
映射到客户端上的“spring.profiles.active”(逗号分隔列表); 和{label}
这是一个服务器端功能,标记“版本”的一组配置文件。默认master
在开发联调环境通过以上配置,my-application这个工程就可以取到git仓库中https://gitlab.company.com/config-files-test/my-application/application-dev.yml了。
生产环境部署
生产环境的配置文件一般由运维人员配置,开发人员通常没有权限访问。这时需要独立一个git仓库专门给生产使用,为了做环境隔离,我们也单独部署一个spring-config-server实例。在配置文件中spring-cloud-config-server增加这样的配置:
---
spring:
profiles: prod
cloud:
config:
server:
git:
uri: https://gitlab.company.com/config-files-prod/{application}
在客户端bootstrap.yml的配置中,可以根据不同的profile访问不同的config-server,具体见bootstrap.yml配置模板。
[1]: https://blog.csdn.net/IT_faquir/article/details/80869578 “SpringBoot配置加载,各配置文件优先级对比”
[2]: https://springcloud.cc/spring-cloud-config.html “Spring Cloud Config”
[3]: https://my.oschina.net/u/1469495/blog/1522784 “Spring boot 激活 profile的几种方式”
[4]: http://www.ruanyifeng.com/blog/2016/07/yaml.html “YAML 语言教程”
[5]: https://blog.csdn.net/J080624/article/details/80508606 “SpringBoot - 配置文件加载位置与优先级”
[6]: https://blog.csdn.net/it_faquir/article/details/79842885 “yaml语法及规范”
[7]: https://blog.51cto.com/blief/2118978 “spring boot/cloud 启动方式说明”