字节裁员,微服务已死?赶紧试试Smart-Config SpringBoot动态变更配置 单机版apollo

226 阅读2分钟

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: github.com/yangfeng20/…

gitee: gitee.com/yangfeng20/…

UML类图

image.png

架构图

smart-config内部架构图| ProcessOn免费在线作图,在线流程图,在线思维导图

image.png