`
liyonghui160com
  • 浏览: 761166 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Zookeeper .Net客户端代码

阅读更多

 

本来此客户端可以通过NuGet获取,如果会使用NuGet, 则可以使用命令Install-Package ZooKeeperNet(需要最新版本的NuGet)

如果不会,就去 NuGet官网了解http://docs.nuget.org/docs/start-here/using-the-package-manager-console

如果你想自己编译 你可以去GitHub下载源码https://github.com/ewhauser/zookeeper

donet编译时会报出Genrated里的文件无法打开,实际上刚开始是没有的;


最后在网上查了很多资料和源码里的说明文档

ewhauser-zookeeper-a52ff80\src\java\main\org\apache\jute\package.html

ewhauser-zookeeper-a52ff80\src\java\main\org\apache\jute\compiler\package.html,

 原来是hadoop的Rcc(是用JAVA编写的 源文件中可以找到),这个东西作用是src下的zookeeper.jute文件转换为C C++ java的数据结构 好像原来是没有C#的,是后来作者加上的,这里就先不管了,可以用就行,接下来说说怎么生成 ewhauser-zookeeper-a52ff80\src\dotnet\ZooKeeperNet\Generated的文件

我们需要运行ant命令

如果不知道ant,那google把

配置好ant 后 运行

ant -file build.xml



 

这样运行后等待build successfully  你的ewhauser-zookeeper-a52ff80\src\dotnet\ZooKeeperNet\Generated就有文件了

现在就能将zookeeperNet编译为Dll了

我编译的时候发现有MiscUtil.dll不存在的警告 ,所以我还是去把这个dll下载了下来

注意这个客户端必须要用.NET4.0编译

 以下整理过的donet的源文件包,大家参考使用

通过C#代码使用zookeeper

 

Zookeeper的使用主要是通过创建其Nuget ZooKeeperNet包下的Zookeeper实例,并且调用其接口方法进行

 

的,主要的操作就是对znode的增删改操作,监听znode的变化以及处理。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZooKeeperNet;

namespace ZookeeperDemo
{
    class Watcher : IWatcher
    {
        public void Process(WatchedEvent @event)
        {
            if (@event.Type == EventType.NodeDataChanged)
            {
                Console.WriteLine(@event.Path);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZooKeeperNet;

namespace ZookeeperDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法 
            using (ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher()))
            {
                var stat = zk.Exists("/root",true);
                 
                ////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失) 
                //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);

                //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的 
                zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);
                //取得/root节点下的子节点名称,返回List<String> 
                zk.GetChildren("/root", true);
                //取得/root/childone节点下的数据,返回byte[] 
                zk.GetData("/root/childone", true, null);

                //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉
                zk.SetData("/root/childone", "childonemodify".GetBytes(), -1);
                //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本 
                zk.Delete("/root/childone", -1);
            }

        }
    }
}

 

 

 

 

  • 大小: 1 KB
分享到:
评论
1 楼 yicong 2015-11-02  
    
非常感谢!
发现github里面的log4net版本太旧,和我们项目的版本冲突,只能再编译一个zookeepernet。

相关推荐

Global site tag (gtag.js) - Google Analytics