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
所以基本上这里发生的事情是 - 只要您的应用程序最初能够连接到您在 spring.cloud.config.server.git.uri = https://your-git/config-repo.git 中设置的 git 存储库,然后在 config-server/container 启动时,您在您的spring.cloud.config.server.git.basedir 在本地创建,默认情况下 spring-cloud 将您的配置克隆到此目录中以作为备用。
因此,每当您的 git 存储库无法访问时,spring-cloud 都会从该基本目录中获取您的配置。
需要注意的重要事项:
除非您真的想仅在 config-server 启动时重新克隆 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 倾向于将 repo 克隆到备用目录中作为/your/config/local/fallback/directory/config-repo。您必须将config-repo 的所有内容(包括.git 文件夹)复制出来并直接复制到/your/config/local/fallback/directory 中
第一次或任何时候启动配置服务器(或其容器)! ......... 瞧!!