Setting up the spring cloud config unified configuration center

Keywords: Spring git Java xml

Configuration center


Create several configuration files on git, as shown in the figure above. The file naming rules are {project}-{profile}. For example, the project name corresponding to the config client dev file is config client, and the version is: dev.

spring cloud config server

pom.xml

<dependencies>
		<!--Specify project to web Mode start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
</dependencies>

application.yml

spring:
  application:
    name: cloud_config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/liu0829/CloudConfig.git
          username: ***  #git account
          password: ***  #git password
          default-label: dev #Default profile version
          search-paths: config #Directory of configuration file in git warehouse
server:
  port: 7060

ConfigApplication.java

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableConfigServer  //Key notes
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class,args);
    }
}

At this point, the server configuration is complete.

Check the server:

Start the server-side service and verify the configuration through http request. Spring Cloud Config has a set of access rules, which we can access directly on the browser.

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties



I tested the first one and the third one. I don't know why the second one doesn't work.

spring cloud config client

pom.xml

<dependencies>
		<!-- Specify project to web Mode start -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  </dependencies>

bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:7060   # Specify config provider path
      label: master				   # Specify the version of git repository
      profile: dev                 # The configuration file that specifies the environment as the dev environment

application.yml

server:
  port: 7061

spring:
  application:
    name: config-client
  profiles:
    active: dev

application-dev.yml

management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"
 
 ## Configure the default configuration parameters to avoid that no parameter error is found at startup. If @ Value("${data.env}") is used in the code to obtain parameters, and no configuration parameter is found, an error will be reported at startup. If you do not have this configuration, you do not need to configure the default parameters. This method is not recommended for configuration acquisition.
 data:
  env: NaN
  user:
    username: NaN
    password: NaN

There are two ways to get parameters:
GitConfig.java

@Data
@Component
public class GitConfig {
    @Value("${data.env}")
    private String env;
    @Value("${data.user.username}")
    private String username;
    @Value("${data.user.password}")
    private String password;
}

GitAutoRefreshConfig.java

@Component
@Data
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {
    @Data
    public static class UserInfo {
        private String username;

        private String password;
        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

    private String env;

    private UserInfo user;
}

Startup class:
ConfigClientApplication.java

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class,args);
    }
}

Write two test interfaces to obtain the parameters of the configuration center:
TestController.java

@RestController
@RefreshScope  
public class TestController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping(value = "show")
    public Object show(){
        return gitConfig;
    }

    @GetMapping(value = "autoShow")
    public Object autoShow(){
        return gitAutoRefreshConfig;
    }
}

Start the client service, test the interface:

Update profile

@RefreshScope annotation
At present, the client can successfully obtain the configuration parameters of the configuration center, but when the configuration parameters change, it needs to restart the client to obtain the latest parameters, which is obviously not desirable in normal operation projects. So @RefreshScope annotations are provided. The function of @RefreshScope is to re fetch the configuration parameters, add the annotation on the classes that need to get the configuration files, and then call the /actuator/refresh interface to refresh the configuration files.
The spring boot starter actor dependency and management configuration in this demo work for this purpose.
Update test:
Modify the contents of the configuration file on git, and then call the /actuator/refresh interface to get the latest configuration.

Note that the refresh interface requires a post request:

Call the test interface again:


It's strange that there are two ways to get the configuration file, one is updated, the other is not updated. Baidu said it was due to the implementation of @ Value(""). Therefore, it is recommended to use @ ConfigurationProperties(prefix = "data") to obtain configuration parameters in the code again.
So far, the update of configuration parameters has been completed, but it is inconvenient to update manually. Next, look down to how to update automatically.

Automatic update of configuration file based on git



webhooks provides me with the method to call the update interface when updating the configuration file. In this way, the configuration is not automatically updated.

Automatic update of configuration file based on Spring Cloud Bus

This part is not practiced in person. It will be updated after the bus environment is built later.

For reference: Spring Cloud Config implements the configuration center. Just read this article

Published 35 original articles, won praise 19, visited 40000+
Private letter follow

Posted by ofi on Mon, 09 Mar 2020 00:26:03 -0700