Spring Cloud Config 使用说明(1) -- 快速开始

2.2.4.RELEASE

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring 的Environment和PropertySource的抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时运行需要的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。

快速开始

本节快速说明spring cloud config 的服务端和客户端的使用。

下载服务器示例代码

首先,启动服务器,如下:

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

服务端是一个spring boot应用,因此也可以在IDE中启动(main class是ConfigServerApplication)。

然后试一下客户端,如下:

$ curl localhost:8888/foo/development
{"name":"foo","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格式发布。

该HTTP服务中具有以下路径的资源:

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

其中“application”是SpringApplication中的spring.config.name(在常规的Spring Boot应用程序中通常为“application”),“profile”是活动的配置文件(或逗号分隔的列表),“label”是可选的git标签(默认为“master”)。

Spring Cloud Config服务器可以从多种数据源中拉取配置。下面的例子是从git服务器获取配置(必须提供):

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

也可以从jdbc数据库、Subversion、Hashicorp Vault、 Credhub和本地文件中获取配置

客户端使用

要使用这些功能,你可以新建一个依赖于spring-cloud-config-client 库的Spring Boot应用。最方便添加依赖的方式是添加库org.springframework.cloud:spring-cloud-starter-config。Maven用户或使用Gradle和Spring CLI的Spring IO版本管理属性文件的用户也 可以 继承 spring-cloud-starter-parent。

pom.xml

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>{spring-boot-docs-version}</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>{spring-cloud-version}</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程序, 比如下面的http服务器:

@SpringBootApplication
@RestController
public class Application {

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

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

}

上面的http服务器运行的时候,会默认从本地8888端口的配置服务器中拉取外部配置。如果要改变配置服务器的地址,你可以修改bootstrap.properties文件(类似于application.properties,但作用于应用程序的启动阶段)中的配置中心地址属性,如:

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

默认情况下,如果没设置应用程序名,会使用“application”。要改变名字,可以在bootstrap.properties文件中添加配置

spring.application.name: myapp

设置应用名称的时候,不要使用“application-”开头,防止解释属性源的时候出错。

bootstrap属性可以在/env接口中获取,是一个高优先级的属性源,如下:

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

一个叫configService:/的属性源,包含一个名叫foo、值为bar的属性,同时它是最高优先级的

上面的属性源中URL是指git服务器地址,不是配置中心的地址

Spring Cloud Config 使用说明(1) – 快速开始
Spring Cloud Config 配置中心使用说明(2) – 服务器+git库
Spring Cloud Config 配置中心使用说明(3) – 服务器+文件存储
Spring Cloud Config 配置中心使用说明(4) – 服务器+数据库存储
Spring Cloud Config 配置中心使用说明(5) – 服务器健康与安全配置
Spring Cloud Config 配置中心使用说明(6) – 服务器 配置文件格式
Spring Cloud Config 配置中心使用说明(7) – 服务器 配置变更通知
Spring Cloud Config 配置中心使用说明(8) – 客户端

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值