集成zookeeper

依赖
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.0</version>
</dependency>
集成
  • 创建ZookeeperWatcher类,实现Watcher类。
package com.ct.zookeeper;

import org.apache.zookeeper.*;


public class ZookeeperWatcher implements Watcher {
    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("connect success");
        }
    }
}
  • 创建client
package com.ct.zookeeper;

import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class ZookeeperUtils {

    public static ZooKeeper getClient(String connectString) {
        try {
            return new ZooKeeper(connectString, 30000, new ZookeeperWatcher());
        } catch (IOException e) {
            //
            System.out.println("create client failed");
        }
        return null;
    }
}

client实现之后,就可以像命令行一样操作服务器。

  • 测试一下
package zookeeper;

import com.ct.zookeeper.ZookeeperUtils;
import com.ct.zookeeper.ZookeeperWatcher;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.util.List;

public class zookeeperTest {

    private static ZooKeeper zooKeeperClient;
    private static final String mockPath = "/testNode";
    private static final String msg = "hello zookeeper";

    static {
        zooKeeperClient = ZookeeperUtils.getClient("192.168.2.113:2181");
    }

    @Test
    public void createTest() throws Exception {
        // exists
        Stat exists = zooKeeperClient.exists(mockPath, new ZookeeperWatcher());
        System.out.println("path=" + exists);

        Stat exists2 = zooKeeperClient.exists("/testNode01", new ZookeeperWatcher());
        System.out.println("path01=" + exists2);

        if (exists == null) {
            //create
            String path = zooKeeperClient.create(mockPath, msg.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(path);

            //get
            byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
            System.out.println(new String(data));

            List<ACL> acl = zooKeeperClient.getACL(mockPath, new Stat());
            for (ACL a : acl) {
                System.out.println(a);
            }
        }

        // get children
        List<String> children = zooKeeperClient.getChildren("/", new ZookeeperWatcher(), new Stat());
        for (String c : children) {
            System.out.println(c);
        }
    }

    @Test
    public void updateTest() throws Exception {
        Stat stat = zooKeeperClient.setData(mockPath, "hello world".getBytes(), -1);
        byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
        System.out.println(stat);
        System.out.println(new String(data));
    }


    @Test
    public void deleteTest() throws Exception {
        Stat exists = zooKeeperClient.exists(mockPath, false);
        if (exists == null) {
           zooKeeperClient.create(mockPath, msg.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
        System.out.println("old data=" + new String(data));

        zooKeeperClient.delete(mockPath, -1);

        exists = zooKeeperClient.exists(mockPath, false);
        if (exists != null) {
            data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
            System.out.println("data=" + new String(data));
        }
    }
}

关于ACL和集群,后面再写,一步一步来。

Java和Zookeeper是广泛使用于分布式系统中的两个重要的技术。其中Java是一种面向对象的编程语言,而Zookeeper是一个分布式的开源软件项目,被广泛用于分布式系统的协调和管理。由于分布式系统具有高可用性、可扩展性和高性能等特点,Java和Zookeeper的结合使得分布式系统在协调和管理方面能够更加完善和灵活。 Java通过提供Zookeeper客户端API来实现集成ZookeeperZookeeper客户端API提供了许多API方法,可以直接与Zookeeper集合进行交互,包括创建节点、获取节点数据、监控节点变化等。通过使用Java与Zookeeper进行集成,可以使开发者更加方便地使用Zookeeper来处理分布式系统的问题。 在Java应用程序中集成Zookeeper后,开发者可以使用Zookeeper来实现分布式锁、分布式队列、服务发现等功能。例如,在互联网应用程序的负载均衡中,可以使用Zookeeper管理这些应用程序,并利用Java访问Zookeeper,以实现应用程序的动态调整和负载均衡。此外,Zookeeper还可以用于配置的管理和监控,以实现分布式系统的自动化管理。 总而言之,Java与Zookeeper集成可以让分布式系统更加灵活,并为开发者提供了更多的选择。Java提供了对Zookeeper客户端API的支持,使得Java应用程序可以更加方便地使用Zookeeper进行分布式协调和管理。使用Java来集成Zookeeper,可以使开发者更加方便地实现分布式锁、分布式队列、服务发现等功能,并可以通过使用Zookeeper来处理分布式系统的配置、管理和监控。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值