Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a local fallback config for Spring Cloud Config service incase remote repo is not available?

We are planning to utilize Spring Cloud Config for our service. Our biggest concern is that when the container starts up, it relies on github to be available all the time so that it can pull the config files. In case github is down, what is the best practice to mitigate the issue?

I was thinking of storing a local folder of the configs as a backup and configuring the application.yml to fallback to it (I do not know how).

I was going to use a Composite Environment Repositories Please see here: Section 2.1.8

However it states:

Any type of failure when retrieving values from an environment repository results in a failure for the entire composite environment.

This means if the git retrieve fails, it does not fall back to the local component of the composite. I wish it did. Have any of you handled a similar problem? How did you solve it?

Here is a good article about best practices. However, I need a workaround for case 1: Best practices on handling GIT repository inavailability

like image 676
Calvin Raveenthran Avatar asked Dec 04 '22 19:12

Calvin Raveenthran


1 Answers

Spring-Cloud has a configuration property to handle this issue;

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.

To have a background knowledge, look at the documentation: http://cloud.spring.io/spring-cloud-static/Finchley.RC1/single/spring-cloud.html#_version_control_backend_filesystem_use


So essentially what happens here is that - as long as your application was initially able to connect to the git repository which you set up in spring.cloud.config.server.git.uri = https://your-git/config-repo.git, then on config-server/container startup, the directory you have defined in your spring.cloud.config.server.git.basedir gets created locally and by default spring-cloud clones your configurations into this directory to be available as fallback.

So whenever your git repository is unreachable, spring-cloud will pick up your configurations from this base directory.


Important things to note:

Unless you really want to re-clone the git configurations only on config-server startup alone, ensure that the property spring.cloud.config.server.git.clone-on-start is NOT set to true or is entirely not set at all - Otherwise, every time you restart your cloud-config service the configurations will be deleted and freshly cloned again and if the repository is not available at the time, application startup will fail - and you perhaps don't want that.

However, if spring.cloud.config.server.git.clone-on-start is set to false or is not even set at all (in which case the default is false), then the git repository will only be cloned on demand - hence if the repository is unreachable, spring-cloud will fallback gracefully to pick up configurations from the spring.cloud.config.server.git.basedir

Even when the application config-server (or its container) is restarted and the git repository is not reachable, you will see something like below;

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

Notice the line:

Adding property source:file:/your/config/local/fallback/directory/application.properties

That's where the magic happens.


So if you want the spring.cloud.config.server.git.basedir to be available as a fallback even before the first startup of your config-server (and whether or not your git repo is unreachable during the startup), you can carry out the following steps;

  1. Manually create the spring.cloud.config.server.git.basedir
  2. From your terminal cd /your/config/local/fallback/directory
  3. git clone https://your-git/config-repo.git while the repo is available
  4. Ensure that all your config files/folders/sub-folders including the .git folder are cloned directly to the root of the fallback directory.

    For instance, there's a tendency that git clone https://your-git/config-repo.git will clone the repo into the fall back directory as /your/config/local/fallback/directory/config-repo. You will have to copy every darn content of config-repo - including the .git folder too - out and directly into /your/config/local/fallback/directory

  5. Start the config-server (or its container) for the first time or whenever! ......... Voila!!

like image 119
SourceVisor Avatar answered Dec 11 '22 10:12

SourceVisor