快速开始
此快速入门指南介绍了如何使用 Spring Cloud Config Server 的服务器端和客户端。
首先,启动服务器,如下所示:
$ cd spring-cloud-config-server $ ../mvnw spring-boot:run
服务器是一个 Spring Boot 应用程序,因此您可以选择在 IDE 中运行它(主类是 ConfigServerApplication)。
接下来尝试一个客户端,如下所示:
$ curl localhost:8888/foo/development
{
"name": "foo",
"profiles": [
"development"
]
....
"propertySources": [
{
"name": "https://github.com/spring-cloud-samples/config-repo/foo-development.properties",
"source": {
"bar": "spam",
"foo": "from foo development"
}
},
{
"name": "https://github.com/spring-cloud-samples/config-repo/foo.properties",
"source": {
"foo": "from foo props",
"democonfigclient.message": "hello spring io"
}
},
....
定位属性源的默认策略是克隆一个 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
例如:
curl localhost:8888/foo/development curl localhost:8888/foo/development/master curl localhost:8888/foo/development,db/master curl localhost:8888/foo-development.yml curl localhost:8888/foo-db.properties curl localhost:8888/master/foo-db.properties
其中 application 作为 spring.config.name 注入到 SpringApplication 中(这在常规 Spring Boot 应用中通常是 application),profile 是一个活动的配置文件(或以逗号分隔的多个配置文件列表),label 是一个可选的 Git 标签(默认为 master)。
Spring Cloud Config Server 从各种来源为远程客户端拉取配置。以下示例从 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 应用程序(例如,参见 config-client 的测试用例或示例应用程序)。
添加依赖项最便捷的方式是使用 Spring Boot Starters org.springframework.cloud:spring-cloud-starter-config。
对于 Maven 用户,还提供了一个父 POM 和 BOM (spring-cloud-starter-parent);对于 Gradle 和 Spring CLI 用户,则提供了一个 Spring IO 版本管理属性文件。以下示例展示了典型的 Maven 配置:
<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 端口获取外部配置。
要修改启动行为,您可以通过使用 application.properties 来更改配置服务器的位置,如下例所示:
spring.config.import=optional:configserver:http://myconfigserver.com
默认情况下,如果未设置应用名称,则将使用 application。要修改名称,请将以下属性添加到 application.properties 文件中:
spring.application.name: myapp
当设置属性 ${spring.application.name} 时,请勿在应用名称前添加保留字 application-,以避免解析正确的属性源时出现问题。 |
配置服务器的属性在 /env 端点中以高优先级属性源的形式显示,如下例所示。
$ curl localhost:8080/env
{
"activeProfiles": [],
{
"name": "servletContextInitParams",
"properties": {}
},
{
"name": "configserver:https://github.com/spring-cloud-samples/config-repo/foo.properties",
"properties": {
"foo": {
"value": "bar",
"origin": "Config Server https://github.com/spring-cloud-samples/config-repo/foo.properties:2:12"
}
}
},
...
}
一个名为 configserver:<URL of remote repository>/<file name> 的属性源包含 foo 属性,其值为 bar。
| 属性源名称中的URL是Git仓库,而不是配置服务器的URL。 |
如果您使用 Spring Cloud Config 客户端,需要设置 spring.config.import 属性才能绑定到配置服务器。您可在此处阅读更多相关内容:Spring Cloud Config 参考指南。 |