import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import com.lenovo.lps.log.Log;
import com.lenovo.lps.psb.zookeeper.util.ZooKeeperUtil;
/**
* ZooKeeperUpdateClient
*
*
*/
public class ZooKeeperUpdateClient {
private Log log = Log.getInstance(this.getClass());
private static ZooKeeperManager zooKeeperManager = ZooKeeperManager
.getInstance();
public ZooKeeperUpdateClient() {
}
public boolean updateData(String path, String data) {
if (data == null) {
log.warn("Parameter data is null");
return false;
}
try {
Stat stat = getZk().exists(path, false);
if (stat == null) {
ZooKeeperUtil.createNodeWithRecurrence(getZk(), path, data.getBytes());
} else {
getZk().setData(path, data.getBytes(), -1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if(log.isInfoEnabled()) {
log.info("update data: path: " + path + ", data: " + data);
}
return true;
}
private ZooKeeper getZk() {
return zooKeeperManager.getZk();
}
}
public interface IZooKeeperReadClient {
/**
* ZooKeeperManager通过此接口通知更新的数据到此read client
*/
public void notify(String path, byte[] data);
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.zookeeper.ZooKeeper;
import com.lenovo.lps.log.Log;
import com.lenovo.lps.psb.zookeeper.api.IZooKeeperReadClient;
/**
* ZooKeeperReadClient
*
*
*
*/
public class ZooKeeperReadClient implements IZooKeeperReadClient {
private Log log = Log.getInstance(this.getClass());
private static final Map<String, byte[]> pathDataMap = new HashMap<String, byte[]>();
private static ZooKeeperManager zooKeeperManager = ZooKeeperManager
.getInstance();
public ZooKeeperReadClient(String path) {
if (path.lastIndexOf("/") == (path.length() - 1)) {
path = path.substring(0, path.length() - 1);
}
pathDataMap.put(path, null);
init();
}
public ZooKeeperReadClient(List<String> pathList) {
for (int i = 0; i < pathList.size(); i++) {
pathDataMap.put(pathList.get(i), null);
}
init();
}
private void init() {
zooKeeperManager.register(pathDataMap.keySet());
zooKeeperManager.addZooKeeperClient(this);
initData();
}
public void initData() {
ZooKeeper zk = zooKeeperManager.getZk();
Iterator<String> iterator = pathDataMap.keySet().iterator();
while (iterator.hasNext()) {
String path = iterator.next();
try {
byte[] data = zk.getData(path, false, null);
pathDataMap.put(path, data);
} catch (Exception e) {
// TODO Auto-generated catch block
log.warn("Can't get data of path: " + path);
// e.printStackTrace();
continue;
}
}
}
@Override
public void notify(String path, byte[] data) {
if (log.isInfoEnabled()) {
log.info("notify: path" + path + ", data: " + new String(data));
}
if (pathDataMap.keySet().contains(path)) {
pathDataMap.put(path, data);
}
}
public static Map<String, byte[]> getPathDataMap() {
return pathDataMap;
}
}
import com.lenovo.lps.psb.pt.dao.vo.GlobalConfigVO;
import com.lenovo.lps.psb.zookeeper.ZooKeeperManager;
import com.lenovo.lps.psb.zookeeper.pt.PTFactory;
import com.lenovo.lps.psb.zookeeper.pt.api.IPTGlobalConfigZooKeeperUpdateClient;
public class Example {
/**
* @param args
*/
public static void main(String[] args) {
// 初始化
// 利用spring注入时,根据实际参数注入connectString和sessionTimeout,并调用init方法
ZooKeeperManager zooKeeperManager = ZooKeeperManager.getInstance();
zooKeeperManager.setConnectString("127.0.0.1:2181");
zooKeeperManager.setSessionTimeout(5000);
zooKeeperManager.init();
// 客户端监听
GlobalConfigVO clientGlobalConfigVO = new GlobalConfigVO();
PTFactory.createPTGlobalConfigZooKeeperClient(clientGlobalConfigVO);
// 打印GlobalConfigVO,应该与zookeeper中的一致
printGlobalConfigVO(clientGlobalConfigVO);
// 获取IPTGlobalConfigOperator接口,以更新global config相关数据
IPTGlobalConfigZooKeeperUpdateClient globalConfigOperator = PTFactory
.createGlobalConfigOperator();
boolean flag = globalConfigOperator
.updateAesCommonKey("AesCommonKey12");
System.out.println("updateAesCommonKey: " + flag);
// 更新GlobalConfigVO
int i = 6;
GlobalConfigVO vo = new GlobalConfigVO();
vo.setAesCommonkey("AesCommonKey" + i);
vo.setOldAesCommonkey("oldAesCommonkey" + i);
vo.setEncryptType("encryptType" + i);
vo.setGlobalHmackey("globalHmackey" + i);
vo.setGlobalOldhmackey("globalOldhmackey" + i);
vo.setRsaPrivatekey("rsaPrivatekey" + i);
vo.setRsaPublickey("rsaPublickey" + i);
vo.setSignType("signType" + i);
flag = globalConfigOperator.updateGlobalConfig(vo);
System.out.println("updateGlobalConfig: " + flag);
// zookeeper可能有延迟,因此应用在使用该接口得到数据后,如果数据无效,最好重试几次。注意,应用不要使用sleep
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再次打印GlobalConfigVO,应该与上面的set值一致
printGlobalConfigVO(clientGlobalConfigVO);
}
private static void printGlobalConfigVO(GlobalConfigVO vo) {
System.out.println("getAesCommonkey: " + vo.getAesCommonkey());
System.out.println("getOldAesCommonkey: " + vo.getOldAesCommonkey());
System.out.println("getEncryptType: " + vo.getEncryptType());
System.out.println("getGlobalHmackey: " + vo.getGlobalHmackey());
System.out.println("getGlobalOldhmackey: " + vo.getGlobalOldhmackey());
System.out.println("getRsaPrivatekey: " + vo.getRsaPrivatekey());
System.out.println("getRsaPublickey: " + vo.getRsaPublickey());
System.out.println("getSignType: " + vo.getSignType());
}
}
import java.util.Iterator;
import java.util.Map;
import com.lenovo.lps.psb.zookeeper.ZooKeeperManager;
import com.lenovo.lps.psb.zookeeper.ZooKeeperReadClient;
public class ReadDemo {
/**
* @param args
*/
public static void main(String[] args) {
init();
ZooKeeperReadClient readClient = new ZooKeeperReadClient(
UpdateDemo.path);
printMap(readClient.getPathDataMap());
while(true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
printMap(readClient.getPathDataMap());
}
}
private static void init() {
ZooKeeperManager zooKeeperManager = ZooKeeperManager.getInstance();
zooKeeperManager.setConnectString("127.0.0.1:2181");
zooKeeperManager.setSessionTimeout(5000);
zooKeeperManager.init();
}
private static void printMap(Map<String, byte[]> pathDataMap) {
Iterator<String> iterator = pathDataMap.keySet().iterator();
while (iterator.hasNext()) {
String path = iterator.next();
System.out.println("path: " + path + ", data: "
+ new String(pathDataMap.get(path)));
}
}
}
import com.lenovo.lps.psb.zookeeper.ZooKeeperManager;
import com.lenovo.lps.psb.zookeeper.ZooKeeperUpdateClient;
public class UpdateDemo {
public static final String path = "/psb/test";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
init();
ZooKeeperUpdateClient updateClient = new ZooKeeperUpdateClient();
int i = 8;
updateClient.updateData(path, "aaa" + i);
}
private static void init() {
ZooKeeperManager zooKeeperManager = ZooKeeperManager
.getInstance();
zooKeeperManager.setConnectString("127.0.0.1:2181");
zooKeeperManager.setSessionTimeout(5000);
zooKeeperManager.init();
}
}