其他分享
首页 > 其他分享> > 万一远程回购不可用,如何为Spring Cloud Config服务设置本地后备配置?

万一远程回购不可用,如何为Spring Cloud Config服务设置本地后备配置?

作者:互联网

我们计划将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检索失败,它就不会退回到组合的本地组件.我希望能做到.你们有没有处理过类似的问题?您是如何解决的?

这是一篇有关最佳做法的好文章.但是,我需要一种情况1:Best practices on handling GIT repository inavailability

解决方法:

Spring-Cloud具有配置属性来处理此问题.

spring.cloud.config.server.git.basedir = /您的/配置/本地/后备/目录

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,然后在配置服务器/容器启动时,您在spring.cloud.config.server.git.basedir中定义的目录在本地创建,默认情况下,spring-cloud将您的配置克隆到该目录中,以作为备用.

因此,每当您的git仓库无法访问时,spring-cloud都会从该基本目录中获取您的配置.

重要注意事项:

除非您真的只想仅在配置服务器启动时才重新克隆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

注意这一行:

添加属性源:文件:/your/config/local/fallback/directory/application.properties

那就是魔术发生的地方.

因此,如果您希望甚至在首次启动配置服务器之前(以及在启动过程中是否无法访问git repo)将spring.cloud.config.server.git.basedir作为后备设备使用,则可以随身携带采取以下步骤;

>手动创建spring.cloud.config.server.git.basedir
>从终端cd /您的/配置/本地/后备/目录
> 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中
>首次或任何时候启动配置服务器(或其容器)! ……瞧!

标签:fallback,spring-cloud-config,spring,github,local-storage
来源: https://codeday.me/bug/20191009/1882611.html