ZK

ZK的架构

1.一个ZK集群中由一组Server节点构成
2.这一组Server节点中存在一个角色为Leader的节点,剩下的其他的节点都是Follower
3.当客户端Client连接到ZK集群时,并且执行读写请求,这些请求会被发送到Leader节点上
4.然后Leader上数据变更会同步到集群的其他的Follower节点
5.Leader节点接收到数据变更请求之后,首先将变更写入到本地磁盘(为了以后容错、恢复之用),
当所有的写请求持久化到磁盘之后,才会将变更应用到内存中
6.当当前的Leader发生故障无法提供服务之后,故障/失败容错是快速响应的,会从Follower中选择一个做为Leader(选择),新的Leader继续做为分布式协调服务的中心,处理所有Client发起的请求。

多数server:

只要集群中有过半的机器是正常工作的,那么整个集群对外就是可用的

Zookeeper核心

  1. 仲裁机制,
  2. 目录式数据结构。

Znode的两种类型:

1)短暂的
客户端会话结束,那么zk就会将该类型的node删除。
2)持久的
就依赖于客户端会话,只有当客户端明确的使用命令或者是API的方式要删除该节点时才会被删除。

用API的方式创建ZK节点

import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class ZKchuangjian {
    private ZooKeeper zooKeeper;  
    private final int SESSION_TIMEOUT=30000;  //超时时间
    private final String path="/info-node";  //节点名称
    @Before
    //连接到ZK
    public void init() throws IOException {
        zooKeeper=new ZooKeeper("hadoop-node3", SESSION_TIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
       //判断是否有这个节点如果有则删除
                if(watchedEvent.getType()== Event.EventType.NodeDeleted){
                    System.out.println("节点被删除了");
                    try {
                        createNode();
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    public void createNode() throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists(path,true);
        //if判断没有则创建
        if(stat == null){
            System.out.println("开始创建节点");
            zooKeeper.create(path,"test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            //PERSISTENT表示永久,EPHEMERAL_SEQUENTIAL表示临时
            System.out.println("节点创建成功");
            zooKeeper.exists(path,true);
            Thread.sleep(1000);
        }else {

            System.out.println("节点存在");
        }
    }
    //断开连接
    @After
    public void close() throws InterruptedException {
        zooKeeper.close();
    }
    //run方法
    @Test
    public void run() throws KeeperException, InterruptedException, IOException {
        createNode();
        System.in.read();
    }
}

用API的方式删除ZK节点

import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;

public class ZKshanchu {
    private ZooKeeper zooKeeper;
    private final int SESSION_TIMEOUT=30000;//超时时间
    private final String path="/info-node";  //节点名称
    @Before
    //连接ZK
    public void kaishi() throws IOException {
        zooKeeper=new ZooKeeper("hadoop-node3", SESSION_TIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if(watchedEvent.getType()== Event.EventType.NodeCreated){
                    System.out.println("节点存在开始删除");
                    try {
                        deleteNode();
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    //run方法
    @Test
    public void run() throws KeeperException, InterruptedException, IOException {
        deleteNode(); //执行deleteNode的方法
        System.in.read();
    }
    public void deleteNode() throws KeeperException, InterruptedException {
        Stat stat=zooKeeper.exists(path,true);
        //判断是否存在
        if(stat!=null){
            System.out.println("节点存在");
            zooKeeper.delete(path,-1);
            System.out.println("创建的节点被删除了");
            zooKeeper.exists(path,true);
        }else {
        //没有则输出不存在.
            System.out.println("不存在存在");
        }
    }
    //断开连接
    @After
    public void end() throws InterruptedException {
        zooKeeper.close();
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值