每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

一、简介

  Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。

  Spring Cloud Config服务端特性

  • HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
  • 属性值的加密和解密(对称加密和非对称加密)
  • 通过使用@EnableConfigServer在Spring boot应用中非常简单的嵌入。

  Config客户端的特性(特指Spring应用)

  • 绑定Config服务端,并使用远程的属性源初始化Spring环境。
  • 属性值的加密和解密(对称加密和非对称加密)

  入门示例:

  只要classpath下有Spring Boot Actuator和Spring Config Client,Spring Boot应用就会尝试连接配置服务http://localhost:8888,这个地址是spring.cloud.config.uri的默认地址。如果你想修改这个地址,你可以在bootstrap.[yml或properties]中设置spring.cloud.config.uri或者通过系统属性或者通过环境变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@EnableAutoConfiguration
@RestController
public class Application {
 
  @Value("${config.name}")
  String name = "World";
 
  @RequestMapping("/")
  public String home() {
    return "Hello " + name;
  }
 
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
 
}

  上面例子中的config.name可以来自本地的配置文件,也可以来自远程的配置服务。默认情况下,远程的配置服务将优先使用。

  为了运行你自己的配置服务中心,你可以使用spring-cloud-config-server依赖,和@EnableConfigServer注解。如果你设置了spring.config.name=configserver,应用将会运行在8888端口,并且从一个样本仓库提供数据。你需要设置spring.cloud.config.server.git.uri来指定你自己的配置数据。默认的,它是一个git仓库,也可以配置成本地的文件系统。

二、Spring Cloud Config服务端

  服务器为外部配置(键称值对或等效的YAML内容)提供了基于资源的HTTP。它可以在Spring Boot应用中使用@EnableConfigServer内嵌。例子如下:

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    }
}

  像所有的Spring Boot应用一样,它默认运行在8080端口,你可以通过多种方式将其切换到8888端口。最简单的可以设置spring.config.name=configserver(在Config Server的jar包有一个configserver.yml),它设置了一个默认的配置仓库。另外一种方式是使用你自己的application.properties,这也是小编推荐的方式:

1
2
server.port: 8888
spring.cloud.config.server.git.uri: git地址

  git地址中是你的YAML或者properties文件。

  环境仓库

  你想在哪里存储配置数据?支持这种行为的策略是EnvironmentRepository,它服务于Environment实例。这个Environment是Spring Environment的一个浅副本。Environment通过3个变量被参数化。

  • {application}映射客户端的"spring.application.name"
  • {profile}映射客户端的"spring.profiles.active"(逗号分隔列表)
  • {label}它是服务端的特性,标记版本的一组配置文件

  仓库的实现通常表现的像Spring boot加载配置文件一样,"spring.config.name"等于{application}参数, "spring.profiles.active" 等于{profile}参数。profiles的优先规则和正常的规则是一样的,活动的profiles优于默认的。如果有多个profiles,则最后一个胜出。

  客户端的配置实例:

1
2
3
4
5
spring:
  application:
    name: foo
  profiles:
    active: dev,mysql

  在Spring Boot应用中,这些参数也可以通过环境变量或者命令行参数设置。

  git后端

  EnvironmentRepository的默认实现是使用git后端,它对管理更新、物理环境和审核更改非常的方便。要改变仓库的地址,你可以在配置服务端设置"spring.cloud.config.server.git.uri"属性(在application.properties文件中)。如果你用file:开头设置它,它将从本地仓库运行,这样可以在没有服务端的情况下非常快速和简单的启动。这种情况,服务端将直接在本地仓库中运行。为了扩展配置服务并使它高可用,你需要把服务的所有实例指向同一个仓库,因此只有共享文件系统可以工作。即使在这种情况下,最好使用共享文件系统存储库的ssh:协议,以便服务器可以将其克隆并使用本地工作副本作为缓存。

  该仓库的实现将HTTP资源中的{label}参数映射到git的标签(提交id、分支名称或者tag)。如果git分支或者tag名称中包含“/”,则HTTP URL中的label要使用特殊字符“(_)”代替。例如:如果分支的名称是foo/bar,则HTTP中的label的格式为foo(_)bar。这个特殊字符也可以用到{application}参数中。

  git URI中的占位符

  Spring Cloud Config Server支持在git URL中使用占位符,使用{application} 和 {profile}(如果使用{label},请记住它是使用在git标签中的)。因此你可以轻松的支持“一个应用一个仓库”的原则。如下:

1
2
3
4
5
6
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/{application}

  或者一个环境一个仓库的原则,使用{profile}代替{application}。另外在{application}参数中使用特殊字符"(_)"可以支持多组织。

1
2
3
4
5
6
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/{application}

  {application}参数的格式为"organization(_)application"。

  模式匹配和多仓库

  在{application}和{profile}参数中使用模式匹配可以支持更多复杂的需求。模式的格式是一组逗号分隔的{application}/{profile},其中的参数可以使用通配符。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

  如果{application}/{profile}没有匹配到任何模式,它将使用默认的仓库地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"仓库匹配的是“simple/*”(它仅仅匹配一个仓库simple,在所有的环境下)。"local"仓库将匹配所有{application}的名字以“local”开头的,并且也是在所有的环境下。“/*”前缀自动添加到所有没有设置{profile}的模式中。

  每一个仓库也可以在子目录下存储配置文件,模式匹配也可以用于搜索这些目录,需要制定searchPaths,如下:

1
2
3
4
5
6
7
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*

  上面的例子中,将在foo和以bar开头的目录中,搜索配置文件。

  默认地,服务器在第一次请求配置文件时克隆远程的仓库,服务器也可以配置在启动的时候克隆仓库,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: http://git/team-a/config-repo.git

  在上面的例子team-a的仓库将在服务端启动时进行克隆,其他的仓库将在第一次请求时克隆。

  认证

  如果远程的git仓库需要用户名和密码,可以参照下面的例子

1
2
3
4
5
6
7
8
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword

  到此,Spring Cloud Config服务端就介绍到这里,还有一些不常用的功能在这里就不介绍了,大家可以参照spring cloud官网。Spring Cloud Config服务端的代码示例可以参照我的GitHub地址:https://github.com/bigbugliu/spring-cloud-config-server。

三、Spring Cloud Config 客户端

  Spring Boot应用可以立即使用Spring Config Server。只要在classpath中有Spring Cloud Config Client的jar包,这个应用就会请求配置的服务端。他将使用绑定的配置服务器(spring.cloud.config.uri中配置的)的属性初始化spring环境。

  在某些情况下,如果服务无法连接到配置服务器,则可能希望启动服务失败。如果这是所需的行为,请设置引导配置属性spring.cloud.config.failFast=true,客户端将以异常停止。

  如果您希望配置服务器在您的应用程序启动时可能偶尔不可用,您可以要求它在发生故障后继续尝试。首先,您需要设置spring.cloud.config.failFast=true,然后您需要将spring-retry和spring-boot-starter-aop添加到您的类路径中。默认行为是重试6次,初始退避间隔为1000ms,指数乘数为1.1,用于后续退避。您可以使用spring.cloud.config.retry.*配置属性配置这些属性(和其他)。

每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

Spring Cloud Config(配置中心)的更多相关文章

  1. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  2. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  3. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  6. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  7. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

  8. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

  9. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  10. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

随机推荐

  1. 表单时间和定时器this的指向

    1.针对表单的 form 表单  input 输入框 select 下拉列表  textarea 文本域 type 类型 radio 单选框 checkbox 多选框 password 密码框 but ...

  2. Http(s)与后台交互方式

    前言 Http(s)是前后端交互的主要方式之一,交互技术主要有:Ajax(XMLHttpRequest).Fetch.地址跳转(window.open.location.href).Http(s)与后 ...

  3. js 时间戳转换为‘yyyy-MM-dd hh:mm’格式(es6语法)

    function formatDate(date,fmt) { if(/(y+)/.test(fmt)){ fmt = fmt.replace(RegExp.$1,(date.getFullYear( ...

  4. js 两数组去除重复数值

    //两数组去除重复数值 mergeArray: function(arr1, arr2) { for (var i = 0; i < arr1.length; i++) { for (var j ...

  5. 如何用JavaScript判断dom是否有存在某class的值?

    例如: <html class="no-js"> <head> </head> <body> </body> </ ...

  6. 2018-08-16 中文代码之Spring Boot添加基本日志

    之前中文代码之Spring Boot实现简单REST服务的演示服务不知为何中止. 新开issue: 演示服务中止 · Issue #2 · program-in-chinese/programming ...

  7. 2018-08-13 中文编程讨论组(GitHub)社区守则一周年修订

    原址在此 社区守则 大原则 求同存异 就事论事 己所不欲勿施于人 注: 在讨论组成立一周年之际, 对行为规范进行一些细化 内 这部分所有内容同样适用于对外 回归技术 所有与中文编程没有直接关系的话题都 ...

  8. 【读书笔记】iOS-更新项目前要注意的事情

    在进行永久更改项目的任何现代化操作之前,要问自己几个问题. 1,我还需要返回项目的旧代码吗? 2,我的同事中有没有人无法升级到最新版本的Xcode? 3,  如果我使用了最新的功能,会不会减少用户? ...

  9. FUNCTIONALITY OF ITEM CATEGORY

    Item Category Purpose This wiki page will breify discuss about functionality of Item Category in SAP ...

  10. The value of ESP was not properly saved across a function call 快速解决

    The value of ESP was not properly...快速解决 今天遇到这个问题,真的是非常头疼,期间电脑居然崩掉一次.所以,分享一下解决办法. 如果是:类定义的时候,新添加了属性, ...