Spring Cloud 入门教程(02):配置管理

配置服务

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

以上是Spring Cloud官网对配置服务的描述, 简单阐述一下我的理解。比如我们要搭建一个网站,需要配置数据库连接,指定数据库服务器的IP地址,数据库名称,用户名和口令等信息。通常的方法, 我们可以在一个配置文件中定义这些信息,或者开发一个页面专门配置这些东西。只有一个web服务器的时候, 很方便。

但假如需要搭建同多台服务器时,当然可以每台服务器做同样配置,但维护和同步会很麻烦。我理解的配置服务至少有两种不同场景:

1). 多个客户使用同一配置: 比如,多台服务器组成的集群,假如后端使用同一数据库,那么每台服务器都是用相同的配置。
2). 不同客户使用不同的配置: 比如典型的场景是,开发,测试,生产使用相同的系统,但使用不同的数据库

如果有个统一的根本配置,是不是就很方便,一个可行的办法是,把这些配置文件放到一个共享存储(比如网络共享盘)中。这样只需要在共享存储修改一个或多个配置文件就可以了。但共享文件的方式受到具体布署环境的限制,很多时候很难达到多台Web服务器共享同一个存储硬盘。

共享盘的缺点是资源定位比较困难,Spring Cloud的解决方案是, 将这些配置文件放到版本管理服务器里面,Spring Cloud缺省配置使用GIT中。所有Web服务均从GIT中获取这些配置文件。由于GIT服务器与具体Web服务器之间不需要共享存储, 只要网络可达就行,从而可以实现Web服务于配置信息的存放位置的解耦。

Spring Cloud统一控制应用和GIT服务的交互,应用只需要按照Spring Cloud的规范配置GIT的URL即可。 使用GIT后,场景2和场景1的区别仅仅是,场景2中不同的client使用不同版本的配置文件,但应用访问的文件看起来是会是同一个。
Spring Cloud的配置服务结构入下图
enter description here

下面我们继续上一节的例子 Spring Cloud 入门教程 (01):服务注册 继续展开, 让“Hello World”从配置文件helloworld.properties读出,内容格式如下

1
hello=Hello World

其中关键字hello的值“Hello World”,就是我们要输出的内容。

一. 创建config Server

  1. 创建Config Server, maven工程里面配置spring-cloud-config-server
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.cwalzy</groupId>
    <artifactId>springcloud.helloworld.config.server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud.helloworld.config.server</name>
    <description>Demo Config Server</description>

    <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

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

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>
  1. 创建Config Server,它也是一个Spring Boot应用@EnableConfigServer注解说明了一个Config Server。同样我们使用@EnableEurekaClient将它注册到服务中心。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package org.cwalzy.config.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableEurekaServer
@EnableConfigServer
@SpringBootApplication
public class SpringcloudHelloworldConfigServerApplication {

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

}
  1. Config server的配置文件appication.yml , 注意配置文件的url是GIT服务器的仓库地址, searchPaths配置文件所在的文件夹在仓库中的路径, 在server端不需要指定具体配置文件名, 因为具体的配置文件是什么有应用(也就是client)决定。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8888

spring:
cloud:
config:
server:
git:
uri: https://gitee.com/ErDongErHuo/spring-cloud
searchPaths: helloworldConfig
application:
name: config-server
  1. 启动config server后,访问http://localhost:8888/abc/xyz, 可见如下响应。这个是输出是并没有包括具体配置文件的内容, 这个响应说明,config server可以正常访问我们配置在application.yml中的GIT服务
    enter description here

这个URL是啥意思, 需要解释一下。我们从输出就可以看到 abc 就是application的名字,xyz是profile的名字, 注意这里的abc, xyz均是随便输入的名字, 并不需要真实存在,config server这个REST接口返回的只是应用名为abc, profile名为xyz时,GIT配置环境的结构。

config server提供的REST接口,Spring Cloud官方文档提供了几个可选URL可以是如下几个:

1
2
3
4
5
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

比如 第三个格式,如果我们在GIT版本库中有一个配置文件 spring-cloud/helloworldConfig/config-client-dev.properties. 那么访问http://localhost:8888/config-client-dev.properties 就可以显示配置文件内容。这个例子中, application的名字是”config-client”(也是下面我们即将创建的client), profile名字是dev, 文件后缀是.properties
enter description here

二. 创建config client

  1. 创建maven工程, pom.xml如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.cwalzy</groupId>
    <artifactId>springcloud.helloworld.config.client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud.helloworld.config.client</name>
    <description>Demo Spring Config Client</description>

    <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

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

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>
  2. 创建一个spring boot应用作为client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package org.cwalzy.config.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
//开启refresh机制, 需要给加载变量的类上面加载@RefreshScope注解,其它代码可不做任何改变,
//那么在客户端执行/refresh的时候就会更新此类下面的变量值,包括通过config client从GIT获取的配置。
@RefreshScope
public class SpringcloudHelloworldConfigClientApplication {

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

@Value("${hello}")
String hello;
@RequestMapping(value = "/hello")
public String hello(){
return hello;
}

}

这个应用非常简单,就是从Config Server中获取配置项hello的值,Client Server向Config Server提交REST请求后,Config Server将访问GIT服务器,并将取得的配置项hello的值返回给client.

  1. Config client需要一个应用配置文件, 定义config Server的URL,以及要访问的GIT具体分支。这个配置文件是bootstrap.yml (或者bootstrap.properties)
1
2
3
4
5
6
7
8
9
10
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8888/
server:
port: 8881

这个配置定义了应用的名字是config-client(这就是将要用于组装前面Config Server一节中题到的application), profile采用dev, GIT分支用master。url是config server的地址。那么问题来了,我们似乎没定义配置文件名, 那配置文件名是什么呢? 这点又体现了约定优于配置的思路, 这里Spring Cloud约定, 应用的配置文件名以如下方式组成:{application}-{profile}.properties(或者{application}-{profile}.yml)。比如我们这个应用的配置文件就是config-client-dev.properties. 所以只需要在GIT的中创建配置文件spring-cloud/helloworldConfig/config-client-dev.properties就可以了, 内容如下:

1
hello=Hello World from GIT
  1. 启动config-client应用后, 可以访问http://locahost/8881/hello, 可以看到,应用本身并没有直接配置hello的具体内容, 也没指定具体配置文件,所欲这些都由spring cloud框架提交给config server了。
    enter description here
  1. 配置的更新

至此,spring cloud的配置管理简单示例已经完成,但client 不能自动感知服务端的变化。 比如,我们修改了GIT中的文件内容,但无论如何刷新client端的页面,都不能反映配置的变化。下一节介绍Spring Cloud的配置自动更新机制

本文转载自 https://www.cnblogs.com/chry/p/7250584.html
原文基于 spring boot 1.5.3.RELEASE 版本
而本文中有一些改动 是基于 spring boot 2.1.5.RELEASE 版本