spring cloud:spring cloud config

spring cloud config在分布式系统中提供了服务端和客户端支持来提供外部化的配置。配置服务器有一个中心配置仓库,来管理应用程序所有环境的外部配置属性。spring cloud config很适合与Spring应用程序配合使用,可以被用于任何应用程序。随着应用程序通过部署管道从开发到测试到生产管理这些环境之间的配置,确保应用程序拥有他们所需要的一切运行时迁移。服务器的默认实现存储后端使用git,因此很容易支持标签版本的配置环境,同时也可以其他后端存储工具管理如文件系统和svn。

定位属性的默认策略是克隆来源git存储库(在“spring.cloud.config.server.git.uri”),并使用它来初始化一个迷你的SpringApplication。迷你应用程序环境中使用枚举属性来源和发布通过JSON端点。
HTTP服务资源的形式:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

“application”作为spring.application.name被注入到SpringApplication中,同时spring.application.name用作yml文件名称,
”profile”是一个激活的配置,或者是逗号分隔的属性列表,
“label”是一个可选的git版本,默认为master。

spring配置服务器将配置远程客户端从git存储库(必须提供):

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

1.客户端的使用

pom.xml配置:

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


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>1.0.1.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>

创建spring boot application:

@Configuration
@EnableAutoConfiguration
@RestController
public class Application {

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

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

}

当它运行它将接外部配置默认本地配置服务器在端口8888上运行。使用bootstrap.properties修改启动行为可以改变使用引导配置服务器的位置,比如application.properties是引导阶段的应用上下文,例如:

spring.cloud.config.uri: http://myconfigserver.com

下面引用/env端点显示的config配置信息:

$ curl localhost:8080/env
{
  "profiles":[],
  "configService:https://github.com/scratches/config-repo/bar.properties":{"foo":"bar"},
  "servletContextInitParams":{},
  "systemProperties":{...},
  ...
}

configService:<URL of remote repository>/<file name>包含一个属性foo,这个属性的值是bar,优先级是最高的。

2.服务端

使用@EnableConfigServer可以很容易的集成到spring boot应用里面,服务提供http和基于外部配置的资源,比如键值对或者yaml文件。

2.1 环境仓库

环境资源又三个变量参数化:
{application}在客户端使用spring.application.name映射
{profile}在客户端使用spring.active.profiles映射,多个用逗号分隔
{label}是服务端的版本标签

配置文件spring.profiles.active的优先级高于default,并且配置最有一个有效,就相当于把配置加到一个map中。

例如客户端bootstrap.yml配置:

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

2.2.git

EnvironmentRepository的默认实现使用Git,这是非常方便的对于管理升级和物理环境,审计的变化。改变仓库的位置你可以设置“spring.cloud.config.server.git.uri“配置属性配置服务器(例如application.yml)。配置服务高可用的话,多个配置服务器必须连接同一个git仓库。
spring cloud config server支持单个或多个git仓库:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/pattern1/config-repo
            special:
              pattern: pattern*,*pattern1*
              uri: https://github.com/pattern2/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

在上面的例子中,如果{application}不匹配的任何模式,它将使用默认spring.cloud.config.server.git.uri。对于simple库,匹配的是simple(它只匹配一个应用程序)。pattern format是一个以逗号分隔,可以使用通配符应用程序名称列表。


spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*

每个存储库还可以选择文件存储的子目录,可以使用searchpath搜索目录。例如上面的例子,会搜索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

配置服务可以配置启动时获取配置文件。例如上面的例子,服务器在启动时获取team-a的配置,其他的开机不克隆。

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword

可以配置git用户名和密码。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值