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

时间:2020-12-02 17:41:45

使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与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的配置服务结构入下图

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

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

hello=Hello World

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

一. 创建config Server

 1.  创建Config Server, maven工程里面配置spring-cloud-config-server

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

完整配置如下:

Spring Cloud 入门教程(二): 配置管理Spring Cloud 入门教程(二): 配置管理
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.chry</groupId>
6 <artifactId>springcloud.helloworld.config.server</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>jar</packaging>
9 <name>helloworld.config.server</name>
10 <description>Demo Config Server</description>
11
12 <parent>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-parent</artifactId>
15 <version>1.5.3.RELEASE</version>
16 <relativePath/> <!-- lookup parent from repository -->
17 </parent>
18
19 <properties>
20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22 <java.version>1.8</java.version>
23 </properties>
24
25 <dependencies>
26 <!--eureka server -->
27 <dependency>
28 <groupId>org.springframework.cloud</groupId>
29 <artifactId>spring-cloud-starter-eureka</artifactId>
30 </dependency>
31 <dependency>
32 <groupId>org.springframework.cloud</groupId>
33 <artifactId>spring-cloud-starter-eureka-server</artifactId>
34 </dependency>
35 <dependency>
36 <groupId>org.springframework.cloud</groupId>
37 <artifactId>spring-cloud-config-server</artifactId>
38 </dependency>
39 <!-- spring boot test-->
40 <dependency>
41 <groupId>org.springframework.boot</groupId>
42 <artifactId>spring-boot-starter-test</artifactId>
43 <scope>test</scope>
44 </dependency>
45 </dependencies>
46
47 <dependencyManagement>
48 <dependencies>
49 <dependency>
50 <groupId>org.springframework.cloud</groupId>
51 <artifactId>spring-cloud-dependencies</artifactId>
52 <version>Dalston.RC1</version>
53 <type>pom</type>
54 <scope>import</scope>
55 </dependency>
56 </dependencies>
57 </dependencyManagement>
58
59 <build>
60 <plugins>
61 <plugin>
62 <groupId>org.springframework.boot</groupId>
63 <artifactId>spring-boot-maven-plugin</artifactId>
64 </plugin>
65 </plugins>
66 </build>
67
68 <repositories>
69 <repository>
70 <id>spring-milestones</id>
71 <name>Spring Milestones</name>
72 <url>https://repo.spring.io/milestone</url>
73 <snapshots>
74 <enabled>false</enabled>
75 </snapshots>
76 </repository>
77 </repositories>
78
79 </project>
pom.xml

2. 创建Config Server,它也是一个Spring Boot应用,@EnableConfigServer注解说明了一个Config Server。同样我们使用@EnableEurekaClient将它注册到服务中心。

 1 package springcloud.helloworld.config.server;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.config.server.EnableConfigServer;
6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
7
8 @EnableEurekaServer
9 @EnableConfigServer
10 @SpringBootApplication
11 public class ConfigServerApplication {
12 public static void main(String[] args) {
13 SpringApplication.run(ConfigServerApplication.class, args);
14 }
15 }

3. Config server的配置文件appication.yml , 注意配置文件的url是GIT服务器的仓库地址, searchPaths配置文件所在的文件夹在仓库中的路径, 在server端不需要指定具体配置文件名, 因为具体的配置文件是什么有应用(也就是client)决定。

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8888

spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/chrywhy/test
searchPaths: spring-cloud/helloworldConfig
application:
name: config-server

4. 启动config server后,访问http://localhost:8888/abc/xyz, 可见如下响应。这个是输出是并没有包括具体配置文件的内容, 这个响应说明,config server可以正常访问我们配置在application.yml中的GIT服务

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

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

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

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{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

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

本例由于配置了eureka服务中心,所以这个config server作为一个eureka client注册到了 eureka server中, 可以从http://localhost:8761看到我们启动的config server, 如果不需要注册到服务中心, 也可把这个配置去掉

 

 二. 创建config client

1.  创建maven工程, pom.xml如下:
Spring Cloud 入门教程(二): 配置管理Spring Cloud 入门教程(二): 配置管理
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>com.chry</groupId>
5 <artifactId>Springcloud.helloworld.config.client</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7 <name>Springcloud.helloworld.config.client</name>
8 <packaging>jar</packaging>
9 <description>Demo Spring Config Client</description>
10
11 <parent>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-parent</artifactId>
14 <version>1.5.3.RELEASE</version>
15 <relativePath/> <!-- lookup parent from repository -->
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21 <java.version>1.8</java.version>
22 </properties>
23
24 <dependencies>
25 <dependency>
26 <groupId>org.springframework.cloud</groupId>
27 <artifactId>spring-cloud-starter-eureka</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 </dependency>
33 <dependency>
34 <groupId>org.springframework.cloud</groupId>
35 <artifactId>spring-cloud-starter-config</artifactId>
36 </dependency>
37 <dependency>
38 <groupId>org.springframework.boot</groupId>
39 <artifactId>spring-boot-starter-test</artifactId>
40 <scope>test</scope>
41 </dependency>
42 </dependencies>
43
44 <dependencyManagement>
45 <dependencies>
46 <dependency>
47 <groupId>org.springframework.cloud</groupId>
48 <artifactId>spring-cloud-dependencies</artifactId>
49 <version>Dalston.RC1</version>
50 <type>pom</type>
51 <scope>import</scope>
52 </dependency>
53 </dependencies>
54 </dependencyManagement>
55
56 <build>
57 <plugins>
58 <plugin>
59 <groupId>org.springframework.boot</groupId>
60 <artifactId>spring-boot-maven-plugin</artifactId>
61 </plugin>
62 </plugins>
63 </build>
64
65 <repositories>
66 <repository>
67 <id>spring-milestones</id>
68 <name>Spring Milestones</name>
69 <url>https://repo.spring.io/milestone</url>
70 <snapshots>
71 <enabled>false</enabled>
72 </snapshots>
73 </repository>
74 </repositories>
75
76
77 </project>
pom.xml

2. 创建一个spring boot应用作为client

 1 package springcloud.helloworld.config.client;
2
3 import org.springframework.beans.factory.annotation.Value;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RestController;
8
9 @SpringBootApplication
10 @RestController
11 public class ConfigClientApplication {
12
13 public static void main(String[] args) {
14 SpringApplication.run(ConfigClientApplication.class, args);
15 }
16
17 @Value("${hello}")
18 String hello;
19 @RequestMapping(value = "/hello")
20 public String hello(){
21 return hello;
22 }
23 }

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

3. Config client需要一个应用配置文件, 定义config Server的URL,以及要访问的GIT具体分支。这个配置文件是bootstrap.yml (或者bootstrap.properties)

 1 spring:
2 application:
3 name: config-client
4 cloud:
5 config:
6 label: master
7 profile: dev
8 uri: http://localhost:8888/
9 server:
10 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就可以了, 内容如下:

hello=Hello World from GIT

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

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

5.  配置的更新

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

 

上一篇: Spring Cloud 入门教程(一): 服务注册

下一篇:Spring Cloud 入门教程(三): 配置自动刷新