|
此版本仍在开发中,目前尚不稳定。如需最新稳定版本,请使用 Spring Cloud Config 5.0.1! |
Spring Cloud Config Server
Spring Cloud Config Server 提供了一个基于 HTTP 资源的 API,用于外部配置(键值对或等效的 YAML 内容)。
该服务器可通过使用 @EnableConfigServer 注解嵌入到 Spring Boot 应用程序中。
因此,以下应用程序是一个配置服务器:
ConfigServer.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
像所有 Spring Boot 应用程序一样,它默认在端口 8080 上运行,但您可以通过多种方式将其切换到更常见的端口 8888。
最简单的方法(同时还会设置一个默认配置仓库)是通过使用 spring.config.name=configserver 启动它(Config Server jar 中包含一个 configserver.yml)。
另一种方法是使用您自己的 application.properties,如下例所示:
application.properties
server.port: 8888
spring.cloud.config.server.git.uri: file://${user.home}/config-repo
其中 ${user.home}/config-repo 是一个包含 YAML 和属性文件的 Git 仓库。
在 Windows 上,如果文件 URL 是带有驱动器前缀的绝对路径(例如 /${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 仓库仅适用于测试目的。在生产环境中,您应使用服务器来托管配置仓库。 |
| 如果您的配置仓库中仅包含文本文件,初始克隆过程将快速高效。如果您存储了二进制文件,尤其是大型二进制文件,可能会在首次请求配置时遇到延迟,或导致服务器内存溢出错误。 |