在现代的分布式系统中,各个服务都有自己的配置文件。随着服务数量的增加,配置文件的管理变得越来越复杂,例如配置的一致性难以保证、修改配置需要重启服务等问题。Spring Cloud Config Server 为我们提供了一种集中管理配置的解决方案,它允许我们将所有服务的配置集中存储在一个地方,方便统一管理和更新。
Spring Cloud Config Server 是 Spring Cloud 生态系统中的一个组件,它基于 Spring Boot 构建,提供了一个 HTTP 服务,用于集中管理应用程序的外部配置。配置可以存储在本地文件系统、Git 仓库、SVN 仓库等,其中 Git 仓库是最常用的存储方式,因为它具有版本控制的功能,方便管理配置的变更历史。
首先,我们使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:
以下是 pom.xml
文件的示例:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在 application.properties
中添加以下配置:
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/yourusername/config-repo.git
这里我们使用 Git 仓库来存储配置文件,需要将 spring.cloud.config.server.git.uri
替换为你自己的 Git 仓库地址。
在主应用类上添加 @EnableConfigServer
注解:
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);
}
}
在 Git 仓库中创建配置文件,例如 application.properties
:
message=Hello, Config Server!
配置文件的命名规则为 {application}-{profile}.properties
或 {application}-{profile}.yml
,其中 {application}
是应用程序的名称,{profile}
是环境配置(如 dev
、prod
等)。如果没有指定 {profile}
,则默认为 default
。
同样使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:
在 bootstrap.properties
中添加以下配置:
spring.application.name=config-client
spring.cloud.config.uri=http://localhost:8888
这里的 spring.application.name
要与 Git 仓库中配置文件的 {application}
部分一致,spring.cloud.config.uri
是 Config Server 的地址。
创建一个控制器来获取配置信息:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${message}")
private String message;
@GetMapping("/message")
public String getMessage() {
return message;
}
}
启动 Config Server 和 Config Client 项目,访问 http://localhost:8080/message
,你应该能看到从 Config Server 获取的配置信息 Hello, Config Server!
。
优点 | 描述 |
---|---|
集中管理 | 所有服务的配置集中存储,方便管理和维护。 |
版本控制 | 使用 Git 等版本控制系统,可以方便地管理配置的变更历史。 |
动态更新 | 可以在不重启服务的情况下更新配置。 |
通过 Spring Cloud Config Server,我们可以轻松地实现配置的集中管理,提高系统的可维护性和可扩展性。希望本文能帮助你快速上手使用 Spring Cloud Config Server。