Learnitweb

Reading configurations from the Git in Spring Cloud Config Server

1. Introduction

In this tutorial we’ll discuss reading configurations from the Git in Spring Cloud Config Server.

This is the default implementation. Using Git makes it very convenient for managing upgrades and physical environments and auditing changes done over time.

You can configure the location of the repository in your Config server configuration (for example in application.yml) using the property spring.cloud.config.server.git.uri. If you set it with a file: prefix then it should work with a local repository.

2. Repostory location

You can set the repository location using the spring.cloud.config.server.git.uri property.

3. Skipping SSL Certificate Validation

The configuration server’s validation of the Git server’s SSL certificate can be disabled by setting the spring.cloud.config.server.git.skipSslValidation property to ‘true’. The default value is false.

4. Setting HTTP Connection Timeout

You can configure the duration, in seconds, that the configuration server will wait to establish an HTTP connection using the spring.cloud.config.server.git.timeout property. The default value is 5.

5. Placeholders in Git URI

Spring Cloud Config Server supports a git repository URL with placeholders for the {application} and {profile} and {label}.

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myapp/{application}

6. Search Paths

Every repository can also optionally store config files in sub-directories, and patterns to search for those directories can be specified as search-paths. Here is an example:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          search-paths:
            - someDir
            - conf*

7. Clone repositories at startup

By default, the server clones remote repositories when configuration is first requested. The server can be configured to clone the repositories at startup. Here is an example:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            repo-a:
                pattern: repo-a-*
                cloneOnStart: true
                uri: https://git/team-a/config-repo.git

In the preceding example, the server clones repo-a’s config-repo on startup, before it accepts any requests. All other repositories are not cloned until configuration from the repository is requested.

Setting this property helps identifying a misconfigured configuration source while the server is starting up.

8. Force pull in Git Repositories

You can make the Spring Cloud Config Server force pull from the remote repository if the local copy is dirty. The default value for force-pull property is false. Here is an example:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-config-samples/config-repo
          force-pull: true

9. Deleting untracked branches in Git Repositories

To maintain a clean and up-to-date local repository, you can set the deleteUntrackedBranches property. It will make Spring Cloud Config Server force delete untracked branches from local repository. For example:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-config-samples/config-repo
          deleteUntrackedBranches: true

10. Git Refresh Rate

You can manage the frequency at which the config server retrieves updated configuration data from your Git backend by setting the spring.cloud.config.server.git.refreshRate property. This value is defined in seconds. By default, it is set to 0, which means the config server will fetch the latest configuration from the Git repository every time a request is made.

11. Default Label

The default label for Git is main. If you do not configure the spring.cloud.config.server.git.defaultLabel property and a branch named main does not exist, the config server will attempt to check out a branch named master by default. To disable this fallback behavior, you can set spring.cloud.config.server.git.tryMasterBranch to false.

12. Accessing Backends Through a Proxy

The table below outlines the proxy configuration properties for both HTTP and HTTPS proxies. All of these properties should be prefixed with either proxy.http or proxy.https.

Property NameRemarks
hostThe host of the proxy.
portThe port with which to access the proxy.
nonProxyHostsAny hosts which the configuration server should access outside the proxy. If values are provided for both proxy.http.nonProxyHosts and proxy.https.nonProxyHosts, the proxy.http value will be used.
usernameThe username with which to authenticate to the proxy. If values are provided for both proxy.http.username and proxy.https.username, the proxy.http value will be used.
passwordThe password with which to authenticate to the proxy. If values are provided for both proxy.http.password and proxy.https.password, the proxy.http value will be used.

Here is an example:

spring:
  profiles:
    active: git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-config-samples/config-repo
          proxy:
            https:
              host: my-proxy.host.com
              password: myproxypassword
              port: '3128'
              username: myproxyusername
              nonProxyHosts: test.com

13. Example

We have updated our earlier example to work with Git. Here is the application.yml file in our Spring Config Server.

server:
  port: 8085
spring:
  application:
    name: configserver
  profiles:
    active: git
  cloud:
    config:
      server:
        git:
          uri: "https://github.com/learnitweb/spring-cloud-config.git"
          default-label: main
          timeout: 5
          clone-on-start: true
          force-pull: true

Here is the property file of Spring Config Client.

spring:
  application:
    name: service1
  profiles:
    active: prod
  config:
    import: "optional:configserver:http://localhost:8085?fail-fast=true&max-attempts=10&max-interval=1500&multiplier=1.2&initial-interval=1100"

14. Conclusion

In this tutorial, we explored how to read configurations from a Git repository using Spring Cloud Config Server. We covered the essential configuration properties and their usage, including setting the refresh rate, default branch labels, and proxy settings. By leveraging these features, you can ensure that your application configurations are consistently managed and easily updated from a central Git repository, enhancing the maintainability and scalability of your distributed systems.