2 Star 6 Fork 2

zcltd-btg / btg-set

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

btg-set

项目介绍

采用.json文件、.xml文件、db等持久化存储,保存项目配置,对项目配置(运行参数、业务参数) 进行独立管理(web页面)和配置监控(配置变化主动回调),避免配置文件修改频繁启动项目, 以及单项目多生产环境时,配置文件管理及快速转移以达到快速部署。

源码结构

      exception:插件自定义异常;
           core:
                Config、Module、Param等核心类,定义了配置管理的基本数据结构和常用api
                ConfigManager类:单例,提供用户常用静态访问支持;实现了ParamGetter接口,该接口定义了一系列getXXX方法可
                                 方便获取参数未各种类型
                BtgSet类:用户操作入口,提供ConfigManager的静态支持
  sourcemanager:通过SourceManager接口定义配置管理器标准,为BtgSet提供配置管理服务,插件内置三种实现,如redis等扩展需自行开发
                JsonFileSourceManager实现通过指定目录以一个config对应一个.json文件的方式进行管理
                XmlFileSourceManager实现通过指定目录以一个config对应一个.xml文件的方式进行管理
                db:使用db关系数据库的方式进行管理
                   DbSourceManager为db管理实现
                   dialect:此包为不同关系数据库方言实现,为DbSourceManager提供支持(目前已支持mysql,oracle等后续支持)
                           Dialect类为抽象实现,对于不同关系数据库的方言实现,可扩展该类重写相应方法
        adaptor:提供事件监听支持,通过扩展EventAdapter类重写相应方法可进行自定义实现扩展(目前已支持onConfigApply,其他后续支持)
           util:插件工具类集合
          token:插件token生成及解析相关类
        servlet:为servlet环境web页面管理提供支持,以及json api等支持
                BtgServlet为servlet容器入口
                api:json api实现
            web:插件servlet环境web页面管理使用到的静态资源,如html、js、css等

三方依赖

  1. slf4j:插件日志框架使用(jdk log、log4j、connons log等自行选择,请自行脑补slf4j正确姿势);
  2. junit:测试类使用(生产环境可不添加);
  3. jdbc:数据库驱动(使用DbSourceManager需添加);
  4. fastjson:使用JsonFileSourceManager需添加;
  5. dom4j:使用XmlFileSourceManager需添加;
  6. web环境的jsonApi依赖servlet容器;

使用说明

//创建配置
Config config1 = Config.createConfig("config1", "配置1");
config1.setDesc("配置创建demo");

//创建配置直属配置项
Param config1Param1 = config1.createParam("config1_param1", "配置1直属配置项1", ParamType.String, "config1_value1");
config1Param1.setDesc("配置直属配置项创建演示");

//创建配置模块
Module config1Module1 = config1.createModule("config1_module1", "配置1模块1");
config1Module1.setDesc("配置模块创建演示");

//创建配置模块配置项
Param config1Module1Param1 = config1Module1.createParam("config1_module1_param1", "配置1模块1配置项1", ParamType.Integer, "config1_module1_value1");
config1Module1Param1.setDesc("配置模块配置项创建演示");

//原生支持json、xml(Param、Module、Config均实现ToJson、ToXml接口,拥有toJson()、toXml()方法,
// 并重写了toString()方法直接调用toJson(),所以也可以使用toString()获取json)
String jsonStrConfig1 = config1.toJson();
String jsonStrConfig1Param1 = config1Param1.toJson();
String jsonStrConfig1Module1 = config1Module1.toJson();
String jsonStrConfig1Module1Param1 = config1Module1Param1.toJson();
String xmlStrConfig1 = config1.toXml();
String xmlStrConfig1Param1 = config1Param1.toXml();
String xmlStrConfig1Module1 = config1Module1.toXml();
String xmlStrConfig1Module1Param1 = config1Module1Param1.toXml();

//打印
System.out.println();
System.out.println("jsonStrConfig1:\r\n" + jsonStrConfig1);
System.out.println("jsonStrConfig1Param1:\n" + jsonStrConfig1Param1);
System.out.println("jsonStrConfig1Module1:\n" + jsonStrConfig1Module1);
System.out.println("jsonStrConfig1Module1Param1:\n" + jsonStrConfig1Module1Param1);
System.out.println("xmlStrConfig1:\n" + xmlStrConfig1);
System.out.println("xmlStrConfig1Param1:\n" + xmlStrConfig1Param1);
System.out.println("xmlStrConfig1Module1:\n" + xmlStrConfig1Module1);
System.out.println("xmlStrConfig1Module1Param1:\n" + xmlStrConfig1Module1Param1);

控制台输出(前半段为log输出,具体格式根据日志框架及配置可能有所不同):
---------------------------------------------------------------------------------------------------------------------------------
2017-09-25 10:49:58:981 DEBUG [cn.usbtg.plugin.set.core.Config]  create config:config1
2017-09-25 10:49:58:985 DEBUG [cn.usbtg.plugin.set.core.Config]  create param:config1>config1_param1
2017-09-25 10:49:58:986 DEBUG [cn.usbtg.plugin.set.core.Config]  add param:config1>config1_param1
2017-09-25 10:49:58:988 DEBUG [cn.usbtg.plugin.set.core.Config]  create module:config1>config1_module1
2017-09-25 10:49:58:988 DEBUG [cn.usbtg.plugin.set.core.Config]  add module:config1>config1_module1
2017-09-25 10:49:58:989 DEBUG [cn.usbtg.plugin.set.core.Module]  create param:config1>config1_module1>config1_module1_param1
2017-09-25 10:49:58:990 DEBUG [cn.usbtg.plugin.set.core.Module]  add param:config1>config1_module1>config1_module1_param1

jsonStrConfig1:
{
    "id":"config1",
    "name":"配置1",
    "desc":"配置创建demo",
    "params":[
        {
            "id":"config1_param1",
            "type":"String",
            "datePattern":"",
            "name":"配置1直属配置项1",
            "value":"config1_value1",
            "desc":"配置直属配置项创建演示",
            "remark":""
        }
    ],
    "modules":[
        {
            "id":"config1_module1",
            "name":"配置1模块1",
            "desc":"配置模块创建演示",
            "params":[
                {
                    "id":"config1_module1_param1",
                    "type":"Integer",
                    "datePattern":"",
                    "name":"配置1模块1配置项1",
                    "value":"config1_module1_value1",
                    "desc":"配置模块配置项创建演示",
                    "remark":""
                }
            ]
        }
    ]
}
jsonStrConfig1Param1:
{
    "id":"config1_param1",
    "type":"String",
    "datePattern":"",
    "name":"配置1直属配置项1",
    "value":"config1_value1",
    "desc":"配置直属配置项创建演示",
    "remark":""
}
jsonStrConfig1Module1:
{
    "id":"config1_module1",
    "name":"配置1模块1",
    "desc":"配置模块创建演示",
    "params":[
        {
            "id":"config1_module1_param1",
            "type":"Integer",
            "datePattern":"",
            "name":"配置1模块1配置项1",
            "value":"config1_module1_value1",
            "desc":"配置模块配置项创建演示",
            "remark":""
        }
    ]
}
jsonStrConfig1Module1Param1:
{
    "id":"config1_module1_param1",
    "type":"Integer",
    "datePattern":"",
    "name":"配置1模块1配置项1",
    "value":"config1_module1_value1",
    "desc":"配置模块配置项创建演示",
    "remark":""
}
xmlStrConfig1:
<?xml version="1.0" encoding="UTF-8"?>
<config id="config1" name="配置1" desc="配置创建demo">
    <param id="config1_param1" type="String" name="配置1直属配置项1" desc="配置直属配置项创建演示"><![CDATA[config1_value1]]></param>
    <module id="config1_module1" name="配置1模块1" desc="配置模块创建演示">
        <param id="config1_module1_param1" type="Integer" name="配置1模块1配置项1" desc="配置模块配置项创建演示"><![CDATA[config1_module1_value1]]></param>
    </module>
</config>
xmlStrConfig1Param1:
<param id="config1_param1" type="String" name="配置1直属配置项1" desc="配置直属配置项创建演示"><![CDATA[config1_value1]]></param>
xmlStrConfig1Module1:
<module id="config1_module1" name="配置1模块1" desc="配置模块创建演示">
    <param id="config1_module1_param1" type="Integer" name="配置1模块1配置项1" desc="配置模块配置项创建演示"><![CDATA[config1_module1_value1]]></param>
</module>
xmlStrConfig1Module1Param1:
<param id="config1_module1_param1" type="Integer" name="配置1模块1配置项1" desc="配置模块配置项创建演示"><![CDATA[config1_module1_value1]]></param>
---------------------------------------------------------------------------------------------------------------------------------

说明:
1、上述代码可参见test目录下cn.usbtg.set.TestInit.testConfigGen()方法;
2、使用new的方式不推荐使用,请尽量使用createXXX系列方法;
3、插件提供了其他的一些api方法调用,具体请查看源码或注释;

===========================================================================================================
BtgSet常用方法说明:
初始化:
BtgSet.init(boolean devModel, SourceManager sourceManager, String defaultConfigId):
    插件初始化,devModel为开发模式开关(开发模式会禁用缓存,直接通过sourceManager获取数据),sourceManager为配置管理器,defaultConfigId为默认使用配置

基础使用:
BtgSet.getXXX("param1"):等效于BtgSet.use().getXXX("param1")
    获取当前使用配置中id为param1的配置项参数值(XXX为系列方法)
BtgSet.getXXX("param1","default1"):等效于BtgSet.use().getXXX("param1","default1")
    获取当前使用配置中id为param1的配置项参数值(XXX为系列方法),若获取到的值为null,返回默认值
BtgSet.use("config1").getXXX("param1"):
    获取id为config1的配置中id为param1的配置项参数值(XXX为系列方法)
BtgSet.use("config1").getXXX("param1","default1"):
    获取id为config1的配置中id为param1的配置项参数值(XXX为系列方法),若获取到的值为null,返回默认值
BtgSet.use("config1").getModule("module1).getXXX("param1"):
    获取id为config1的配置中id为module1的模块id为param1的配置项参数值(XXX为系列方法)
BtgSet.use("config1").getModule("module1).getXXX("param1","default1"):
    获取id为config1的配置中为module1的模块id为param1的配置项参数值(XXX为系列方法),若获取到的值为null,返回默认值

多sourceManager支持:
BtgSet.useSourceManager("xml").use("config1").getXXX("param1"):
    获取name为xml的sourceManager中id为config1的配置中id为param1的配置项参数值(XXX为系列方法)
BtgSet.useSourceManager("xml").use("config1").getXXX("param1","default1"):
    获取name为xml的sourceManager中id为config1的配置中id为param1的配置项参数值(XXX为系列方法),若获取到的值为null,返回默认值
BtgSet.useSourceManager("xml").use("config1").getModule("module1).getXXX("param1"):
    获取name为xml的sourceManager中id为config1的配置中id为module1的模块id为param1的配置项参数值(XXX为系列方法)
BtgSet.useSourceManager("xml").use("config1").getModule("module1).getXXX("param1","default1"):
    获取name为xml的sourceManager中id为config1的配置中为module1的模块id为param1的配置项参数值(XXX为系列方法),若获取到的值为null,返回默认值

高级进阶:
BtgSet.use("config1"):
    获取id为config1的配置
BtgSet.useSourceManager("xml"):
    获取name为xml的配置管理器
BtgSet.apply("config1"):
    将id为config1的配置设为默认配置
BtgSet.applySourceManager("xml"):
    将name为xml的配置管理器设为默认配置管理器
BtgSet.merge("config1"):
    将id为config1的配置合并到当前sourceManager中(存在则更新,不存在则新增)
BtgSet.mergeAll("config1"):
    将id为config1的配置合并到所有sourceManager中(存在则更新,不存在则新增)
BtgSet.copyTo("config1","config2"):
    从配置config1拷贝并重命名config2(config2会被添加到当前sourceManager中)

住:其他方法请查看类BtgSet、ConfigManager源码或注释

===========================================================================================================
SourceManager的使用:
插件定义了SourceManager接口,通过实现SourceManager接口为插件提供配置管理服务,可自定义扩展。
插件默认给出了三种常见配置管理方式:
1、JsonFileSourceManager
    使用jsson文件进行管理,一个config对应一个.json文件,必须参数(folderPaht-文件存放目录)
2、XmlFileSourceManager
    使用xml文件进行管理,一个config对应一个.xml文件,必须参数(folderPaht-文件存放目录)
3、DbSourceManager
    使用db文件进行管理,必须参数(DataSource-jdbc数据源连接池)

===========================================================================================================
log说明:
1、config>module>param
    表示config配置下的module模块下的param参数(>符号表示层级关系)
2、config>param
    表示config下的param(>符号表示层级关系)

===========================================================================================================
token生成说明:
    token主要用于web页面管理和json api的安全验证,BtgSet可进行设置是否开关。

    使用:
    java -jar btg-set-xxx.jar -h
        -h    帮助信息
        -k    必须,加密key,即应用配置的验证token的key
        -t    必须,token失效时间,格式为yyyyMMddHHmmss,默认为从生成时间开始计算20分钟后失效
        -ip   ip地址白名单正则表达式,多个以","连接,不指定时为不限制ip
        -u    登录用户名白名单正则表达式,多个以","连接,不指定时为不限制登录用户

    示例:
    java -jar btg-set-xxx.jar -kmykey -t20180918235959 -ip192\.168\.0\.[1-9]+,192\.168\.[3-4]+\.2 -uuser1,user2
    java -jar btg-set-xxx.jar -kmykey -t20990101235959

    结果:
    A5CDEA8AAD0288974CC3B3F476A9E0AA40330AC91E15F9574A78FEDC4A89989E1677E65E4C09583D1A0130B36BCE92BA751F46F33D71297BFDC92CF254EBAB38D1F2DADA8D6649F4
    B936E55470AEEDAAEFA918A2CCD9F5C14D830DC18F0F6309

    示意:
    用户user1,user2可通过ip 192.168.0.1到192.168.0.9和192.168.3.2、192.168.4.2进行访问,并在2018-09-18 23:59:59后过期
    所有用户在所有ip可进行访问,并在2099-01-01 23:59:59后过期(相当于无任何限制也无过期时间)

升级记录

v4.1.5
1、未加载到配置是给出警告日志

v4.1.4
1、btg-util升级至4.0.24;
2、SourceManager load 当未找到配置时默认返回一个空的Config,而不是null

v4.1.3
1、btg-util升级至4.0.15;
2、扩展ParamConvert,支持去两端空格,参数默认去空格;

v4.1.2
1、btg-util升级至4.0.14;

v4.1.1
1、config、module、param的name属性改为非必填;
2、util包由btg-util支持;

v4.0.1
1、转为独立maven依赖,去掉parent;

v3.0.3
1、btg-parent升级到v2.0.1;

v3.0.2
1、统一依赖管理;

v3.0.1
1、转为maven项目;
2、统一迁移至公司名下;

v1.0.5:
1、事件规整,仅支持onThisConfigLoad;

v1.0.4:
1、修复devMode模式不生效的bug;
2、json、xml的SourceManager增加config重复验证;

v1.0.3:
1、测试全覆盖;

v1.0.2:
1、增加getBoolean的支持;

v1.0.1:
1、采用.json文件、.xml文件、db等持久化存储,保存项目配置,对项目配置(运行参数、业务参数)进行独立管理(web页面)和配置监控(配置变
  化主动回调),避免配置文件修改频繁启动项目,以及单项目多生产环境时,配置文件管理及快速转移以达到快速部署。

豆圆

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 豆圆 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

采用.json文件、.xml文件、db等持久化存储,保存项目配置,对项目配置(运行参数、业务参数)进行独立管理(web页面)和配置监控(配置变化主动回调),避免配置文件修改频繁启动项目,以及单项目多生产环境时,配置文件管理及快速转移以达到快速部署。 展开 收起
Java 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zcltd-btg/btg-set.git
git@gitee.com:zcltd-btg/btg-set.git
zcltd-btg
btg-set
btg-set
master

搜索帮助