Spring-Cloud 有一个配置属性来处理这个问题;
spring.cloud.config.server.git.basedir = /your/config/local/fallback/directory
注意 - 如果您使用的是.yml
文件,然后将上述属性定义为
根据 yaml 约定。
要了解背景知识,请查看文档:http://cloud.spring.io/spring-cloud-static/Finchley.RC1/single/spring-cloud.html#_version_control_backend_filesystem_use http://cloud.spring.io/spring-cloud-static/Finchley.RC1/single/spring-cloud.html#_version_control_backend_filesystem_use
所以本质上这里发生的是 - 只要你的应用程序最初能够连接到你设置的 git 存储库spring.cloud.config.server.git.uri = https://your-git/config-repo.git
,然后在 config-server/container 启动时,您在中定义的目录spring.cloud.config.server.git.basedir
在本地创建,默认情况下 spring-cloud 将您的配置克隆到此目录中以用作后备。
因此,每当你的 git 存储库无法访问时,spring-cloud 就会从此基本目录中获取你的配置。
重要注意事项:
除非您确实只想在配置服务器启动时重新克隆 git 配置,否则请确保该属性spring.cloud.config.server.git.clone-on-start
未设置为true
或者完全没有设置 - 否则,每次重新启动云配置服务时,配置都会被删除并再次重新克隆,如果存储库当时不可用,应用程序启动将失败 -你也许不想要这样.
然而,如果spring.cloud.config.server.git.clone-on-start
被设定为false
或者甚至根本没有设置(在这种情况下,默认值是false
),那么git仓库只会被克隆一经请求- 因此,如果存储库无法访问,spring-cloud 将优雅地回退以从存储库中获取配置spring.cloud.config.server.git.basedir
即使应用程序配置服务器(或其容器)重新启动并且无法访问 git 存储库,您也会看到如下所示的内容;
No custom http config found for URL: https://your-git/config-repo.git/info/refs?service=git-upload-pack
... s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3a26f314: startup date [Mon Oct 15 22:01:34 EDT 2018]; root of context hierarchy
... o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/your/config/local/fallback/directory/application.properties
注意这一行:
Adding property source:file:/your/config/local/fallback/directory/application.properties
这就是奇迹发生的地方。
所以如果你想要spring.cloud.config.server.git.basedir
要在首次启动配置服务器之前就可以用作后备(无论您的 git 存储库在启动期间是否无法访问),您可以执行以下步骤;
- 手动创建
spring.cloud.config.server.git.basedir
- 从您的终端
cd /your/config/local/fallback/directory
-
git clone https://your-git/config-repo.git
当回购可用时
-
确保您的所有配置文件/文件夹/子文件夹,包括.git
文件夹直接克隆到后备目录的根目录。
例如,有一种倾向,git clone https://your-git/config-repo.git
将把存储库克隆到后备目录中,如下所示/your/config/local/fallback/directory/config-repo
。你必须复制每一个该死的内容config-repo
- 包括.git
文件夹也 - 出来并直接进入/your/config/local/fallback/directory
第一次启动配置服务器(或其容器)或每当! …………Voila!!