在大型集群和分布式应用中,配置数据不宜分散到集群结点中,应该集中起来统一管理。本文主要介绍如何使用这个开源工具。
1、首先从github下载该项目源码:
[plain] view plain copy
- 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、在控制管理界面,登陆/admin,密码是123456
9、创建版本和配置数据
10、通过java代码获取配置数据
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>config-toolkit</artifactId>
<version>3.1.6-RELEASE</version>
</dependency>
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集成获取配置数据
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="configProfile" class="com.dangdang.config.service.zookeeper.ZookeeperConfigProfile">
<constructor-arg name="connectStr" value="127.0.0.1:2181"/>
<constructor-arg name="rootNode" value="/atyroot"/>
<constructor-arg name="version" value="0.0"/>
</bean>
<!--代码中使用这个bean,可以实现配置数据热替换-->
<bean id="jdbcConfigGroup" class="com.dangdang.config.service.zookeeper.ZookeeperConfigGroup"
c:configProfile-ref="configProfile" c:node="jdbc"/>
<bean id="configGroupSources" class="com.dangdang.config.service.support.spring.ConfigGroupSourceFactory"
factory-method="create">
<constructor-arg name="configGroups">
<list>
<ref bean="jdbcConfigGroup"></ref>
</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>
<!--spring容器只加载一次,所以不能热替换-->
<bean id="holder" class="net.aty.dangdang.ConfigInfoHolder">
<property name="detail" value="${url}"></property>
</bean>
</beans>
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就可以了,前者是登录,后者是注册。