【问题标题】:How do I setup a local fallback config for Spring Cloud Config service incase remote repo is not available?如果远程 repo 不可用,如何为 Spring Cloud Config 服务设置本地回退配置?
【发布时间】:2019-02-28 09:00:26
【问题描述】:

我们计划将 Spring Cloud Config 用于我们的服务。我们最大的担心是,当容器启动时,它依赖于 github 始终可用,以便它可以拉取配置文件。如果 github 出现故障,缓解问题的最佳做法是什么?

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

我打算使用复合环境存储库 请看这里:Section 2.1.8

但是它说:

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

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

这是一篇关于最佳实践的好文章。但是,我需要针对案例 1 的解决方法:Best practices on handling GIT repository inavailability

【问题讨论】:

    标签: spring github local-storage spring-cloud-config fallback


    【解决方案1】:

    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 启动时重新克隆 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 倾向于将 repo 克隆到备用目录中作为/your/config/local/fallback/directory/config-repo。您必须将config-repo 的所有内容(包括.git 文件夹)复制出来并直接复制到/your/config/local/fallback/directory

    5. 第一次或任何时候启动配置服务器(或其容器)! ......... 瞧!!

    【讨论】:

    • 使用 Openshift / Kubernetes,我们在 /your/config/local/fallback/directory 添加一个卷,如果 git 现在不可用也没问题 :) 谢谢你的解决方案 :)
    猜你喜欢
    • 2015-10-28
    • 2019-08-11
    • 1970-01-01
    • 2020-07-10
    • 2020-07-09
    • 1970-01-01
    • 2020-04-22
    • 2017-01-08
    • 2020-08-10
    相关资源
    最近更新 更多