SpringCloud Config配置中心的使用

概述

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),
但最推荐的还是Git,而且使用的是http/https访问的形式

作用

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露

使用

配置中心服务器搭建

先建一个git仓库用于存放配置,参考仓库地址: http://git.codetool.top/RhettPeng/springcloud-config

SpringCloud Config配置中心的使用

新建模块cloud-config-center-3344

依赖:

<!-- config server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

yaml:

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: http://git.codetool.top/RhettPeng/springcloud-config.git # 仓库地址
          search-paths:
            - springcloud-config
      label: master   # 读取分支

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

启动类:这里要使用@EnableConfigServer

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

这样一个配置中心服务器就搭建完了,可以进行测试。

url映射规则

如何通过配置中心服务器访问git仓库中的配置文件?映射规则如下所示:

  • /{application}/{profile}[/{label}](可省略拓展名,但响应的配置格式可能是json形式的,和源文件不相同)
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml(label指定分支,spring.cloud.config.label可配置默认分支)
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

以上面搭建的配置中心服务器为例,可以访问 http://localhost:3344/master/config-dev.yml 来获取 config-dev.yml 配置。还有另外两种写法:

  • http://localhost:3344/config-dev.yml(已配置默认分支)
  • http://localhost:3344/master/config-dev (省略拓展名)

加载顺序和分歧管理

applicaiton.yml是用户级的资源配置项

bootstrap.yml是系统级的,优先级更加高

Spring Cloud会创建一个"Bootstrap Context",作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。

Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件, 保证Bootstrap Context和Application Context配置的分离。

要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的。

因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml(让本地配置的优先级更高)

搭建配置使用端

新建模块cloud-config-client3355,依赖:

<!-- config client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

yaml配置:文件名为bootstrap.yml,这里spring.cloud.config下面配置的是读取哪个配置文件,对应url映射中的参数。

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名称
      name: config # 配置文件名称
      profile: dev # 读取的profile
      uri: http://localhost:3344 #配置中心的地址

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

编写一个可以获取配置的controller测试,这里的config.info是在配置中心中的配置项:

@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

启动该模块,访问 http://localhost:3355/configInfo ,可以看到浏览器输出:

master branch,springcloud-config/config-dev.yml version=1

对应的就是master下的config-dev配置文件

如何动态获取配置修改

如果git仓库中的配置文件发生修改,配置中心能立即获得修改,但配置使用端确无法对修改的配置立即生效,除非重启。那么如何让配置使用端动态获取配置的修改?

首先,保证配置使用端引入了actuator依赖(上面已经引入):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然后,在配置文件中暴露监控端点:

management:
  endpoints:
    web:
      exposure:
        include: "*"

然后,在业务类上加上@RefreshScope注解:

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

此后,当配置修改后,可以向 http://localhost:3355/actuator/refresh 发送一个 POST 请求,使得模块主动刷新配置(这个请求可以利用webhook实现)

但是,倘若有多个配置使用方,难道需要给每个主机发送一个POST请求?有没有更好的方法?这就需要SpringCloud Bus了。。。(见下篇)

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/springcloud-config%e9%85%8d%e7%bd%ae%e4%b8%ad%e5%bf%83%e7%9a%84%e4%bd%bf%e7%94%a8/

(0)
彭晨涛彭晨涛管理者
上一篇 2020年3月22日 10:40
下一篇 2020年3月22日 21:49

相关推荐

发表回复

登录后才能评论