package org.mycat.web.util;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Preconditions;
/**
* zookeeper公共操作的curator框架实现,单例
*/
public final class ZookeeperCuratorHandler {
private static final Logger LOG = LoggerFactory
.getLogger(ZookeeperCuratorHandler.class);
private CuratorFramework client = null;
private StateListener listener = new StateListener();
private String errorWithNullClient = "zookeeper CuratorFramework is null, please invoke connect method first";
// private String zookeeper;
private final String zooKey="zookeeper";
private static class SingletonHolder {
private static ZookeeperCuratorHandler instance = new ZookeeperCuratorHandler();
}
private ZookeeperCuratorHandler() {
}
public static ZookeeperCuratorHandler getInstance() {
return SingletonHolder.instance;
}
public List<String> getChildrenName(String parentNodePath) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
return client.getChildren().forPath(parentNodePath);
}
public synchronized boolean connect(String host, String nameSpace) {
boolean blockUntilConnected = false;
if (client == null
|| client.getState() != CuratorFrameworkState.STARTED) {
Preconditions.checkArgument(StringUtils.isNotBlank(host),
"zk host cannot be empty");
nameSpace = (nameSpace == null ? "" : nameSpace);
try {
LOG.info("start to connect zookeeper[{}] with namespace[{}]",
host, nameSpace);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory
.builder();
client = builder.connectString(host).sessionTimeoutMs(60000)
.connectionTimeoutMs(3000).canBeReadOnly(false)
.defaultData("".getBytes("UTF-8"))
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.namespace(nameSpace).build();
listener = new StateListener();
client.getConnectionStateListenable().addListener(listener);
client.start();
blockUntilConnected = client.blockUntilConnected(5000,
TimeUnit.MILLISECONDS);
if (blockUntilConnected) {
LOG.info(
"connect zookeeper[{}] with namespace[{}] successful",
host, nameSpace);
createMainPath();
} else {
disconnect();
throw new Exception("fail to connect zookeeper server["
+ host + "] with namespace[" + nameSpace + "]");
}
} catch (Exception e) {
LOG.error("fail to connect zookeeper server[" + host
+ "] with namespace[" + nameSpace + "]", e);
}
}
return blockUntilConnected;
}
public boolean isConnected() {
return client != null
&& client.getState() == CuratorFrameworkState.STARTED;
}
private static class StateListener implements ConnectionStateListener {
public void stateChanged(CuratorFramework client,
ConnectionState newState) {
switch (newState) {
case LOST: // 一旦丢失链接,就意味着zk server端已经删除了锁数据 lockedThread.clear();
LOG.info("ZK-LOST.....");
break;
case SUSPENDED:
LOG.info("ZK-SUSPENDED.....");
break;
case CONNECTED: // 一旦丢失链接,就意味着zk server端已经删除了锁数据
// lockedThread.clear();
LOG.info("ZK-CONNETED.....");
break;
case RECONNECTED: // 一旦丢失链接,就意味着zk server端已经删除了锁数据
// lockedThread.clear();
LOG.info("ZK-RECONNECTED.....");
break;
case READ_ONLY:
LOG.info("ZK-READ_ONLY.....");
break;
default:
LOG.info("ZK-" + newState.toString());
break;
}
}
}
public synchronized void disconnect() {
if (client != null) {
try {
client.getConnectionStateListenable().removeListener(listener);
client.close();
} catch (Exception e) {
}
}
}
public void UpdateZkConfig(String zkinfo) throws Exception {
Properties properties = new Properties();
properties.load(ZookeeperCuratorHandler.class.getClassLoader()
.getResourceAsStream("mycat.properties"));
properties.setProperty(zooKey, zkinfo);
String realPath = ZookeeperCuratorHandler.class.getClassLoader()
.getResource("mycat.properties").getPath();
OutputStream out = new FileOutputStream(realPath);
System.out.println("realPath : " + realPath);
properties.store(out, "###ZK CONFIG");
}
public void createEphemeralNode(String path, String data) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
data = data == null ? "" : data;
client.create().creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.forPath(path, data.getBytes(Constant.CHARSET));
}
public String createSeqNode(String path, String data) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
String rePath = client.create().creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath(path, data.getBytes(Constant.CHARSET));
return rePath;
}
public void createEphemeralSeqNode(String path, String data)
throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
data = data == null ? "" : data;
client.create().creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(path, data.getBytes(Constant.CHARSET));
}
public boolean existsNode(String path) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
Stat stat = client.checkExists().forPath(path);
if (stat == null) {
return false;
} else {
return true;
}
}
public void createEphemeralNode(String path) throws Exception {
createEphemeralNode(path, null);
}
public void deleteNode(String path) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
client.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
}
public void deleteChildrenNodes(String path) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
List<String> childrenName = getChildrenName(path);
for (String childrenNode : childrenName) {
client.delete().guaranteed().deletingChildrenIfNeeded()
.forPath(path + "/" + childrenNode);
}
}
public String getNodeData (String path) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
byte[] byteData = client.getData().forPath(path);
String rep = new String(byteData, Constant.CHARSET);
return rep;
}
public Map<String,Object> getNodeDataForMap(String path) throws Exception{
Preconditions.checkNotNull(client, errorWithNullClient);
Stat stat = new Stat();
byte[] nodeData = client.getData().storingStatIn(stat).forPath(path);
String dataNode = new String(nodeData);
return JsonUtils.json2Map(dataNode);
}
public String getNodeData(String path, Stat stat) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
byte[] byteData = client.getData().storingStatIn(stat).forPath(path);
return new String(byteData, Constant.CHARSET);
}
public void setNodeData(String path, String data) throws Exception {
Preconditions.checkNotNull(client, errorWithNullClient);
client.setData().forPath(path, data.getBytes(Constant.CHARSET));
}
public void setNodeDataWithVersion(Stri
没有合适的资源?快使用搜索试试~ 我知道了~
Mycat-web-1.0-20160617.083134-5-20160617163048-win.zip
共890个文件
js:129个
png:120个
css:109个
需积分: 10 13 下载量 29 浏览量
2016-07-14
17:56:37
上传
评论
收藏 51.81MB ZIP 举报
温馨提示
该附件是官网上原版的,附件是一个war包,有需要的可以自行下载,想在linux上运行的请不要下载该版本,可以去下载我上传的linux下的版本进行安装,具体安装方法请自行在网上查看。附linux安装包下载地址:http://download.csdn.net/detail/liuxingsiye/9576233
资源推荐
资源详情
资源评论
收起资源包目录
Mycat-web-1.0-20160617.083134-5-20160617163048-win.zip (890个子文件)
start.bat 381B
ZookeeperCuratorHandler.class 20KB
JMServer.class 17KB
ZkConfigService.class 12KB
DataSourceUtils.class 12KB
MySqlRepService.class 10KB
JMConnManager.class 10KB
DispatcherAction.class 10KB
ShowService.class 9KB
DruidConnectionHolder.class 8KB
AbstractConfigSevice.class 7KB
SNMPService.class 6KB
MycatService.class 6KB
MysqlService.class 5KB
JMXService.class 5KB
ZkTestReadConfig.class 5KB
MailService.class 5KB
MailConfigUtils.class 4KB
LBServer.class 4KB
SyncSysSqtable.class 4KB
TaskManger.class 4KB
TaskStartupListener.class 4KB
JrdsUtils.class 3KB
JMJSonCfgLoader.class 3KB
JitService.class 3KB
JitModel.class 3KB
TaskManger$2.class 3KB
CheckMycatSuspend.class 3KB
SyncSysSqlsum.class 3KB
JMEevntCenter.class 3KB
MycatServer.class 3KB
SyncSysSqlslow.class 3KB
SyncSysSqlhigh.class 3KB
SyncSysSql.class 3KB
ShowMycatProcessor.class 3KB
DBSourceService.class 3KB
SyncClearData.class 3KB
ExplainService.class 3KB
MySqlRep.class 3KB
DataHost.class 2KB
RuleMapFileConfigService.class 2KB
CheckServerDown.class 2KB
MycatNodeService.class 2KB
MycatHostService.class 2KB
MyCatZoneService.class 2KB
MycatMysqlsService.class 2KB
BlockSqlService.class 2KB
DataHostService.class 2KB
SchemaService.class 2KB
TableService.class 2KB
ChildrenTableService.class 2KB
UserService.class 2KB
SequenceService.class 2KB
DataNodeService.class 2KB
RuleService.class 2KB
ZookeeperCuratorHandler$StateListener.class 2KB
MailUtil.class 2KB
Menu.class 2KB
TaskManger$1.class 2KB
JsonUtils.class 2KB
MySqlServer.class 2KB
MycatHost.class 2KB
MMDataGrid.class 2KB
JavaBeanToMapUtil.class 2KB
MyCatNode.class 2KB
PublishServiceStartupListener.class 2KB
JMConnBean.class 2KB
SqliteStore.class 1KB
Constant.class 1KB
ZtreeModel.class 1KB
LBServer$Zone.class 1KB
Rainbowlistener.class 1KB
TestMailLog.class 1KB
DataSourceUtils$MycatPortType.class 1KB
ZookeeperCuratorHandler$1.class 1KB
ChildrenTable.class 1KB
Rule.class 1KB
User.class 1KB
SchemaTable.class 1KB
Table.class 1KB
SchemaOrders.class 1KB
Schema.class 1KB
SystemParams.class 1KB
ZookeeperCuratorHandler$2.class 1KB
LBServer$Node.class 1KB
MySqlGroup.class 1KB
LBServer$Mycat.class 1020B
LBServer$Mysql.class 1014B
Schema.class 992B
Sequence.class 978B
TaskManger$2$1.class 968B
SchemaChildTable.class 951B
MycatCluster.class 948B
DataNode.class 943B
MailUtil$1.class 931B
DataSourceUtils$1.class 891B
LBServer$LBGroup.class 828B
ZookeeperCuratorHandler$SingletonHolder.class 825B
BlockSql.class 734B
MycatZone.class 724B
共 890 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
胖虎0_0
- 粉丝: 19
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功