TL--3--zookeeper--2

标签: TL3期-zookeeper

ACL的问题,即权限的问题。客户端设置的时候解决这个问题。

是或的运算就是相加的运算。

加入三个权限。

上节课遗漏的地方?

第一步:echo -n luban:123456 | openssl dgst -binary -sha1 | openssl base64

2Rz3ZtRZEs5RILjmwuXW/wT13Tk=

第二步:setAcl /luban2/xiaoluban78 digest:luban:2Rz3ZtRZEs5RILjmwuXW/wT13Tk=:ra

第三步:addauth digest luban:123456

第四步:

代码:

public class Datatest {
    ZooKeeper zooKeeper;

    @Before
    public void init() throws IOException {
        String conn = "10.211.55.10:2181";
        zooKeeper = new ZooKeeper(conn,4000,new Watcher(){
            public void process(WatchedEvent event){
                System.out.println(8888);
                System.out.println(event.getPath());
            }
        });
    }

    @Test
    public void getData() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/luban2",false,null);
        System.out.println(new String(data));
        System.out.println(123);
    }

    @Test
    public void getDataWatch() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/luban2",true,null);
        System.out.println(new String(data));
        System.out.println(123);
        Thread.sleep(Long.MAX_VALUE);
    }

    @Test
    public void getData3() throws KeeperException, InterruptedException {
        Stat stat = new Stat();
        zooKeeper.getData("/luban2", new Watcher() {
            public void process(WatchedEvent event) {
                try {
                    zooKeeper.getData(event.getPath(),this,null);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(event.getPath());
            }
        },stat);
        System.out.println(stat);
        Thread.sleep(Long.MAX_VALUE);
    }

    @Test
    public void getChildren() throws KeeperException, InterruptedException {
       List<String> list = zooKeeper.getChildren("/luban2",false);
       list.stream().forEach(System.out::println);
    }

    @Test
    public void getChildrenWatch() throws KeeperException, InterruptedException {
        List<String> list = zooKeeper.getChildren("/luban2",event->{
            //  数据的修改是不会被触发的
            System.out.println(event.getPath());
            try {
                zooKeeper.getChildren(event.getPath(),false);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        list.stream().forEach(System.out::println);
        Thread.sleep(Long.MAX_VALUE);
    }

    @Test
    public void getCallBack() throws KeeperException, InterruptedException {
        zooKeeper.getData("/luban2", false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
                // i是是不是成功的 s是  ctx是传的低下那个值 data是返回的结果
                System.out.println(stat);
            }
        },"123");
        Thread.sleep(Long.MAX_VALUE);
    }

    @Test
    public void create() throws KeeperException, InterruptedException {
        // ACL是必须填的
        List<ACL> list = new ArrayList<>();
        int perm = ZooDefs.Perms.ADMIN|ZooDefs.Perms.READ;
        // 用数字位移的方式来编写
        int c = 1<<0;
        int D = 1<<1;
        int Q = 1<<2;
        ACL acl1 = new ACL(perm,new Id("world","anyone"));
        ACL acl2 = new ACL(perm,new Id("ip","10.211.55.10"));
        ACL acl3 = new ACL(perm,new Id("ip","127.0.0.1"));
        list.add(acl1);
        list.add(acl2);
        list.add(acl3);
        zooKeeper.create("/luban2/xiaoluban78","hello".getBytes(),list,CreateMode.PERSISTENT);
    }
}

zkClient:

api

-------

下半节课

--------

我的集群在10.211.55.10

zookeeper集群。

伪集群的搭建:https://blog.csdn.net/vbirdbest/article/details/82688462

第一步:在datadir下创建myid文件夹

第二步:写配置文件

第三步:启动三个集群

zkServer.sh start /usr/local/apps/zookeeper/zookeeper02/zookeeper/conf/zoo.cfg
zkServer.sh start /usr/local/apps/zookeeper/zookeeper03/zookeeper/conf/zoo.cfg
zkServer.sh start /usr/local/apps/zookeeper/zookeeper/conf/zoo.cfg

第四步:查看状态

zkServer.sh status /usr/local/apps/zookeeper/zookeeper/conf/zoo.cfg
zkServer.sh status /usr/local/apps/zookeeper/zookeeper02/zookeeper/conf/zoo.cfg
zkServer.sh status /usr/local/apps/zookeeper/zookeeper03/zookeeper/conf/zoo.cfg

 

版权声明:本文为qq_28764557原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_28764557/article/details/105103322