Learn from me Spring Cloud (Finchley version)-20-Spring Cloud Config-Git warehouse configuration details

Learn from me Spring Cloud (Finchley version)-20-Spring Cloud Config-Git warehouse configuration details

This section discusses in detail how to configure a Git repository.

1. Placeholder support

The placeholder of Config Server supports {application}, {profile} and {label}.

Example:

server:
  port: 8080
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/itmuch/{application}
          username:
          password:

In this way, you can easily support one Git repository for one application. Similarly, it can also support a profile corresponding to a Git repository.

2. pattern matching

Pattern matching refers to a list of {application}/{profile} names with wildcards. If {application}/{profile} does not match any pattern, it will use the spring.cloud.config.server.git.uridefined URI.

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

In this example, for the simple warehouse, it only matches the application named simple in all configuration files, and its mode is equivalent to simple/*. The local repository matches the names of all applications starting with local in all configuration files.

3. search the catalog

In many scenarios, we may put the configuration file in a subdirectory of the Git repository. At this time, you can use search-paths to specify it, and search-path also supports placeholders.

spring:
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/itmuch/spring-cloud-config-repo
          search-paths: foo,bar*

In this way, Config Server will look for configuration files in the Git repository root directory, the foo subdirectory, and all subdirectories starting with bar.

4. load the configuration file at startup

By default, Config Server will only clone the Git repository when the configuration is requested for the first time. We can also make Config Server clone the Git repository when it starts, for example.

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            team-a:
                pattern: microservice-*
                clone-on-start: true
                uri: http://git.oschina.net/itmuch/spring-cloud-config-repo

The property spring.cloud.config.server.git.repos.*.clone-on-startis set to true, you can make Config Server clone designated Git repository at startup.

Of course, also be used spring.cloud.config.server.git.clone-on-start=truefor global configuration.

Configure clone-on-start = true, which can help Config Server quickly identify the wrong configuration source (for example, invalid Git repository) when it starts.

Tips

Set the log level of the following packages to DEBUG to print the details of the Git repository requested by the Config Server. We can use the log to better understand the Git warehouse configuration of Config Server, and at the same time, it is also convenient for us to quickly locate the problem.

logging:
  level:
    org.springframework.cloud: DEBUG
    org.springframework.boot: DEBUG
Reference: https://cloud.tencent.com/developer/article/1460861 Follow Me Spring Cloud (Finchley Edition)-20-Spring Cloud Config-Git Warehouse Configuration Detailed Explanation-Cloud + Community-Tencent Cloud