5.SpringCloud学习(五)——Spring Cloud Config 配置中心

1.简介

1.1 概述

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

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

1.2 特点

  • HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content): HTTP, 用于外部配置的基于资源的API(键值对,或等效的YAML内容)

  • Encrypt and decrypt property values (symmetric or asymmetric): 加密和解密属性值(对称或非对称)

  • Embeddable easily in a Spring Boot application using @EnableConfigServer Config Client features (for Spring applications): 使用 @EnableConfigServer Config Client功能(适用于Spring应用程序),可轻松集成到Spring Boot应用程序中

  • Bind to the Config Server and initialize Spring Environment with remote property sources: 绑定到Config Server并使用远程属性源初始化Spring `Environment

2.演示环境

  1. JDK 1.8.0_201
  2. Spring Boot 2.2.0.RELEASE、Spring Cloud Hoxton.RELEASE
  3. 构建工具(apache maven 3.6.3)
  4. 开发工具(IntelliJ IDEA )

3.演示代码

image-20200822214521079

总体结构说明:spring cloud config 简单使用

  • ofc-config-server: config 服务端,负责配置管理
  • ofc-config-client: config 客户端,获取服务端配置

3.1 ofc-config-server

3.1.1 代码说明

使用 spring-cloud-starter-config-server 开启配置管理服务端

3.1.2 maven 依赖

pom.xml

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

3.1.3 配置文件

application.properties

spring.application.name=ofc-config-server
server.port=11000
# 本地仓库的git url配置
spring.cloud.config.server.git.uri=file:///E:/GitRepository/gitee/config-server-test

config-server-test 目录下包含4个配置文件

image-20200822215657217

里面的内容如下

# soulballad.properties
my.name=soulballad

# soulballad-dev.properties
my.name=soulballad-dev

# soulballad-prod.properties
my.name=soulballad-pro

# soulballad-test.properties
my.name=soulballad-test

3.1.4 java代码

OfcConfigServerApplication.java

@EnableConfigServer
@SpringBootApplication
public class OfcConfigServerApplication {

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

3.2 ofc-config-client

3.2.1 代码说明

使用 spring-cloud-starter-config-client 调用服务端管理的配置

3.2.2 maven 依赖

pom.xml

<dependencies>
    <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.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

3.2.3 配置文件

bootstrap.properties

bootstrap 中配置服务启动依赖的配置

# 配置服务端url
spring.cloud.config.uri=http://localhost:11000
# 客户端应用名称,对应配置文件名称前缀(soulballad、soulballad-dev、soulballad-test、soulballad-prod)
spring.cloud.config.name=soulballad
# profile:激活的配置
spring.cloud.config.profile=dev
# label:在git中的分支名称
spring.cloud.config.label=master

application.properties

spring.application.name=ofc-config-client
server.port=11010

#management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

management.endpoint.refresh.enabled=true
management.endpoint.beans.enabled=true
management.endpoint.health.enabled=true

3.2.4 java代码

OfcConfigController.java

// 1. 需要开启actuator端点,然后调用刷新接口才能生效; 
// 2. 多节点的情况下,只有调用了刷新接口的节点生效,其他节点未变;
@RefreshScope
@RestController
public class OfcConfigController {

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

    @GetMapping(value = "/name")
    public String getName() {
        return name;
    }
}

OfcConfigClientApplication.java

@SpringBootApplication
public class OfcConfigClientApplication {

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

3.3 git 地址

spring-cloud-ofc-01-config: Spring Cloud 官方提供的分布式配置中心方案

4.效果展示

依次启动 ofc-config-server、ofc-config-client,它们分别监听在 11000、11010 端口。

4.1 ofc-config-server

ofc-config-server.http 访问下列地址,观察输出的配置信息。

默认配置

### GET soulballad-default 默认配置
GET http://localhost:11000/soulballad-default.properties

image-20200822220049149

dev配置

### GET soulballad-dev dev环境配置
GET http://localhost:11000/soulballad-dev.properties

image-20200822220302029

test配置

### GET soulballad-test test环境配置
GET http://localhost:11000/soulballad-test.properties

image-20200822220327296

prod配置

### GET soulballad-prod prod环境配置
GET http://localhost:11000/soulballad-prod.properties

image-20200822220350930

4.2 ofc-config-client

ofc-config-client.http 访问下列地址,观察输出的配置信息。

### 通过url获取配置结果
### GET /name
GET http://localhost:11010/name

由于在 bootstrap.properties 中激活的配置是 dev,所以获取到 soulballad-dev.properties 中配置信息

image-20200822220647113

4.3 配置变更

在实际环境中,配置中心管理的配置信息会有变动,并且希望能够在不重启的情况下使修改后的配置生效。这里模拟着个场景。

修改本地 soulballad-dev.properties 中配置信息,内容如下:

my.name=soulballad-dev1234

查看服务端修改是否生效,发现服务端输出的已经是修改后的配置

### GET soulballad-dev dev环境配置
GET http://localhost:11000/soulballad-dev.properties

image-20200822221056875

查看客户端是否获取到最新配置,发现客户端获取的还是修改前的配置

### GET /name
GET http://localhost:11010/name

image-20200822221203583

这是因为客户端缓存了服务端的配置,修改了配置文件只有服务端生效了,客户端没有生效

在开启 actuator 的情况下,可以手动调用客户端接口,刷新客户端配置

### GET /actuator/refresh 手动触发配置刷新
POST http://localhost:11010/actuator/refresh

image-20200822221415532

刷新完成后,再次在客户端访问配置信息,发现配置已经更新。

### GET /name
GET http://localhost:11010/name

image-20200822221531607

在分布式环境下,如果 config-client 有多个节点,那么只有调用了 /actuator/refresh 接口的节点,配置才能被刷新,未调用接口的节点,配置不会更新。

针对这个问题,Spring Cloud 提供了另外一冲解决方案 Spring-Cloud-Bus

且看下一篇 6.SpringCloud学习(六)——Spring Cloud Bus 消息总线

5.参考

  1. 官方文档-Spring-Cloud-Config
原文地址:https://www.cnblogs.com/col-smile/p/13548867.html