SPC-Config 笔记

Spring Cloud Config
在分布式系统中为服务端和客户端提供扩展配置信息。
我们有一个集中的位置来跨所有环境管理应用程序的外部属性。
客户端和服务器映射到Spring的Environment和PropertySource抽象,所以对Spring应用程序来说非常契合。
在我们从开发到测试再到生产部署整个应用程序开发管道中,我们可以利用它来管理各个阶段的环境配置。
默认的服务后端存储是使用的git,所以能很容易的标签化配置版本管理。被大范围的工具访问。
还可以很容易的通过Spring的配置来实现添加替换实现和插件。

$ cd spring-cloud-config-server
$ ../mvnw spring-boot:run

$ curl localhost:8888/foo/development
{"name":"development","label":"master","propertySources":[
  {"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},
  {"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
]}

默认的定位属性源策略是复制一个git仓库(spring.cloud.config.server.git.uri) 用它来初始化一个最小配置的SpringApplication。
这个最小配置的应用程序的Environment 可以如下形式访问
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

这里的application 会注入spring.config.name的值, profile是活动目录,label是可选的git标签(默认为master)

Spring Cloud Config Server从一个git仓库为一个远程客户端拉配置信息。
spring:
 cloud:
  config:
   server:
    git:
     uri:https://github.com/spring-cloud-samples/config-repo
     
     
     
客户端如何使用:
要在一个应用程序中使用这些,只需要创建一个Spring Boot应用程序然后添加spring-cloud-config-client依赖:
org.springframework.cloud:spring-cloud-starter-config

spring-cloud-starter-parent

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-start-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/><!-- lookup parent from repository-->
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<!-- repositories also needed for snapshots and milestones -->


Application.java

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

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

Spring Cloud Config Server
为外部配置(名值对)提供基于资源的HTTP API,该服务可以使用@EnableConfigServer声明很容易的被嵌入到一个Spring Boot 应用程序中。
ConfigServer.java

@SpringBootApplication
@EnableConfigServer
public class ConfigServer{
    public static void main(String[] args){
        SpringApplication.run(Configuration.class,args);
    }
}
该服务通过ConfigServer Jar包里的configserver.yml文件的spring.config.name=configserver设置了默认的配置仓库。
另外我们可以应用自己的application.properties文件设置
因为所有的Spring Boot Application默认运行在8080 端口,我们可以通过application.properties文件中设置修改它。
server.port:8888
spring.cloud.config.server.git.uri: file://${user.home}/config-repo

这里的${user.home}/config-repo 是一个包含YAML和Properties文件的git仓库
如果是Windows系统下,需要写成:  file:///${user.home}/config-repo

Git库创建:

$ cd $HOME
$ mkdir config-repo
$ cd config-repo
$ git init .
$ echo info.foo: bar > application.properties
$ git add -A .
$ git commit -m "Add application.properties"

Environment Repository环境仓库
我们需要将配置服务器的配置数据放到哪里呢?
处理这个问题的是EnvionmentRepository,围绕着Environment对象操作。
Environment对象是对Spring Environment(包括propertySource作为主要内容)的浅拷贝。
Environment资源被参数化为三个变量:
{application} 映射到客户端的 spring.application.name
{profile} 映射到客户端的spring.profile.active 通常是以逗号分开的列表
{label}这是服务端的内容标签一个版本化的配置文件设置。

Repository实现的通用功能跟一个Spring Boot 应用程序一样,从spring.config.name({application}参数),spring.profile.active({profiles}参数)加载配置文件.
优先级规则跟标准的Spring Boot 应用程序一样,活动Profile比默认的优先级更高,如果有多个profiles,则最后一个被使用。

bootstrap.yml

spring:
 application:
  name: foo
 profiles:
  active:dev,mysql
  

如果仓库是基于文件的,服务会从application.yml和foo.yml来创建Environment。
如果YAML文件有文档在里面指向Spring Profiles,那么它们有更高的优先级。(依照profiles的排列顺序递增)

Git 后端:
EnvironmentRepository 默认是使用Git后端实现的。非常方便的管理升级和物理环境,以及审计变化。
我们可以通过Config Server的application.yml文件里 用spring.cloud.config.server.git.uri 配置属性来修改仓库位置。
如果我们使用file:前缀,将会从本地仓库获取。
最好使用ssh协议来共享文件仓库,这样服务可以克隆它使用本地网络来拷贝一个缓存。

Repository实现了映射HTTP资源的{label}参数到一个git label(提交id,branch名,或者标签)
如果git branch或者标签名含有斜杠(“/”),则HTTP URL中应该使用下划线替代。
比如 foo/bar , foo_bar

Git URI中的占位符:
支持git仓库URL使用占位符,比如{application},{profile},{label}等。
所以很容易实现一个应用程序对应一个仓库。或者使用{profile}实现一个目录对应一个仓库。
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/{application}


模式匹配和多仓库:
支持模式匹配application和profile名,格式是{application}/{profile}

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值。
repo的pattern属性实际上是一个数组,所以我们可以使用YAML数组([0],[1])绑定多个模式:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            development:
              pattern:
                - */development
                - */staging
              uri: https://github.com/development/config-repo
            staging:
              pattern:
                - */qa
                - */production
              uri: https://github.com/staging/config-repo
              
              
每个仓库还可以可选的保存配置文件到一个子目录,用searchPaths指定模式查询的目录。
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*
          
这里服务器会从顶层目录和foo/子目录以及名字中含有bar的所有子目录中查找配置文件。

默认情况下,服务会在第一次请求配置文件时拷贝远程仓库。服务器也可以通过配置使其在启动时拷贝。
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

身份验证:
使用HTTP基础身份验证,需要设置username和password
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword

如果我们不适用HTTPS和用户信用凭证,我们需要将uri执行一个SSH位置("git@github.com:configuration/cloud-configuration")
对于Git Server来说在~/.ssh/known_hosts文件中入口,是ssh-rsa格式。


文件系统后端:
从本地classpath或者文件系统中(spring.cloud.server.native.searchLocations 静态URL)
要是用native profile只需要设置Config Server的 spring.profiles.active=native

file:///${user.home}/config-repo

默认的搜寻目录:
[classpath:/, classpath:/config, file:./, file:./config]


组合多个Environment仓库
有时候我们需要从多个环境仓库中获取配置数据,只需要在Config Server的 application.properties  或者 YAML文件中,启用多profiles即可。
spring:
  profiles:
    active: git, svn
  cloud:
    config:
      server:
        svn:
          uri: file:///path/to/svn/repo
          order: 2
        git:
          uri: file:///path/to/git/repo
          order: 1        

转载于:https://my.oschina.net/u/924064/blog/900151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值