如果远程仓库不可用,如何为 Spring Cloud Config 服务设置本地后备配置?

2023-12-31

我们计划在我们的服务中使用 Spring Cloud Config。我们最担心的是,当容器启动时,它依赖 github 始终可用,以便它可以拉取配置文件。如果 github 宕机,缓解该问题的最佳实践是什么?

我正在考虑存储配置的本地文件夹作为备份,并配置 application.yml 以回退到它(我不知道如何)。

我打算使用复合环境存储库 请看这里:第2.1.8节 https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

然而它指出:

从环境存储库检索值时的任何类型的失败都会导致整个复合环境失败。

这意味着如果 git 检索失败,它不会回退到组合的本地组件。我希望如此。你们中有人处理过类似的问题吗?你是怎么解决的?

这是一篇关于最佳实践的好文章。但是,我需要针对情况 1 的解决方法:处理 GIT 存储库不可用的最佳实践 https://github.com/spring-cloud/spring-cloud-config/issues/527


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 就会从此基本目录中获取你的配置。


重要注意事项:

除非您确实只想在配置服务器启动时重新克隆 g​​it 配置,否则请确保该属性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 存储库在启动期间是否无法访问),您可以执行以下步骤;

  1. 手动创建spring.cloud.config.server.git.basedir
  2. 从您的终端cd /your/config/local/fallback/directory
  3. git clone https://your-git/config-repo.git 当回购可用时
  4. 确保您的所有配置文件/文件夹/子文件夹,包括.git文件夹直接克隆到后备目录的根目录。

    例如,有一种倾向,git clone https://your-git/config-repo.git将把存储库克隆到后备目录中,如下所示/your/config/local/fallback/directory/config-repo。你必须复制每一个该死的内容config-repo- 包括.git文件夹也 - 出来并直接进入/your/config/local/fallback/directory

  5. 第一次启动配置服务器(或其容器)或每当! …………Voila!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果远程仓库不可用,如何为 Spring Cloud Config 服务设置本地后备配置? 的相关文章

随机推荐