Spring Cloud Config Server
Spring Cloud Config Server 为外部配置(名称-值对或等效 YAML 内容)提供基于 HTTP 资源的 API。服务器可以通过使用 @EnableConfigServer
注解嵌入到 Spring Boot 应用中。因此,以下应用就是一个 Config Server
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 和 properties 文件的 git 仓库。
在 Windows 上,如果文件 URL 是带有驱动器前缀的绝对路径(例如,/${user.home}/config-repo ),则需要在文件 URL 中多加一个 "/"。 |
以下列表展示了创建上例中 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 仓库仅用于测试目的。在生产环境中,你应该使用服务器来托管配置仓库。 |
如果只在配置仓库中保留文本文件,则首次克隆会非常快速高效。如果你存储了二进制文件,特别是大型文件,则首次请求配置时可能会遇到延迟,或者在服务器中遇到内存不足错误。 |