zoukankan      html  css  js  c++  java
  • Spring Cloud Config(配置中心)

    每天学习一点点 编程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

  • 相关阅读:
    java任务调度之Timer定时器
    springboot 启动的时候报java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    Spring 体系结构
    为什么MySQL数据库要用B+树存储索引?
    Nginx反向代理服务器的安装与配置
    详细的最新版fastdfs单机版搭建
    FastDFS 分布式文件系统(部署和运维)
    linux
    Spring Cloud底层原理
    Spring中ioc的实现原理
  • 原文地址:https://www.cnblogs.com/scode2/p/8819953.html
Copyright © 2011-2022 走看看