轻量级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]

 

posted @ 2022-03-09 14:43  codest  阅读(1264)  评论(1编辑  收藏  举报