使用Config Toolkit实现配置中心

Config Toolkit简介

Config Toolkit是当当开源的一个分布式配置的工具集,主要用于封装应用和zookeeper配置之间的获取及更新等细节,并且提供了一个操作zookeeper的配置界面。

搭建配置界面

下载config-toolkit项目
git clone https://github.com/dangdangdotcom/config-toolkit.git
cd config-toolkit/config-zk-web
mvn package

将编译好的config-web.war部署到tomcat即可

创建初始权限配置

使用命令行创建zookeeper配置根节点,根节点密码使用sha1加密,如果要使用明文密码,可以自行修改config-zk-web的鉴权部分代码 以根路径为/projectx/modulex密码为abc为例

python -c "import hashlib;print hashlib.sha1('abc').hexdigest();"
# a9993e364706816aba3e25717850c26c9cd0d89d 
zkCli.sh -server localhost:2181
create /projectx 1
create /projectx/modulex a9993e364706816aba3e25717850c26c9cd0d89d
登录config-web,创建示例配置
  • 访问http://localhost:8080/config-web
  • 点击"切换根节点",输入/projectx/modulex,密码abc
  • 点击"新建版本",输入1.0.0
  • 左侧的组管理,输入group,点击"创建"
  • 在右侧添加两个配置,分别为config.str=hello, config.int=7758

项目中加载配置

添加maven依赖
<dependency>
  <groupId>com.dangdang</groupId>
  <artifactId>config-toolkit</artifactId>
  <version>3.1.7-RELEASE</version>
</dependency>
使用Java代码直接获取配置
ZookeeperConfigProfile configProfile = new ZookeeperConfigProfile("localhost:2181", "/projectx/modulex", "1.0.0");
GeneralConfigGroup group = new ZookeeperConfigGroup(configProfile, "group");

String stringProperty = group.get("config.str");
Preconditions.checkState("hello".equals(stringProperty));
String intProperty = group.get("config.int");
Preconditions.checkState(7758 == Integer.parseInt(intProperty));
结合spring placeholder方式注入配置
<bean id="configProfile" class="com.dangdang.config.service.zookeeper.ZookeeperConfigProfile">
    <constructor-arg name="connectStr" value="localhost:2181" />
    <constructor-arg name="rootNode" value="/projectx/modulex" />
    <constructor-arg name="version" value="1.0.0" />
</bean>

<bean id="configGroupSources" class="com.dangdang.config.service.support.spring.ConfigGroupSourceFactory" factory-method="create">
    <constructor-arg name="configGroups">
        <list>
            <bean class="com.dangdang.config.service.zookeeper.ZookeeperConfigGroup" c:configProfile-ref="configProfile" c:node="-group" />
        </list>
    </constructor-arg>
</bean>

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="order" value="1" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="propertySources" ref="configGroupSources" />
</bean>

<!-- Your business bean -->
<bean class="your.BusinessBean">
    <property name="strProp" value="${config.str}" />
    <property name="intProp" value="${config.int}" />
</bean>

由于spring对多个placeholder的支持不太好,需要仔细配置order,所以建议使用SPEL方式来配置

结合spring SPEL方式注入配置
<bean id="configProfile" class="com.dangdang.config.service.zookeeper.ZookeeperConfigProfile">
    <constructor-arg name="connectStr" value="localhost:2181" />
    <constructor-arg name="rootNode" value="/projectx/modulex" />
    <constructor-arg name="version" value="1.0.0" />
</bean>
<bean id="groupProp" class="com.dangdang.config.service.zookeeper.ZookeeperConfigGroup">
    <constructor-arg name="configProfile" ref="configProfile" />
    <constructor-arg name="node" value="group" />
</bean>

<!-- Your business bean -->
<bean class="your.BusinessBean">
    <property name="strProp" value="#{groupProp['config.str']}" />
    <property name="intProp" value="#{groupProp['config.int']}" />
</bean>

OK, 大功告成,下一篇再写一些高级的应用

转载于:https://my.oschina.net/u/813340/blog/780623

Config Toolkit 是大型集群和分布式应用配置工具包。Config toolkit 用于简化从本地配置文件到 zookeeper 的迁移。在大型集群和分布式应用中,配置不宜分散到集群结点中,应该集中管理.依赖JAVA 7 TOMCAT 7 for ConfigWeb模块Config Toolkit - 封装应用属性配置的获取及更新ConfigWeb - 提供web界面维护属性配置,提供配置导入导出功能Spring集成本地配置覆盖配置管理web界面版本控制,支持灰度发布支持为配置项添加注释Quick Startload properties from zookeeperZookeeperConfigProfile configProfile = new ZookeeperConfigProfile("zoo.host1:8181", "/projectx/modulex", "1.0.0");     GeneralConfigGroup propertyGroup1 = new ZookeeperConfigGroup(configProfile, "property-group1");load properties from classpath fileFileConfigProfile configProfile = new FileConfigProfile("UTF8", "properties");     ConfigGroup configGroup = new FileConfigGroup(configProfile, "classpath:property-group1.properties");load xml properties from classpath fileFileConfigProfile configProfile = new FileConfigProfile("UTF8", "xml");     ConfigGroup configGroup = new FileConfigGroup(configProfile, "classpath:property-group1.xml");load properties from fileFileConfigProfile configProfile = new FileConfigProfile("UTF8", "properties");     ConfigGroup configGroup = new FileConfigGroup(configProfile, "file:/Users/yuxuanwang/Work/git/config-toolkit/config-toolkit-demo/src/main/resources/property-group1.properties");load properties from httpFileConfigProfile configProfile = new FileConfigProfile("UTF8", "properties");     ConfigGroup configGroup = new FileConfigGroup(configProfile, "http://crnlmchina.github.io/config-group.properties"); 标签:Config
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值