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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hu_wen/article/details/82772616

使用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 入门之一. 服务注册 继续展开, 让“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>

完整配置如下:

 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服务

这个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

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

 二. 创建config client

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

 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了。

 

5.  配置的更新

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

转至:http://www.cnblogs.com/chry/p/7250584.html

猜你喜欢

转载自blog.csdn.net/Hu_wen/article/details/82772616