在微服务架构中,每一个服务都有自己的配置文件,这些配置文件还会因为生产、测试环境的不同而分为多个。某些配置项是相同的,某些配置项又是不同的,这给服务的部署和管理造成了一些困难。
Config Center
可以解决这些问题。
通过将配置文件统一放到某个地方(通常是 GitHub),然后让 配置中心 来统一读取、刷新配置信息。
Spring Cloud 提供了 Spring Cloud Config
来提供这一功能。
本节介绍一下 Spring Cloud Config
的使用。本节源码在 https://github.com/laolunsi/spring-boot-examples 中。
config-sever
首先创建一个父级 maven 项目,取名 spring-cloud-config-example
,添加 spring-cloud 依赖:
<?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-dependencies</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-config-example</artifactId>
<version>1.0.RELEASE</version>
<properties>
<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>