Spring Cloud技术指南--第二部分 Spring Cloud Config

Dalston.SR5
Spring Cloud Config 提供服务端和客户端用来支持在分布式系统中的集中配置管理。服务端作为配置中心通过不同的环境设置来管理应用的各种配置信息。这个概念在Spring中被抽象成EnvironmentPropertySource,因此它无缝集成Spring,而且适用于任何语言开发的任何应用。基于此将应用从开发环境转移到测试环境,再到生产环境,只需修改对应的环境设置即可,利于持续集成。它的默认存取是基于git的,因此它天然支持配置的版本管理。

4 快速开始

启动服务端:


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


服务端是一个Spring Boot应用,因此你可以直接右键主类(ConfigServerApplication)运行,使用curl测试:


$ 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环境使用JSON端点的方式来列举出属性资源:


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


一个标准的Spring Boot应用使用spring.config.name将应用配置注入到SpringApplication中取,profile代表当前配置文件(也可以是以逗号分隔的属性文件列表),label是一个可选的git标签(默认是master
Srping Cloud Config Server从远程git仓库拉去配置信息:


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

4.1 客户端使用

新建Spring Boot应用,添加spring-cloud-config-client依赖,非常方便的方法是添加org.springframework.cloud:spring-cloud-starter-config依赖,示例:
pom.xml

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

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

接下来创建标准Spring Boot应用,例如:

@SpringBootApplication
@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一样,但是早于application.properties解析加载)来修改它的配置信息和地址,例如:


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


访问/env:


$ curl localhost:8080/env

{
  "profiles":[],
  "configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
  "servletContextInitParams":{},
  "systemProperties":{...},
  ...
}

5 Spring Cloud Config Server 配置服务器

服务端提供基于HTTP的资源访问API访问配置信息,使用@EnableConfigServer注解使Spring Boot应用成为一个配置服务器,例如:

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}

Spring Boot应用默认是运行在8080端口,你可以修改它为8888,设置它的配置仓库,例如:


server.port:8888
spring.cloud.config.server.git.uri:file://${user.home}/config-repo


${user.home}/config-repo是一个存储YAML和properties文件的git仓库
注意:在window下如果是绝对路径开始的URL,需要在URL中添加“/”: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"

开发测试时使用本地git仓库,生产环境建议使用服务器的git仓库
纯文本文件的初始化拷贝很快,如果你存储的是二进制文件,而且很大,第一次请求有可能很慢,或者出现内存错误

5.1 Environment Repository 环境仓库

EnvironmentRepository用来操作存储Environment对象,这个Environment对象是Spring Environment的浅拷贝(包含有propertySources),Environment有三个参数:

  • {application}映射为客户端的spring.application.name
  • {profile}映射为客户端的spring.profiles.active
  • {label}类似于版本控制标签
    未完待续。。。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值