简介

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

Spring Cloud Config 服务器

官方文档:链接
服务器为外部配置(名称值对或等效的YAML内容)提供了基于资源的HTTP。服务器可以使用@EnableConfigServer注释轻松嵌入到Spring Boot应用程序中。

1. 创建一个基础的springboot项目

2. 添加Mavan依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. </dependency>

3. 创建启动类

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

4. 修改 bootstrap.yml 配置文件

spring:
  profiles:
    active: native #不使用Git,使用“本机”配置文件
  application:
    name: cloud-config

server:
  port: 8888

以上步骤就创建了一个ConfigServer。
实际使用中,各个服务将通过ConfigServer去加载各种配置。配置文件一般都是存放在Git仓库中,我们为了方便,直接使用本地文件系统。

5. 创建目录并添加配置文件

springboot查找被加载配置文件的目录为:[classpath:/, classpath:/config, file:./, file:./config],所以只需将配置文件存放在这些目录下都可以。也可以自由指定目录:

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/conf

在 cloud-config/src/main/resources/config 目录下新建两个配置文件,service-customer-dev.yml 和service-task-dev.properties
,文件名称需要遵循一定的规则:{application}-{profiles}.yml 或 {application}-{profiles}.properties。

这里配置文件主要是提供给config-client使用的,所以一般情况下,application为client的应用名,profiles 为client的环境配置,如dev,test和prod等。在之后创建客户端的时候,相关的配置需要和这里的文件名对应上。

  • “application”= ${spring.application.name}
  • “profile”= ${spring.profiles.active}

启动服务,使用浏览器访问 http://localhost:8888/service-customer/dev ,http://localhost:8888/service-task/dev,就可以获取到configServer 通过HTTP发布的配置。这里只做为一个测试,实际项目中会自动获取并加载配置。

ConfigServer的HTTP服务具有以下格式的资源:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

Spring Cloud Config 客户端

在微服务架构中,基本上所有的业务服务都可以是一个Config客户端,只要你有从配置中心获取配置的需求,就可以你的服务作为一个Config客户端。

1. 创建一个基础的springboot项目

2. 添加Mavan依赖

<!-- config client 需要的依赖 -->
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!-- 作为一个web服务需要的依赖 -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3. 不进行别的配置,直接启动服务。

2019-06-14 11:24:16.939  INFO 5827 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2019-06-14 11:24:16.979 DEBUG 5827 --- [           main] o.s.web.client.RestTemplate              : HTTP GET http://localhost:8888/application/default

通过上面的启动日志,可以观察到,客户端默认会去本地的8888端口获取外部配置服务。请求的URL为:
http://localhost:8888/application/default, 这个URL也是根据默认配置生成的,其中:
application 为 ${spring.application.name}
default 为 ${spring.profiles.active}
可以分别修改这两个配置,观察是否是这样的。

4. 配置config客户端

config-client 按默认配置获取外部配置服务,但实际中,我们肯定都会有自己配置。
删除默认的application.properties文件,创建一个bootstrap.yml,由于springboot配置文件的加载顺序,推荐所有服务的主配置文件斗使用bootstrap.yml。
编辑配置文件bootstrap.yml:

spring:
  cloud:
    config:
      uri: ["http://localhost:8889"]
  application:
      name: service-customer
    profiles:
      active: dev

为了区别默认配置,我们将配置服务器端口修改为8889,spring.cloud.config.uri 配置的作用是指定配置中心的位置,支持配置多个地址,客户端会依次获取外部配置,直到成功为止。

5. 启动配置服务器 和 配置客户端

编辑一下配置服务器的service-customer-dev.yml文件,添加一些测试配置项

config:
  name: 'customer'

重启配置服务器和配置客户端服务
如果一切顺利,日志中可以看到客户端请求配置服务器的日志记录,并没有报错信息了。
创建一个HTTP服务测试一下:

@RestController
public class HelloController {

    @Value("${config.name}")
    private String name;

    @RequestMapping("/hello")
    public String sayHello(){
        return "Hello " + name;
    }
}

请求 http://localhost:8082/hello
返回:

Hello customer

相关替代品

  1. nacos 阿里巴巴 https://github.com/alibaba/nacos
  2. apollo 携程 https://github.com/ctripcorp/apollo