Spring Cloud Config 伺服器

Spring Cloud Config Server 提供基於 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 啟動它(在 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 是絕對路徑且帶有磁碟機前綴,則需要在檔案 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 儲存庫僅適用於測試。在生產環境中,您應該使用伺服器來託管您的組態儲存庫。
如果您只在組態儲存庫中保留文字檔,則組態儲存庫的初始複製可以快速而有效率。如果您儲存二進位檔案,尤其是大型檔案,您可能會在第一次組態請求時遇到延遲,或在伺服器中遇到記憶體不足錯誤。