此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Cloud Config 4.1.4! |
Spring Cloud 配置服务器
Spring Cloud Config Server 为外部配置(名称-值对或等效的 YAML 内容)提供了基于 HTTP 资源的 API。
服务器可嵌入到 Spring Boot 应用程序中,方法是使用@EnableConfigServer
注解。
因此,以下应用程序是配置服务器:
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
(有一个configserver.yml
在 Config Server jar 中)。
另一种是使用你自己的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 存储库仅用于测试。 您应该使用 服务器在生产环境中托管配置存储库。 |
如果您只在配置存储库中保留文本文件,则配置存储库的初始克隆可以快速高效。 如果存储二进制文件,尤其是大型文件,则可能会在第一次配置请求时遇到延迟,或者在服务器中遇到内存不足错误。 |