zoukankan      html  css  js  c++  java
  • 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-a52ff80srcjavamainorgapachejutepackage.html

    ewhauser-zookeeper-a52ff80srcjavamainorgapachejutecompilerpackage.html,

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

    我们需要运行ant命令

    如果不知道ant,那google把

    配置好ant 后 运行

    ant -file build.xml



     

    这样运行后等待build successfully  你的ewhauser-zookeeper-a52ff80srcdotnetooKeeperNetGenerated就有文件了

    现在就能将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);
                }
    
            }
        }
    }
    
     
    复制代码

    本来此客户端可以通过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-a52ff80srcjavamainorgapachejutepackage.html

    ewhauser-zookeeper-a52ff80srcjavamainorgapachejutecompilerpackage.html,

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

    我们需要运行ant命令

    如果不知道ant,那google把

    配置好ant 后 运行

    ant -file build.xml



     

    这样运行后等待build successfully  你的ewhauser-zookeeper-a52ff80srcdotnetooKeeperNetGenerated就有文件了

    现在就能将zookeeperNet编译为Dll了

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

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

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

    通过C#代码使用zookeeper

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

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

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using ZooKeeperNet;  
    6.   
    7. namespace ZookeeperDemo  
    8. {  
    9.     class Watcher : IWatcher  
    10.     {  
    11.         public void Process(WatchedEvent @event)  
    12.         {  
    13.             if (@event.Type == EventType.NodeDataChanged)  
    14.             {  
    15.                 Console.WriteLine(@event.Path);  
    16.             }  
    17.         }  
    18.     }  
    19. }  
    20.   
    21. using System;  
    22. using System.Collections.Generic;  
    23. using System.Linq;  
    24. using System.Text;  
    25. using ZooKeeperNet;  
    26.   
    27. namespace ZookeeperDemo  
    28. {  
    29.     class Program  
    30.     {  
    31.         static void Main(string[] args)  
    32.         {  
    33.   
    34.             //创建一个Zookeeper实例,第一个参数为目标服务器地址和端口,第二个参数为Session超时时间,第三个为节点变化时的回调方法   
    35.             using (ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", new TimeSpan(0, 0, 0, 50000), new Watcher()))  
    36.             {  
    37.                 var stat = zk.Exists("/root",true);  
    38.                    
    39.                 ////创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失)   
    40.                 //zk.Create("/root", "mydata".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);  
    41.   
    42.                 //在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的   
    43.                 zk.Create("/root/childone", "childone".GetBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent);  
    44.                 //取得/root节点下的子节点名称,返回List<String>   
    45.                 zk.GetChildren("/root", true);  
    46.                 //取得/root/childone节点下的数据,返回byte[]   
    47.                 zk.GetData("/root/childone", true, null);  
    48.   
    49.                 //修改节点/root/childone下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉  
    50.                 zk.SetData("/root/childone", "childonemodify".GetBytes(), -1);  
    51.                 //删除/root/childone这个节点,第二个参数为版本,-1的话直接删除,无视版本   
    52.                 zk.Delete("/root/childone", -1);  
    53.             }  
    54.   
    55.         }  
    56.     }  
    57. }  
    58.   
    59.    
     
     
  • 相关阅读:
    Java中Io类-File类的构造方法
    hadoop的wordcount例子运行
    关于“javax.servlet.include.request_uri”属性值
    如何高效地分析框架源码
    代码重构的技巧——合理使用@Deprecated
    spring的事件机制
    在eclipse中使用jetty插件替代m2e开发调试maven web项目
    maven多配目配置总结
    如何禁止anonymous用户访问nexus
    efront二次开发记要
  • 原文地址:https://www.cnblogs.com/soundcode/p/10872710.html
Copyright © 2011-2022 走看看