輕量級SpringBoot配置中心 - Minimal-Config


介紹

minimal-config-spring-boot-starter,是基於Spring-Boot原生配置注入實現原理的基礎上,拓展的輕量級配置中心,項目體積只有24KB,設計理念為服務中小型項目,快速構建遠程配置中心及配置實時刷新,本身提供了基於Gitee代碼倉庫的遠程配置讀取能力,開發者只需要簡單配置資源文件路徑和授權訪問Token即可實現配置中心的功能。

開源項目地址:https://github.com/23557544/minimal-config-spring-boot-starter

歡迎大家提交PR

 

  應用啟動 配置刷新
成員變量使用@Value注解 支持 支持
構造方法參數使用@Value注解 支持 暫不支持
Set方法使用@Value注解 支持 暫不支持
Spel表達式 支持 支持

注:暫不支持的功能,大家可以提交PR,或者等本人后期有空再行完善。 

 

對比

Nacos和Apollo,前者是Spring Cloud Alibaba生態組件,后者是攜程開源的配置中心中間件,兩者有一個共同點是需要搭建服務端,其中阿里雲的微服務引擎注冊配置中心的價格,比單台ECS還貴,從成本角度考慮,不適合預算有限的中小型項目部署需求。

Spring Cloud Config,SpringCloud生態組件,同樣支持使用Git代碼倉庫作為配置中間,需要同時依賴spring-cloud-config-server和spring-boot-starter-actuator,而且actuator設計的初衷是應用的健康監控,引入依賴后會增加很多用不上的功能,增加應用打包或者鏡像體積。

 

使用

 Maven引入minimal-config-spring-boot-starter依賴

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

 

使用Gitee代碼倉庫作為配置中心時,在SpringBoot配置文件中添加相應配置:

# gitee配置文件資源地址,通過Gitee Open API讀取文件信息,查閱:https://gitee.com/api/v5/swagger
codest.config.gitee.url=https://gitee.com/api/v5/repos/倉庫名稱/項目名稱/contents/demo.properties
# gitee授權訪問token,登錄Gitee在設置 - 安全設置 - 私人令牌中添加
codest.config.gitee.token=xxxx

 

通過實現RemoteConfigProvider接口自定義遠程配置加載方式,可以根據自身需求,將配置信息放在數據庫、文件系統或其它中間件持久化,同時實現配置項加載功能即可:

public interface RemoteConfigProvider {
    Properties load();
}

 

在SpringBoot配置文件中指定配置源,指定配置項實現類后,不再加載git倉庫配置項:

注意,配置中心通過EnvironmentPostProcessor完成初始化工作,此時例如日志、DataSource等組件還沒有初始化完成

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

 

配置刷新

默認沒有提供定時重載遠程配置的功能,可以根據實際需求通過REST接口或者定時任務刷新配置,關鍵代碼如下:

private final RefreshConfigExecutor executor;

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

 

Demo

添加Maven依賴

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

配置Gitee代碼倉庫配置文件資源地址

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

Gitee代碼倉庫添加demo.properties配置文件

name=張三
month=1,2,3,4,5,6

Application代碼

 
         
@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());
}

}

 

項目啟動完成后配置輸出:

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

 

修改Gitee代碼倉庫中的配置文件如下:

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

 

訪問http://localhost:8080/refresh刷新配置,如下圖

 

 

 訪問http://localhost:8080/print打印下刷新后的配置內容

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM