我们前面接触到的 spring cloud 组件都是基于 Netflix 的组件进行实现的, 这次我们来看下 spring cloud 团队自己创建的一个全新项目: Spring Cloud Config.
它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持, 分为服务端和客户端两个部分.
其中服务端也称为分布式配置中心, 他是独立的微服务应用, 用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息, 加密, 解密信息等);
客户端是微服务架构中的各个微服务应用或基础设施, 它们通过制定的配置中心来管理应用资源与业务相关的配置内容, 并在启动的时候从配置中心获取和加载配置信息.
由于配置中心默认采用 Git 来存储配置信息, 因此我们会用到 Git 相关的内容, 如果没有用过 Git 或者忘记怎么用了, 可以参考下廖雪峰老师的 Git 教程 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 .
另外, 我自己用的 Git 远程仓库是码云.
==================== 华丽的分割线 ===================
接下来看下代码怎么实现.
一, 准备远程 Git 仓库
在 Gitee 上新建一个项目 https://gitee.com/sam-uncle/spring-cloud-learning
在项目下新建子目录 spring-cloud-config-file, 然后新建三个文件
内容分别是
- from
- =
- git-dev-1.0,
- from
- =git-test-1.0,from=git-1.0
新建一个分支 config-lable-test, 新分支里面新建三个同名的文件, 不过内容分别是 from=git-dev-2.0,from=git-test-2.0,from=git-2.0
二, 构建配置中心
先给出最终代码结构:
搭建过程如下:
新建 maven 工程 config-server
修改 POM 文件
- <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>
- <groupId>com.sam</groupId>
- <artifactId>config-server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.1.RELEASE</version>
- </parent>
- <properties>
- <javaVersion>1.8</javaVersion>
- </properties>
- <!-- 使用 dependencyManagement 进行版本管理 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Camden.SR6</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <!-- 引入 config server 依赖 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- </dependencies>
- </project>
创建启动类
- /**
- * @EnableConfigServer
- *
- * 开启 Spring Cloud Config 的服务端功能
- *
- */
- @SpringBootApplication
- @EnableConfigServer
- public class ConfigServerApp {
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApp.class, args);
- }
- }
配置 application.properties 文件, 指定远程仓库信息
- server.port=7001
- spring.application.name=config-server
- # 配置 Git 仓库的地址
- spring.cloud.config.server.git.uri=https://gitee.com/sam-uncle/spring-cloud-learning/
- # 配置仓库路径下的相对搜索位置, 可以配置多个
- spring.cloud.config.server.git.search-paths=spring-cloud-config-file
- # 这里配置你的 Git 仓库的用户名
spring.cloud.config.server.git.username = 用户名
# 这里配置你的 Git 仓库的密码
spring.cloud.config.server.git.password = 密码
启动并验证
访问配置信息的 URL 与配置文件的映射关系如下:
- /{application}/{profile} [/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{appliction}-{profile}.properties
上面的 url 会映射 {application}-{profile}.properties 对应的配置文件, 其中{label} 对应 Git 上不同的分支, 默认是 master.
通过浏览器访问 http://localhost:7001/sam/dev/config-label-test, 结果如下:
三, 实现客户端
最终代码结构:
搭建过程如下:
新建 maven 工程 config-client
修改 POM 文件
- <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>
- <groupId>com.sam</groupId>
- <artifactId>config-client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.1.RELEASE</version>
- </parent>
- <properties>
- <javaVersion>1.8</javaVersion>
- </properties>
- <!-- 使用 dependencyManagement 进行版本管理 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Camden.SR6</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <!-- 引入 config 依赖 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- </project>
创建启动类
- @SpringBootApplication
- public class ConfigClientApp {
- public static void main(String[] args) {
- SpringApplication.run(ConfigClientApp.class, args);
- }
- }
配置 bootstrap.properties 文件, 指定 config-server 位置
- server.port=7002
- #{application}
- spring.application.name=sam
- #{profile}
- spring.cloud.config.profile=dev
- #{label}
- spring.cloud.config.label=master
- #config server uri
- spring.cloud.config.uri=http://localhost:7001/
创建 controller
- @RefreshScope
- @RestController
- public class TestController {
- /**
- * 通过 @Value 来讲配置文件中的值写入到代码中
- */
- @Value("${from}")
- private String from;
- @RequestMapping("/from")
- public String from() {
- return from;
- }
- }
启动并测试
四, 工作原理
Spring Cloud Config 配置中心的工作原理如下:
客户端启动时, 根据 bootstrap.properties 中配置的应用名{application}, 环境名{profile}, 分支名{label}, 向 Config Server 请求获取配置信息.
Config Server 根据自己维护的 Git 仓库信息和客户传递过来的配置定位信息去查找配置信息.
通过 git clone 命令将找到的配置信息下载到本地(Config Server 的文件系统中). 在通过页面访问或启动客户端的时候, 我们在服务端能看到如下下载的 log:
- 2018-05-14 22:51:58.055 INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam-dev.properties
- 2018-05-14 22:51:58.055 INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam.properties
Config Server 创建 Spring 的 ApplicationContext 实例, 并从 Git 本地仓库中加载配置文件, 最后将这些配置内容读取出来返回给客户端.
客户端在获取外部配置信息后加载到客户端的 applicationContext 实例.
来源: https://www.cnblogs.com/sam-uncle/p/9036053.html