From 4d71a0e6dc6eff23c463607c8947008080f45c19 Mon Sep 17 00:00:00 2001 From: sohudo Date: Tue, 15 Dec 2015 17:49:34 +0800 Subject: [PATCH] del ZkCreate.java --- src/main/java/demo/ZkCreate.java | 142 ------------------------------- 1 file changed, 142 deletions(-) delete mode 100644 src/main/java/demo/ZkCreate.java diff --git a/src/main/java/demo/ZkCreate.java b/src/main/java/demo/ZkCreate.java deleted file mode 100644 index d9e425099..000000000 --- a/src/main/java/demo/ZkCreate.java +++ /dev/null @@ -1,142 +0,0 @@ -package demo; - -import com.alibaba.fastjson.JSON; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.ExponentialBackoffRetry; -import org.apache.curator.utils.ZKPaths; -import org.apache.zookeeper.data.Stat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; - -import java.io.InputStream; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -/** - * Created by v1.lion on 2015/10/7. - */ -public class ZkCreate { - private static final String ZK_CONFIG_FILE_NAME = "/zk-create.yaml"; - private static final Logger LOGGER = LoggerFactory.getLogger(ZkCreate.class); - - - private static final String CONFIG_URL_KEY = "zkURL"; - - private static final String MYCAT_CLUSTER_KEY = "mycat-cluster"; - private static final String MYCAT_ZONE_KEY = "mycat-zones"; - private static final String MYCAT_NODES_KEY = "mycat-nodes"; - private static final String MYCAT_HOST_KEY = "mycat-hosts"; - private static final String MYCAT_MYSQLS_KEY = "mycat-mysqls"; - private static final String MYCAT_MYSQL_GROUP_KEY = "mycat-mysqlgroup"; - - private static CuratorFramework framework; - private static Map zkConfig; - - public static void main(String[] args) { - zkConfig = loadZkConfig(); - framework = - createConnection((String) zkConfig.getOrDefault(CONFIG_URL_KEY, "127.0.0.1:2181")); - - createConfig(MYCAT_HOST_KEY, false, MYCAT_HOST_KEY); - createConfig(MYCAT_ZONE_KEY, false, MYCAT_ZONE_KEY); - createConfig(MYCAT_NODES_KEY, true, MYCAT_NODES_KEY); - createConfig(MYCAT_CLUSTER_KEY, true, MYCAT_CLUSTER_KEY); - createConfig(MYCAT_MYSQLS_KEY, true, MYCAT_MYSQLS_KEY); - createConfig(MYCAT_MYSQL_GROUP_KEY, true, MYCAT_MYSQL_GROUP_KEY); - } - - private static void createConfig(String configKey, boolean filterInnerMap, - String configDirectory, String... restDirectory) { - String childPath = ZKPaths.makePath("/" + "mycat", configDirectory, restDirectory); - LOGGER.trace("child path is {}", childPath); - - try { - Stat systemPropertiesNodeStat = framework.checkExists().forPath(childPath); - if (systemPropertiesNodeStat == null) { - framework.create().creatingParentsIfNeeded().forPath(childPath); - } - - Object mapObject = zkConfig.get(configKey); - //recursion sub map - if (mapObject instanceof Map) { - createChildConfig(mapObject, filterInnerMap, childPath); - return; - } - - framework.setData().forPath(childPath, JSON.toJSONString(mapObject).getBytes()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void createChildConfig(Object mapObject, boolean filterInnerMap, - String childPath) { - if (mapObject instanceof Map) { - Map innerMap = (Map) mapObject; - innerMap.forEach((key, value) -> { - if (value instanceof Map) { - createChildConfig(value, filterInnerMap, - ZKPaths.makePath(childPath, String.valueOf(key))); - } else { - LOGGER.trace("sub child path is {}", childPath); - processLeafNode(innerMap, filterInnerMap, childPath); - } - }); - } - } - - private static void processLeafNode(Map innerMap, boolean filterInnerMap, - String childPath) { - try { - Stat restNodeStat = framework.checkExists().forPath(childPath); - if (restNodeStat == null) { - framework.create().creatingParentsIfNeeded().forPath(childPath); - } - - if (filterInnerMap) { - Map filteredSubItem = innerMap.entrySet().stream() - .filter(mapEntry -> !(mapEntry.getValue() instanceof Map)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - - framework.setData() - .forPath(childPath, JSON.toJSONString(filteredSubItem).getBytes()); - } else { - framework.setData().forPath(childPath, JSON.toJSONString(innerMap).getBytes()); - } - } catch (Exception e) { - LOGGER.error("create node error: {} ", e.getMessage(), e); - throw new RuntimeException(e); - } - } - - private static Map loadZkConfig() { - InputStream configIS = ZkCreate.class.getResourceAsStream(ZK_CONFIG_FILE_NAME); - if (configIS == null) { - throw new RuntimeException("can't find zk properties file : " + ZK_CONFIG_FILE_NAME); - } - return (Map) new Yaml().load(configIS); - } - - private static CuratorFramework createConnection(String url) { - CuratorFramework curatorFramework = - CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6)); - - //start connection - curatorFramework.start(); - //wait 3 second to establish connect - try { - curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS); - if (curatorFramework.getZookeeperClient().isConnected()) { - return curatorFramework; - } - } catch (InterruptedException ignored) { - } - - //fail situation - curatorFramework.close(); - throw new RuntimeException("failed to connect to zookeeper service : " + url); - } -}