Spring Cloud exploration | distributed configuration center (Config Client)

Posted by fareforce on Sun, 09 Feb 2020 16:41:09 +0100

The last chapter Spring Cloud exploration | distributed configuration center (Config Server) >, and then explain the configuration process of Config Client.

Spring Cloud Config Client configuration process

1. pom.xml add dependency
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencies>
    <!-- Config Client Dependence -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!-- For our own monitoring of application information -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- Act as web The application starts. If it is not added, it will fail to start, and endpoints Function will not work -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
2. Add configuration information

application.yml

server:
  port: 8080 # Specify listening port

# Load all endpoints. Only info / health is loaded by default
management:
  endpoints:
    web:
      exposure:
        include: "*"

bootstrap.yml

spring:
  application:
    name : configClient01
  cloud:
    config:
      profile: test
      label: master
      uri: http://localhost:8090

Configuration explanation:

  • spring.cloud.config. *: by default, Config Server will provide the property source from / {name}/{profile}/{label}. The default binding in Config Client is:
    • "name" = ${spring.application.name}
    • "profile" = ${spring.profiles.active} (actually Environment.getActiveProfiles())
    • "label" = "master"
      However, all of this can be overridden by setting spring.cloud.config. * (where * is "name", "profile" or "label"). (we use this method here)

Label is very useful for the configuration of rolling back to the previous version; when using the default Config Server implementation, the label can be git label, branch name or commit id. The label can also be provided as a comma separated list, in which case the items in the list will try one by one until one succeeds. For example, when developing on a feature branch, you might want to map the label to the branch, but set it to optional (for example, spring.cloud.config.label = myfeature, development).

  • spring.cloud.config.uri: address of Config Server;
  • spring.application.name: configures the application name (used to identify the application when there is a registry). It is not useful here (it is also overwritten by the top);
3. Modify Config Server configuration

You only need to modify search paths. Why do you do this< Spring Cloud exploration | distributed configuration center (Config Server)>

spring:
  cloud:
    config:
      server:
        git:
          search-paths : '{application}'
4. Conduct the test

To be continued......

Topics: Spring Java git xml