作者:mobiledu2502928043 | 来源:互联网 | 2024-10-15 16:03
实现增删改查,根据zk源码1.zk的连接publicclassMyzkTest{zk的链接地址publicstaticfinalStringzkconnect8
实现增删改查,根据zk源码
1.zk的连接
public class MyzkTest {//zk的链接地址public static final String zkconnect = "82.156.34.121:2181";//超时时间public static final int timeout = 15000;public static void main(String[] s) throws IOException, InterruptedException, KeeperException {ZooKeeper zooKeeper= connect();
}}//连接方法
public static ZooKeeper connect() throws IOException {ZooKeeper zk = new ZooKeeper(zkconnect,timeout,null);System.out.println("zk连接成功");return zk;}
2.增加
//增加
public static void create( ZooKeeper zooKeeper,String node,String data) throws InterruptedException, KeeperException {System.out.println("开始创建节点:"+node+"节点数据为:"+data);List acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;CreateMode createMode = CreateMode.PERSISTENT;zooKeeper.create(node,data.getBytes(),acl,createMode);System.out.println("zk节点创建成功");}
3.删除
//删除
public static void delete(ZooKeeper zooKeeper,String node) throws InterruptedException, KeeperException {Stat stat = zooKeeper.exists(node,false);System.out.println("开始删除节点:"+node+"原来的版本号是::"+stat.getVersion());zooKeeper.delete(node,stat.getVersion());System.out.println("节点删除成功");}
4.修改
//修改
public static void setData(ZooKeeper zooKeeper,String node,String data) throws InterruptedException, KeeperException {//先判断节点是否存在Stat stat = zooKeeper.exists(node,false);System.out.println("开始修改节点:"+node+"原来的版本号是:"+ stat.getVersion());zooKeeper.setData(node,data.getBytes(), stat.getVersion());System.out.println("zk节点修改成功");}
5.查看
//查看
public static void getData(ZooKeeper zooKeeper,String node) throws InterruptedException, KeeperException {byte[] data = zooKeeper.getData(node,false,null);System.out.println("查看节点:"+node+"的数据为:"+new String(data));
}
6.主方法
public static void main(String[] s) throws IOException, InterruptedException, KeeperException {ZooKeeper zooKeeper= connect();//create(zooKeeper,"/myzktest","/lll");//delete(zooKeeper,"/myzktest");//setData(zooKeeper,"/myzktest","/sss");getData(zooKeeper,"/myzktest");
}
总体图片:
排查问题的步骤:
第一步:先看一下程序有没有正常启动,lsof -i:2181;
如果说有输出,说明程序正常启动。
如果没有输出,说明程序没有正常启动,然后重新启动程序。
第二步:如果程序正常启动,本地还是访问不到。
先检查防火墙,再检查安全组。只有这两个都开放端口了,才说明这个端口开放了。