快速入门
本快速入门演练了使用 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) 并使用它来初始化一个 miniSpringApplication.
迷你应用程序的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(通常是什么application在常规的 Spring Boot 应用程序中),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 Startersorg.springframework.cloud:spring-cloud-starter-config.
还有一个父 pom 和 BOM (spring-cloud-starter-parent) 用于 Maven 用户,以及用于 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-以防止在解析正确的属性源时出现问题。 |
配置服务器属性显示在/envendpoint 作为高优先级属性源,如以下示例所示。
$ 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 参考指南中阅读更多相关信息。 |