zk配置中心---Config Toolkit配置与使用

Config Toolkit 简介:用于分布式系统中的资源配置中心,替代传统properties文件,优点:立即修改配置立即生效,不需要新重新打包部署项目。

下载地址:https://github.com/dangdangdotcom/config-toolkit

配置zk,生产环境一般配置5台,创建配置节点时会同步,这里使用默认配置,启动zkServer。

启动项目(将config-face打包成jar或者war部署到服务器,随意):

输入http://localhost:8080 进入登录页面:

创建节点及密码,默认根节点密码使用sha1加密,创建密码方法,(密码:abc):

[root@hadoop01 ~]# python -c "import hashlib;print hashlib.sha1('abc').hexdigest();"

a9993e364706816aba3e25717850c26c9cd0d89d

创建节点:

1. 使用zk客户端

create /projectx 1

create /projectx/modulex a9993e364706816aba3e25717850c26c9cd0d89d

2.使用java代码

<dependency>
   <groupId>com.101tec</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.10</version>
</dependency>
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

/**
 * Created by yz on 2018/6/11.
 */
public class TestZookeeper {
    public static void main(String[] args) {
        // 60000 session超时时间;1000 连接超时时间
        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 60000, 10000);
        // 节点(路径);值;节点类型 PERSISTENT永久节点
        //zkClient.create("/projectx","1", CreateMode.PERSISTENT);
        zkClient.create("/projectx/modulex","a9993e364706816aba3e25717850c26c9cd0d89d", CreateMode.PERSISTENT);
        zkClient.close();
        System.out.println("###注册成功###");
    }
}

使用节点名:“/projectx/modulex”,密码:“abc”登录,登录后,点击new 创建版本1.0.0

分别创建组,创建节点

使用配置:

1.代码直接获取配置:

import com.dangdang.config.service.GeneralConfigGroup;
import com.dangdang.config.service.zookeeper.ZookeeperConfigGroup;
import com.dangdang.config.service.zookeeper.ZookeeperConfigProfile;

/**
 * @description:
 * @author: yz
 * @create: 2019/2/14 15:17
 */
public class YzDemo {

    public static void main(String[] args) {
        ZookeeperConfigProfile configProfile = new ZookeeperConfigProfile("localhost:2181", "/projectx/modulex", "1.0.0");
        GeneralConfigGroup group = new ZookeeperConfigGroup(configProfile, "group");

        String stringProperty = group.get("config.str");
        System.out.println("stringProperty: "+stringProperty);
        String intProperty = group.get("config.int");
        System.out.println("intProperty: "+intProperty);
    }

}

2.spring注入配置

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:config="https://crnlmchina.github.io/config"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             https://crnlmchina.github.io/config https://crnlmchina.github.io/config/config.xsd">

<config:profile connect-str="localhost:2181" root-node="/projectx/modulex" version="1.0.0"/>
<config:group id="groupProp" node="group"/>

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

</beans>

 对应zk节点:

 

 

 

https://github.com/dangdangdotcom/config-toolkit

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值