Smart-Config SpringBoot动态变更配置 单机版apollo

Smart-Config(单机Apollo)

  • 智能配置:单体应用下的动态配置。
  • 主要用来解决在单体应用没有配置中心时,想要实现动态变更配置,程序自动处理配置变更,给字段赋值的痛点。可以理解为单机版的Apollo。
  • Apollo是携程开源的分布式配置中心,需要单独的部署Apllo配置中心应用,然后在业务应用中指定配置中心的地址,去获取配置中心的配置,实现自定义逻辑。
  • Smart-Config对比Applo的区别是,它是不需要单独去部署应用的,是直接内嵌在我们的业务应用中的。并且侵入性小,上手简单。

优势

  • 权限校验
    • 登录之后才能够查看,修改,发布配置
  • 动态变更配置
    • 业务程序自动获取最新配置,给对应字段赋值
  • 配置描述推断
    • 将配置文件中的注释通过推断显示在webUi中
  • WebUi修改配置
  • 支持非SpringBoot应用
  • 无缝衔接SpringBoot应用
    • 启动类添加@EnableSmartConfig注解使用SmartConfig功能
  • 支持结构化数据(json,集合,对象)
    • Spring使用@Value来支持简单类型
    • SmartConfig使用@JsonValue来支持结构化数据
  • 轻量级无冗余第三方库
  • 内嵌轻量级jetty服务器
  • WebUi支持中英文切换
  • 支持springboot多配置文件
  • 支持spring.config.location以及spring.profiles.active
  • 支持启动参数修改WebUi端口以及配置描述推断

如何使用

依赖引入
<dependency>
   <groupId>io.github.yangfeng20</groupId>
   <artifactId>smart-config-core</artifactId>
   <version>1.0.0</version>
</dependency>
配置文件添加WebUi用户名和密码
# smart配置
smart.username=admin
smart.password=admin
  1. Spring-Boot使用

    主类添加注解@EnableSmartConfig

    @EnableSmartConfig
    public class XxxApplication {
        ApplicationContext application = SpringApplication.run(XxxApplication.class, args);
    }
    

    使用@Value注解

    public class AppConfig{
    
       @Value("${configKey}")
       private String value;
       
       @JsonValue("${list:[1]}")
       private List<Integer> list;
       
       @JsonValue("${config.entity:{}}")
       private XxxEntity entity; 
    
    } 
    
  2. 非spring项目使用

    创建LocalFileConfig对象,参数分别为webUi的端口,是否推断配置描述。

    public class App{
       public static void main(String[]args){
           SmartConfig smartConfig = new LocalFileConfig(6767, true);
           List<String> list = new ArrayList<>();
           list.add("com.maple.smart.config.test");
           AbsConfigBootstrap bootstrap = new LocalConfigBootstrap(true, 6767,"classpath:application.properties", list);
           bootstrap.init();
       }
    }   
    

    静态类的静态字段使用@SmartValue注解

    public class AppConfig{
    
        @SmartValue("配置key:默认值")
        private static String config1;
    
        @SmartValue("biz.name:abc")
        private static String bizName;
    
        @JsonValue("${list:[1]}")
        private static List<Integer> list;
       
        @JsonValue("${config.entity:{}}")
        private static XxxEntity entity; 
    }
    

注意事项

  • Web-Ui默认端口:6767
  • SpringBoot应用配置类被代理后请勿直接访问属性,而是通过getter访问
  • 非Spring应用仅支持静态变量。

项目地址,记得点个star

github: https://github.com/yangfeng20/smart-config
gitee: https://gitee.com/yangfeng20/smart-config

UML类图

在这里插入图片描述

架构图地址:

https://www.processon.com/preview/6574750440125a40fd170439
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值