Lightweight SpringBoot configuration center - Minimum config

introduce

Minimal config spring boot starter is an expanded lightweight configuration center based on the implementation principle of spring boot native configuration injection. The project volume is only 24KB. The design concept is to serve small and medium-sized projects, quickly build a remote configuration center and refresh the configuration in real time. It itself provides remote configuration reading capability based on Gitee code warehouse, Developers only need to simply configure the resource file path and authorize access to the Token to realize the function of the configuration center.

Open source project address: https://github.com/23557544/minimal-config-spring-boot-starter

Welcome to submit PR

 

  Application startup Configure refresh
Member variables are annotated with @ Value support support
Construct method parameters with @ Value annotation support Not supported at the moment
The Set method uses the @ Value annotation support Not supported at the moment
Spel expression support support

Note: for functions that are not supported temporarily, you can submit PR or improve them later when you are free.  

 

contrast

Nacos and Apollo, the former is the Spring Cloud Alibaba ecological component and the latter is Ctrip's open source configuration center middleware. They have one thing in common is that they need to build a server. The price of Alibaba cloud's Micro service engine registration configuration center is more expensive than a single ECS. From the perspective of cost, it is not suitable for the deployment needs of small and medium-sized projects with limited budget.

Spring Cloud Config, the spring cloud ecological component, also supports the use of Git code warehouse as the configuration intermediate. It needs to rely on Spring Cloud Config server and spring boot starter actor at the same time. Moreover, the original intention of the actor is to monitor the health of applications. After introducing dependency, it will add many useless functions and increase the volume of application packaging or image.

 

use

Maven introduces minimal config spring boot starter dependency

<dependency>
    <groupId>cn.codest</groupId>
    <artifactId>minimal-config-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

 

When using Gitee code warehouse as the configuration center, add the corresponding configuration in the SpringBoot configuration file:

# Gitee configuration file resource address, read file information through Gitee Open API, see: https://gitee.com/api/v5/swagger
codest.config.gitee.url=https://gitee.com/api/v5/repos/Warehouse name/entry name/contents/demo.properties
# Gitee is authorized to access the token and log in to gitee in settings - security settings- Add to private token
codest.config.gitee.token=xxxx

 

By implementing the remoteconfigureprovider interface to customize the remote configuration loading mode, you can put the configuration information in the database, file system or other middleware for persistence according to your own needs, and realize the configuration item loading function at the same time:

public interface RemoteConfigProvider {
    Properties load();
}

 

Specify the configuration source in the SpringBoot configuration file. After specifying the configuration item implementation class, the git warehouse configuration item will not be loaded:

Note that the configuration center completes the initialization through the EnvironmentPostProcessor. At this time, the initialization of components such as log and DataSource has not been completed

codest.config.provider=cn.codest.demo.provider.CustomConfigProvider

 

Configure refresh

By default, it does not provide the function of scheduled overload remote configuration. You can refresh the configuration through the REST interface or scheduled tasks according to the actual needs. The key codes are as follows:

private final RefreshConfigExecutor executor;

@GetMapping("/refresh")
public String refresh() {
    executor.execute();
    return HttpStatus.OK.getReasonPhrase();
}

 

Demo

Add Maven dependency

<dependency>
    <groupId>cn.codest</groupId>
    <artifactId>minimal-config-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

Configure Gitee code warehouse configuration file resource address

codest.config.gitee.url=https://gitee.com/api/v5/repos/codest-c/config-folder/contents/demo.properties
codest.config.gitee.token=xxxx

Gitee code warehouse add demo Properties configuration file

name=Zhang San
month=1,2,3,4,5,6

Application code


@RestController
@SpringBootApplication
public class ConfigDemoApplication {

private final static Logger log = LoggerFactory.getLogger(ConfigDemoApplication.class);

@Value("#{'Hi, ${name}'.concat('!')}")
private String name;

@Value("${month}")
private List<Integer> month;

private final RefreshConfigExecutor executor;

public ConfigDemoApplication(RefreshConfigExecutor executor) {
this.executor = executor;
}

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

@GetMapping("/refresh")
public String refresh() {
executor.execute();
return HttpStatus.OK.getReasonPhrase();
}

@PostConstruct
@GetMapping("/print")
public void print() {
log.info(name);
log.info(month.toString());
}

}

 

Configure output after project startup:

INFO 17184 --- [           main] c.c.configdemo.ConfigDemoApplication     : Hi, Zhang San!
INFO 17184 --- [           main] c.c.configdemo.ConfigDemoApplication     : [1, 2, 3, 4, 5, 6]

 

Modify the configuration file in Gitee code warehouse as follows:

name=Li Si
month=1,2,3,4,5,6,7,8,9,10,11,12

 

visit http://localhost:8080/refresh Refresh the configuration, as shown in the following figure

 

 

Visit http://localhost:8080/print Print the refreshed configuration

INFO 7792 --- [nio-8080-exec-5] c.c.configdemo.ConfigDemoApplication     : Hi, Li Si!
INFO 7792 --- [nio-8080-exec-5] c.c.configdemo.ConfigDemoApplication     : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

 

Keywords: Spring Boot

Added by ColdFusion on Wed, 09 Mar 2022 08:54:27 +0200