一沙一世界, 一花一天堂。

使用当当网的Config Toolkit集中管理分布式集群的配置数据

[Java] 韩玉龙 2017-09-02 10:41:31 点击率:4240次

1、首先从github下载该项目源码:

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



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


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

4、启动zookeeper server,端口号使用默认的2181


6、创建项目需要的根节点,并对内容进行加密。比如我们的根节点是/atyroot,数据内容是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,创建根节点


8、在控制管理界面,登陆/atyroot,密码是123456


9、创建版本和配置数据


10、通过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", "/atyroot", "0.0");
GeneralConfigGroup group = new ZookeeperConfigGroup(configProfile, "jdbc");
System.out.println(group.get("url"));
}
}

11、与spring集成获取配置数据



xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">









c:configProfile-ref="configProfile" c:node="jdbc"/>


factory-method="create"


import com.dangdang.config.service.zookeeper.ZookeeperConfigGroup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.IOException;

public class SpringDangDangMain {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
"src/main/java/net/aty/dangdang/spring-dangdang.xml");// 加载 spring 配置文件
ConfigInfoHolder holder = (ConfigInfoHolder) context.getBean("holder");


ZookeeperConfigGroup group = (ZookeeperConfigGroup) context.getBean("jdbcConfigGroup");
System.out.println(holder.getDetail());
System.out.println(group.get("url"));

// 修改url配置数据后,在控制台回车,观察热替换效果
try {
System.in.read();
} catch (IOException e) {

}

System.out.println(holder.getDetail());
System.out.println(group.get("url"));

}

}


12、再谈管理界面的登录和注册

上面我们是通过自己对zookeeper创建节点并加密,然后可以用这个信息登录easyZK配置界面。那只不过是为了展示easyZK的实现原理,实际项目中使用是很容易的。输入节点名(/aty)和密码(123456)之后,直接sign in 和sign up就可以了,前者是登录,后者是注册。


转载自:http://blog.csdn.net/aitangyong/article/details/52933063