【Spring Cloud系列】Config详解与应用

【Spring Cloud系列】Config详解与应用

一、概述

Spring Cloud Config是由Spring Cloud团队开发的一个项目,它是为微服务架构中各个微服务提供集中化的外部配置支持。虽然现在已经有不少公司用最新版本的Nacos可以取代它,但是Spring Cloud Config还是有很多老的项目依然在运行。

Spring Cloud Config可以将各个微服务的配置文件集中存储在一个外部的存储仓库或系统(例如Git,SVN等)中,对配置的统一管理,以支持各个微服务的运行。

对于单体项目,一般都是直接把相关配置放在单独的配置文件中,以properties或yml的格式出现,更省事儿的方式是直接放到 application.properties 或 application.yml 中。但是这样的方式有个明显的问题,那就是,当修改了配置之后,必须重启服务,否则配置无法生效。

目前在国内有比较多开源配置中心,比如携程Apollo、蚂蚁金服的DisConf等,这些配置中心对比Spring Cloud Config功能更强大,配置中心更好用。

二、Config组成

Spring Cloud Config 包含以下两个部分:

  • Config Server:也被称为分布式配置中心,它是一个独立运行的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密信息和解密信息的访问接口。
  • Config Client:指的是微服务架构中的各个微服务,它们通过 Config Server 对配置进行管理,并从 Config Sever 中获取和加载配置信息。

Spring Cloud Config 默认使用 Git 存储配置信息,因此使用 Spirng Cloud Config 构建的配置服务器天然就支持对微服务配置的版本管理。我们可以使用 Git 客户端工具方便地对配置内容进行管理和访问。除了 Git 外,Spring Cloud Config 还提供了对其他存储方式的支持,例如 SVN、本地化文件系统等。

三、Spring Cloud Config 工作原理

3.1 原理图

在这里插入图片描述

3.2 Spring Cloud Config的原理

  1. Spring Cloud Config 的核心原理是将应用程序的配置存储在远程仓库中,并将其作为一个 REST API 来访问。Config Server 会自动从远程仓库中获取配置,然后将其返回给 Config Client。Config Client 可以通过 HTTP 或 HTTPS 协议来访问 Config Server,并获取应用程序的配置。
  2. Spring Cloud Config 支持多种仓库类型,如 Git、SVN、本地文件系统、Vault 等。其中,Git 是最常用的仓库类型。在使用 Git 作为配置仓库时,Config Server 会自动从 Git 仓库中获取配置文件,并将其转换为一个 REST API 来访问。配置文件的命名规则是 a p p l i c a t i o n − {application}-application−{profile}.yml 或 a p p l i c a t i o n − {application}-application−{profile}.properties,其中 application是应用程序的名称, {application} 是应用程序的名称,application是应用程序的名称,{profile} 是应用程序的环境。

四、如何使用Spring Cloud Config

下面介绍如何使用Spring Cloud Config来管理应用程序的配置。

4.1 创建Config Server
  • 创建一个Config Server,用于存储和管理应用程序的配置。可以使用Spring Boot来创建Config Server,只需要添加以下依赖:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 应用程序的启动类商添加@EnableConfigServer注解,即可启动Config Server:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    
    
  public static void main(String[] args) {
    
    
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}
  • 通过在application.yml文件配置Config Server会从Git仓库中获取配置文件
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-server.git
4.2 创建Config Client
  • bom.xml添加依赖
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • 在应用程序的启动类上添加@EnableConfigrationProperties注解,启动Config Client:
@SpringBootApplication
@EnableConfigurationProperties
public class ConfigClientApplication {
    
    
  public static void main(String[] args) {
    
    
    SpringApplication.run(ConfigClientApplication.class, args);
  }
}
  • applicaion.yml添加配置指定Config Server配置,Config Client会从Config Server中获取应用程序的配置
spring: 
  cloud: 
    config: 
      uri: http://localhost:8088
  • 获取配置

上面我们已创建了Config Server和Config Client,下面将介绍如何从Config Server中获取应用程序配置。

我们可以通过在application.yml文件中添加以下配置来指定应用程序的名称和环境:

spring: 
  application: 
    name: configapp
  profiles: 
    active: dev

这里,我们将应用程序的名称设置为 configapp,环境设置为 dev。然后,在 Config Server 中创建一个名为 myapp-dev.properties 的配置文件,内容如下:

foo = bar

接下来,在 Config Client 中可以通过 @Value 注解来获取配置:

@RestController
public class ConfigController {
    
    
  @Value("${foo}")
  private String foo;

  @GetMapping("/foo")
  public String getFoo() {
    
    
    return foo;
  }
}

这样,当访问 /foo 接口时,就可以获取到配置中的 foo 属性了。

4.3 配置加解密

在实际使用中,我们可能需要对配置进行加解密,以保证配置的安全性。Spring Cloud Config 支持配置加解密,可以使用Jasypt来实现。

首先,我们需要在 Config Server 和 Config Client 中添加以下依赖:

<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot-starter</artifactId>
</dependency>

然后,在 Config Server 中,可以通过在 application.yml 文件中添加以下配置来指定加解密密钥:

jasypt:
  encryptor:
    password: mysecretkey

然后,在 Config Server 中创建加密的配置文件,可以使用 Jasypt 命令行工具来加密:

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="foo=bar" password=mysecretkey algorithm=PBEWithMD5AndDES

将加密后的结果保存为 myapp-dev.properties.encrypted 文件。然后,在 Config Client 中,可以通过在 application.yml 文件中添加以下配置来指定解密密钥:

jasypt:
  encryptor:
    password: mysecretkey

然后,在 Config Client 中可以直接获取解密后的配置:

@RestController
public class ConfigController {
    
    
  @Value("${foo}")
  private String foo;

  @GetMapping("/foo")
  public String getFoo() {
    
    
    return foo;
  }
}

这样,当访问 /foo 接口时,就可以获取到解密后的配置中的 foo 属性了。

五、总结

Spring Cloud Config 是 Spring Cloud 生态系统中的一个重要组件,它提供了一种分布式配置管理的解决方案,能够集中管理应用程序的配置,支持多种后端存储,如 Git、SVN、本地文件系统、Vault 等。在本文中,我们介绍了 Spring Cloud Config 的概念、原理和使用方法,并提供了一些代码示例。希望本文对于了解和使用 Spring Cloud Config 有所帮助。

猜你喜欢

转载自blog.csdn.net/songjianlong/article/details/133394512