使用当当网的Config Toolkit集中管理配置数据

1、首先下载该项目源码

https://download.csdn.net/download/qq_34021712/10317654?utm_source=bbsseo

使用当当网的Config Toolkit集中管理配置数据_第1张图片

2、将config-zk-web这个maven工程打包,可以得到一个war,这就是配置管理界面后台。

cd config-toolkit/config-zk-web/
mvn clean package

3、启动zookeeper server

5、将war部署到tomcat下,就可以通过http://127.0.0.1:8080/config-web/访问

可以通过修改/config-web/WEB-INF/classes/zk.properties来修改注册中心地址

使用当当网的Config Toolkit集中管理配置数据_第2张图片

6、创建项目需要的根节点,并对内容进行加密。比如我们的根节点是/PAYCENTER,数据内容是123456。为了能在配置管理界面连接这个节点,需要对内容进行SHA1加密。

import com.google.common.hash.Hashing;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
 
public class SHA1 {
    public static void main(String[] args) throws NoSuchAlgorithmException {
 
        // 7c4a8d09ca3762af61e59520943dc26494f8941b
        String password = SHA1("123456");
        System.out.println(password);
 
        // 7c4a8d09ca3762af61e59520943dc26494f8941b
        String dest = Hashing.sha1().hashBytes("123456".getBytes()).toString();
        System.out.println(dest);
    }
 
    public static String SHA1(String decript) throws NoSuchAlgorithmException {
        MessageDigest digest = java.security.MessageDigest
                .getInstance("SHA-1");
        digest.update(decript.getBytes());
        byte messageDigest[] = digest.digest();
 
        StringBuffer hexString = new StringBuffer();
 
        for (int i = 0; i < messageDigest.length; i++) {
            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();
 
    }
}

7、启动zk客户端连接zookeeper,创建根节点

使用当当网的Config Toolkit集中管理配置数据_第3张图片

使用当当网的Config Toolkit集中管理配置数据_第4张图片

8、Java程序连接


     com.dangdang
     config-toolkit
     3.1.6-RELEASE

import com.dangdang.config.service.GeneralConfigGroup;
import com.dangdang.config.service.zookeeper.ZookeeperConfigGroup;
import com.dangdang.config.service.zookeeper.ZookeeperConfigProfile;
 
public class DandangMain {
    public static void main(String[] args) {
        ZookeeperConfigProfile configProfile = new ZookeeperConfigProfile("127.0.0.1:2181", "/PAYCENTER", "1.0.0");
        GeneralConfigGroup group = new ZookeeperConfigGroup(configProfile, "test1");
        System.out.println(group.get("password"));
    }
}

9、与spring集成

application.properties

##配置中心zk地址ַ
zk.address=192.168.40.223:2181
##配置中心
configToolkit.rootNode=/PAYCENTER


	

   
   
   








		
			
				
				
			
		



		
		
		




	

/**
 * @Title: 支持热更新的配置项,该配置项的数据来自config-toolkit。
 */
@SuppressWarnings("rawtypes")
public class HotConfig implements InitializingBean {
	/**
	 * zookeeper配置项
	 */
	private ZookeepConfig zookeeperConfigGroup;

	/**
	 * 时间任务
	 */
	private Timer timer;

	/**
	 * 配置对象的更新周期
	 */
	private long objCfgUpdatePeriod = 1000*60*60 ;

	public void setZookeeperConfig(ZookeepConfig zookeeperConfig) {
		this.zookeeperConfigGroup = zookeeperConfig;
	}

	public Map getZookeeperConfigGroup() {
		return zookeeperConfigGroup;
	}

	public void setZookeeperConfigGroup(ZookeepConfig zookeeperConfigGroup) {
		this.zookeeperConfigGroup = zookeeperConfigGroup;
	}
	
	public Object get(String key,Object def){
		Object ret=null;
		try {
			ret= this.zookeeperConfigGroup.get(key);
			if(ret==null){
				ret= def;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	/**
	 * 实时获取最新的参数配置
	 * @param key
	 * @return
	 */
	public Object get(String key) {
		return this.zookeeperConfigGroup.get(key);
	}

	public void put(String key,String value){
		zookeeperConfigGroup.update(key,value);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		timer = new Timer();
	}
}
public class ZookeepConfig extends  ZookeeperConfigGroup{
    private ZookeeperConfigProfile configProfile;
    public ZookeepConfig(ConfigGroup internalConfigGroup, ZookeeperConfigProfile configProfile, String node) {
        super(internalConfigGroup, configProfile, node);
        this.configProfile=configProfile;
    }

    public ZookeepConfig(ZookeeperConfigProfile configProfile, String node) {
        super(configProfile, node);
        this.configProfile=configProfile;
    }

    public String getProperty(String key){
        String value=super.get(key);
        return value.trim();
    }

    public void update(String key,String value){
        try {
           // String nodeName="/"+configProfile.getRootNode()+"/"+configProfile.getVersion()+"/"+getNode()+"/"+key;
            String nodeName= configProfile.getRootNode()+"/"+configProfile.getVersion()+"/"+getNode()+"/"+key;
            Field field=this.getClass().getSuperclass().getDeclaredField("client");
            field.setAccessible(true);
            CuratorFramework client=(CuratorFramework)field.get(this);
            boolean suc = false;
            try {
                Stat stat = client.checkExists().forPath(nodeName);
                if (stat != null) {
                    Stat opResult = client.setData().forPath(nodeName, value.getBytes(Charsets.UTF_8));
                    suc = opResult != null;
                } else {
                    String opResult = client.create().creatingParentsIfNeeded().forPath(nodeName, value.getBytes(Charsets.UTF_8));
                    suc = Objects.equal(nodeName, opResult);
                }
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
}

 

你可能感兴趣的:(dubbo)