微信登录

配置中心 - Config Server - 集中管理配置

Java - Web - Spring 《配置中心 - Config Server - 集中管理配置》

一、引言

在现代的分布式系统中,各个服务都有自己的配置文件。随着服务数量的增加,配置文件的管理变得越来越复杂,例如配置的一致性难以保证、修改配置需要重启服务等问题。Spring Cloud Config Server 为我们提供了一种集中管理配置的解决方案,它允许我们将所有服务的配置集中存储在一个地方,方便统一管理和更新。

二、Spring Cloud Config Server 概述

Spring Cloud Config Server 是 Spring Cloud 生态系统中的一个组件,它基于 Spring Boot 构建,提供了一个 HTTP 服务,用于集中管理应用程序的外部配置。配置可以存储在本地文件系统、Git 仓库、SVN 仓库等,其中 Git 仓库是最常用的存储方式,因为它具有版本控制的功能,方便管理配置的变更历史。

三、搭建 Config Server

3.1 创建 Config Server 项目

首先,我们使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Cloud Config Server

以下是 pom.xml 文件的示例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.7.5</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.example</groupId>
  13. <artifactId>config-server</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>config-server</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>2021.0.4</spring-cloud.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-config-server</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

3.2 配置 Config Server

application.properties 中添加以下配置:

  1. spring.application.name=config-server
  2. server.port=8888
  3. spring.cloud.config.server.git.uri=https://github.com/yourusername/config-repo.git

这里我们使用 Git 仓库来存储配置文件,需要将 spring.cloud.config.server.git.uri 替换为你自己的 Git 仓库地址。

3.3 启用 Config Server

在主应用类上添加 @EnableConfigServer 注解:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.config.server.EnableConfigServer;
  4. @SpringBootApplication
  5. @EnableConfigServer
  6. public class ConfigServerApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ConfigServerApplication.class, args);
  9. }
  10. }

四、创建配置文件仓库

在 Git 仓库中创建配置文件,例如 application.properties

  1. message=Hello, Config Server!

配置文件的命名规则为 {application}-{profile}.properties{application}-{profile}.yml,其中 {application} 是应用程序的名称,{profile} 是环境配置(如 devprod 等)。如果没有指定 {profile},则默认为 default

五、创建 Config Client 项目

5.1 创建项目

同样使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Cloud Config Client
  • Spring Boot Starter Web

5.2 配置 Config Client

bootstrap.properties 中添加以下配置:

  1. spring.application.name=config-client
  2. spring.cloud.config.uri=http://localhost:8888

这里的 spring.application.name 要与 Git 仓库中配置文件的 {application} 部分一致,spring.cloud.config.uri 是 Config Server 的地址。

5.3 获取配置信息

创建一个控制器来获取配置信息:

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ConfigClientController {
  6. @Value("${message}")
  7. private String message;
  8. @GetMapping("/message")
  9. public String getMessage() {
  10. return message;
  11. }
  12. }

六、测试

启动 Config Server 和 Config Client 项目,访问 http://localhost:8080/message,你应该能看到从 Config Server 获取的配置信息 Hello, Config Server!

七、总结

7.1 优点

优点 描述
集中管理 所有服务的配置集中存储,方便管理和维护。
版本控制 使用 Git 等版本控制系统,可以方便地管理配置的变更历史。
动态更新 可以在不重启服务的情况下更新配置。

7.2 注意事项

  • 确保 Config Server 和 Config Client 的版本兼容。
  • 配置文件的命名和存储要遵循规则,否则可能无法正确获取配置信息。

通过 Spring Cloud Config Server,我们可以轻松地实现配置的集中管理,提高系统的可维护性和可扩展性。希望本文能帮助你快速上手使用 Spring Cloud Config Server。