Spring Boot集成Spring Cloud Config实现集中式配置管理?

Spring Cloud Config是现在比较常用的一个集中式配置管理系统,其目的就是在分布式系统中提供统一的集中的配置管理。Spring Cloud Config属于Spring Cloud生态的一部分,支持了微服务系统中的配置集中化管理动态配置和配置版本管理等功能。下面我们就来详细介绍一下如何在Spring Boot应用程序中集成Spring Cloud Config实现集中式统一配置管理。

为什么要用Spring Cloud Config?

在一个分布式系统中,为了能够统一的对各个微服务实例配置进行管理,我们可以通过Spring Cloud Config实现配置文件的集中式的管理,这样我们就可以在一个中心位置配置和管理所有微服务的配置文件,而不需要再每个服务中独立的维护配置,这样不但提高了配置的管理效率,Spring Cloud Config还支持了热部署的功能,也就是说在不重启服务的情况下就可以实现配置的更新,这样我们就可以保证配置能够实时的更新。

通过基于Git或者是SVN的提供的版本管理功能,可以轻松实现配置历史管理实现配置文件的回退版本管理等操作,同时为了支持不同的环境配置,也可以通过不同的环境来动态的加载配置文件,例如可以在开发环境、测试环境、灰度环境、生产环境中使用不同的配置文件。

通过这种方式能够极大提升微服务架构中的配置管理效率。

使用Spring Cloud Config

在Spring Cloud Config实现中包含了两个重要的组件如下所示。

  • Config Server(配置服务器):用于存储和提供配置的服务,通常会从Git仓库、SVN或文件系统中读取配置,并将其提供给客户端应用程序。
  • Config Client(配置客户端):在Spring Boot应用中,通过集成Config Client来拉取配置并将其注入到应用的上下文中。实现配置动态管理。

配置Spring Cloud Config Server

首先需要先建设一个Config Server,如下所示,在Spring Boot的POM依赖中添加如下的依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

然后通过@EnableConfigServer注解,来启动配置服务端功能,如下所示。

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

完成上述配置之后,接下来就是在配置文件中添加Config Server的数据源配置,如下所示,通过Git仓库来管理配置文件。

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/config-repo
          searchPaths: config
          cloneOnStart: true
  • uri: 用来指定配置源的Git仓库地址。
  • searchPaths: 用来指定仓库中存放配置文件的路径。
  • cloneOnStart: 用来在服务启动时检查是否自动克隆Git仓库。

接下来,我们就可以启动应用,然后尝试访问http://localhost:8888查看配置管理。

配置Spring Boot应用程序作为Config Client

配置完服务端之后,接下来就是配置客户端的的依赖,如下所示,首先需要在客户端应用中添加客户端的配置依赖,如下所示。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

然后接下来需要注意,添加客户端的配置链接的内容是在bootstrap.yml配置文件中实现,与application.yml不同的是bootstrap.yml配置文件是在应用启动的时候进行加载的而我们需要的配置文件就是需要在应用启动的时候就准备好,所以我们可以在bootstrap.yml中指定Config Server的地址和应用的名称,来提前指定好配置加载的路径,然后再容器加载的时候就可以根据配置文件进行加载了。

spring:
  application:
    name: my-application   # 这个名字会映射到Config Server上配置文件的名称
  cloud:
    config:
      uri: http://localhost:8888   # Config Server的地址

然后我们就可以在在Git仓库中的config目录下,为不同的应用和环境添加不同的配置文件,例如添加my-application.yml配置文件,这里的配置文件命名可以通过spring.application.name设置的值来进行命名,如下所示。

# my-application.yml (在Git仓库中)
server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: password

然后我们可以通过@Value注解或@ConfigurationProperties来读取Config Server提供的配置,如下所示。

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Value("${server.port}")
    private int serverPort;

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Server is running on port: " + serverPort);
    }
}

这样在启动Spring Boot应用程序时,应用程序就会自动从Config Server中拉取配置并添加到应用中使用对应配置。

动态刷新配置

在上面的配置中我们介绍的是静态配置的获取,在Spring Cloud Config还提供了动态配置操作,也就是说当在Config Server中更新了配置之后,客户端不需要重启就可以更新配置内容,要启动这个功能,我们可以通过@RefreshScope注解和Spring Cloud Bus来实现。如下所示。

添加Spring Cloud Bus依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId> <!-- 或使用其他消息中间件 -->
</dependency>

然后通过@RefreshScope注解来添加动态配置功能。

@RestController
@RefreshScope
public class HelloController {

    @Value("${message:default}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}

配置完成之后,我们可以通过调用/actuator/refresh端点来触发客户端配置更新的操作。如下所示。

curl -X POST http://localhost:8080/actuator/refresh

总结

作为Spring Cloud生态的一部分Spring Cloud Config能够充分的为分布式应用提供集中式的配置管理功能,通过Spring Cloud Config Server我们可以快速高效的实现统一的配置管理操作。极大的提升了微服务架构中的配置管理效率。