Spring Cloud Config Server
Spring Cloud Config Server 为外部配置(名称-值对或等效 YAML 内容)提供了基于 HTTP 资源的 API。通过使用 @EnableConfigServer
注释,可以在 Spring Boot 应用程序中嵌入服务器。因此,以下应用程序是配置服务器:
Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content).
The server is embeddable in a Spring Boot application, by using the @EnableConfigServer
annotation.
Consequently, the following application is a config server:
@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
,如下面的示例所示:
Like all Spring Boot applications, it runs on port 8080 by default, but you can switch it to the more conventional port 8888 in various ways.
The easiest, which also sets a default configuration repository, is by launching it with spring.config.name=configserver
(there is a configserver.yml
in the Config Server jar).
Another is to use your own application.properties
, as shown in the following example:
server.port: 8888
spring.cloud.config.server.git.uri: file://${user.home}/config-repo
其中 ${user.home}/config-repo
是包含 YAML 和属性文件的 git 存储库。
where ${user.home}/config-repo
is a git repository containing YAML and properties files.
在 Windows 上,如果文件 URL 是带有驱动器前缀的绝对路径,则需要在其中添加一个额外的“/”(例如, |
On Windows, you need an extra "/" in the file URL if it is absolute with a drive prefix (for example, |
以下清单显示了在上一个示例中创建 git 存储库的秘诀: The following listing shows a recipe for creating the git repository in the preceding example: $ 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 存储库是仅用于测试。您应该使用服务器在生产中托管您的配置文件存储库。
Using the local filesystem for your git repository is intended for testing only. You should use a server to host your configuration repositories in production.
如果您只在配置文件存储库中保留文本文件,则其初始克隆会快速高效。如果您存储二进制文件,尤其是较大的文件,则可能会在首次请求配置时遇到延迟,或在服务器中遇到内存不足错误。
The initial clone of your configuration repository can be quick and efficient if you keep only text files in it. If you store binary files, especially large ones, you may experience delays on the first request for configuration or encounter out of memory errors in the server.