我们计划将 Spring Cloud Config 用于我们的服务。我们最大的担忧是当容器启动时,它依赖于 github 一直可用,以便它可以拉取配置文件。如果 github 关闭,缓解该问题的最佳实践是什么?
我正在考虑将配置的本地文件夹存储为备份并将 application.yml 配置为回退到它(我不知道如何)。
我打算使用复合环境存储库
请看这里:Section 2.1.8
然而它指出:
Any type of failure when retrieving values from an environment repository results in a failure for the entire composite environment.
这意味着如果 git retrieve 失败,它不会回退到组合的本地组件。我希望它做到了。有没有人处理过类似的问题?你是怎么解决的?
这是一篇关于最佳实践的好文章。但是,我需要针对案例 1 的解决方法:Best practices on handling GIT repository inavailability
最佳答案
Spring-Cloud 有一个配置属性来处理这个问题;spring.cloud.config.server.git.basedir = /your/config/local/fallback/directory
NOTE - If you're using a
.yml
file, then define the above property as per yaml conventions.
要了解背景知识,请查看文档: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
或者完全没有设置 - 否则,每次重新启动 cloud-config 服务时,配置将被删除并重新克隆,如果当时存储库不可用,应用程序启动将失败 - 你可能不想要那个 .但是,如果
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 repo 在启动期间是否无法访问),也可以作为后备使用,您可以执行以下步骤;spring.cloud.config.server.git.basedir
cd /your/config/local/fallback/directory
git clone https://your-git/config-repo.git
虽然 repo 可用 .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
关于spring - 如果远程存储库不可用,如何为 Spring Cloud Config 服务设置本地后备配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52486278/