Spring Cloud 配置服务器

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 仓库仅用于测试。在生产环境中,您应该使用服务器来托管您的配置仓库。
如果您的配置仓库只包含文本文件,则其初始克隆可以快速高效。如果您存储二进制文件,特别是大型文件,则在首次请求配置时可能会遇到延迟,或者在服务器中遇到内存不足错误。
© . This site is unofficial and not affiliated with VMware.