Spring Cloud 配置服务器
Spring Cloud 配置服务器提供基于 HTTP 资源的 API,用于外部配置(名称-值对或等效的 YAML 内容)。
服务器可嵌入 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在配置服务器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是绝对且带有驱动器前缀的,你需要在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 仓库仅用于测试。 你应该用服务器来托管你的配置仓库。 |
| 如果你只保留文本文件,配置仓库的初始克隆可以快速高效。 如果你存储二进制文件,尤其是大型文件,可能会在首次配置请求时遇到延迟,或者服务器内存不足时出现错误。 |