zoukankan      html  css  js  c++  java
  • Etcd学习(一)安装和.NETclient測试

    Etcd是一个比較新的分布式协调框架,由CoreOS的开发团队开发,如今才仅仅到0.4.6版本号,还没公布1.0版本号

    我看了一下GitHub上作者们的提交记录,如今应该还在如火如荼的开发以及改动Bug中,预计要有点耐心再等一等了。。。

    并且2014年7月25号CoreOS(server操作系统)公布了自己的第一个稳定版本号,当中包含:

    • Linux 3.15.2
    • Docker 1.0.1
    • Support on all major cloud providers, including Rackspace Cloud, Amazon EC2 (including HVM), and Google Compute Engine
    • Commercial support via CoreOS Managed Linux
    可是他们说为了这个版本号的CoreOS的稳定,临时决定不把Etcd和Fleet包括进来。。。这两个玩意的稳定版本号之后才会搞进CoreOS。。。


    从网上搜etcdkeyword,基本上就仅仅能看到“开源中国”的介绍:

    etcd 是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。etcd 的灵感来自于 ZooKeeper 和 Doozer,側重于:

    • 简单:支持 curl 方式的用户 API (HTTP+JSON)
    • 安全:可选 SSL client证书认证
    • 高速:单实例可达每秒 1000 次写操作
    • 可靠:使用 Raft 实现分布式

    Etcd is written in Go and uses the raft consensus algorithm to manage a highly-available replicated log.


    一、安装和測试

    安装很easy,大概经历下面几个步骤吧:

    1、下载VMWare9.0虚拟机和一个Ubuntu12 desktop版的ISO,安装好Ubuntu,以及root用户,vim软件等的初始化设置。(必须安装Linux-64bit,否则执行时etcd会出错)

    2、下载Go语言编译器,用来编译Etcd的,下载地址是:http://tip.golang.so/dl/,上面有解压说明教程。

    3、下载CURL,用于在Linux终端发送HTTP请求到Etcdserver,网上有它的编译和安装教程,比方这个http://blog.csdn.net/lifan5/article/details/7350154

    4、下载etcd源代码,到GitHub下载就能够,下载了以后解压,然后用Go编译器进行编译,它的GitHub网页上有说明。

    5、环境都搞完了以后就能够启动etcd了,然后你能够先照着上面的API教程操作一把:https://github.com/coreos/etcd/blob/master/Documentation/api.md


    二、.NETclient代码測试

    我准备先开发.NET的client,由于这个眼下急需。

    下载 drusellers/etcetera

    然后用VS202打开自行编译成DLL,将DLL增加到自己的新的project中。


    1、节点存活周期測试:

    普通创建的节点(不加TTL)在ETCD崩溃或者重新启动之后,不会被删除,下次再启动Etcd这些节点还会在,值也不会变。


    2、节点监视

    设置和监视单层节点,正常工作:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {
                client.Set("config1", "value1");
                client.Watch("config1", FollowUp_Config);
    
                Thread.Sleep(3000);
    
                client.Set("config1", "value2"); //watch回调函数会被触发

    设置和监视多层节点,将无论用:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {
                client.Set("config1/x", "value1");
                client.Watch("config1/x", FollowUp_Config);
    
                Thread.Sleep(3000);
    
                client.Set("config1/x", "value2"); //watch回调函数不会被触发

    由于config1和你预想的不一样,实际上config1不是一个文件夹,由于内部实现没有解析这种格式,然后为config1创建一个相应的文件夹。

    而是提供了一个createDir()的接口来创建文件夹。


    所以在为config1创建了文件夹的前提下,监视config1/x的watch回调函数才会被触发:

    client.CreateDir("config1");
    
                client.Set("config1/x", "value1");
                client.Watch("config1/x", FollowUp_Config);
    
                Thread.Sleep(3000);
    
                client.Set("config1/x", "value2"); //watch回调函数会被触发


    另外更重要的一点,设置对节点的watch仅仅是一次有效的!!!

    比方以下代码:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {           
                client.Watch("conf", FollowUp_Config);
    
                Thread.Sleep(2000);
                client.Set("conf", "value1");  //会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Set("conf", "value2");  //不会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Set("conf", "valuejiyiqin");    //不会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Delete("conf"); //不会触发watch回调函数

    必需要连续设置watch才会有效:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {           
                client.Watch("conQ", FollowUp_Config);
                Thread.Sleep(2000);
                client.Set("conQ", "value1");  //会触发watch回调函数
    
                client.Watch("conQ", FollowUp_Config);
                Thread.Sleep(2000);
                client.Set("conQ", "value2");  //会触发watch回调函数
    
                client.Watch("conQ", FollowUp_Config);
                Thread.Sleep(2000);
                client.Delete("conQ"); //会触发watch回调函数

    3、文件夹监视

    假设要监视config1文件夹,须要在Watch函数的第三个參数recursive = true?指定为true,这样才会触发回调函数。

    并且监视仅仅是一次有效,比方这样:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {
                client.CreateDir("config2");
                client.Watch("config2", FollowUp_Config, true);
    
                Thread.Sleep(2000);
                client.Set("config2/x", "value1");  //会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Set("config2/x", "value2");  //不会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Set("config2/y", "valuejiyiqin");    //不会触发watch回调函数
    
                Thread.Sleep(2000);
                client.Delete("config2/x"); //不会触发watch回调函数

    这样才会全部在这个文件夹以下的增删该操作都会触发回调函数:

    static string registryCenterIP = "192.168.195.128";
            static int registryCenterPort = 4001;
            static string sysFlag = "systemA";
            static string URL = "http://" + registryCenterIP + ":" + registryCenterPort + "/v2/keys/" + sysFlag + "/";
            static EtcdClient client = new EtcdClient(new Uri(URL));
    
            static void Main(string[] args)
            {
                client.CreateDir("config2");
    
                client.Watch("config2", FollowUp_Config, true);
                Thread.Sleep(2000);
                client.Set("config2/x", "value1"); //会触发
    
                client.Watch("config2", FollowUp_Config, true);
                Thread.Sleep(2000);
                client.Set("config2/x", "value2"); //会触发
    
                client.Watch("config2", FollowUp_Config, true);
                Thread.Sleep(2000);
                client.Set("config2/y", "valuejiyiqin"); //会触发
    
                client.Watch("config2", FollowUp_Config, true);
                Thread.Sleep(2000);
                client.Delete("config2/x"); //会触发

    4、同一个进程下监视多个节点,同一时候设置节点值

    不要尝试在一个进程中,用一个线程开启对多个节点的监视,然后又在另外一个线程中设置被监视的节点值,然后眼巴巴地期望监视回调函数呗调用,像这样:

    static void Main(string[] args)
            {            
                IRegistryCenterClient rCenter = new RegistryCenterClient(registryCenterIP, registryCenterPort, sysFlag);
                client.Watch(sysFlag + "/config/" + "user1DBConstr", FollowUp_Config, true);
                client.Watch(sysFlag + "/config/" + "user2DBConstr", FollowUp_Config2, true);
                Console.WriteLine("设置监视完毕");
    
                Thread thread1 = new Thread(Server3);
                thread1.Start();
    
                Thread.Sleep(15000);       
            }
    
            private static void Server3()
            {
                IRegistryCenterClient rCenter = new RegistryCenterClient(registryCenterIP, registryCenterPort, sysFlag);
    
                Thread.Sleep(4000);
                rCenter.SetConfigItem("user1DBConstr", "v7");
            }

    回调函数不会被调用,无论你加不加watch中的recursive參数,也无论你的回调函数指定的是同一个还是不同的,总之不会被调用。

    现象就是卡住不动,像这样:


    然后我调试了一下,发现是set操作向etcd写值失败!!!原因是response中指示错误。

    我很操心是这个C#的etcdclient有问题,然后立马去掉thread1线程的创建,仅仅保留上面的两段监视代码,像这样:

    static void Main(string[] args)
            {            
                IRegistryCenterClient rCenter = new RegistryCenterClient(registryCenterIP, registryCenterPort, sysFlag);
                client.Watch(sysFlag + "/config/" + "user1DBConstr", FollowUp_Config, true);
                client.Watch(sysFlag + "/config/" + "user2DBConstr", FollowUp_Config2, true);
                Console.WriteLine("设置监视完毕");
    
                //Thread thread1 = new Thread(Server3);
                //thread1.Start();
    
                Thread.Sleep(15000);       
            }
    然后打开Linux终端,直接用CURL命令给这两个节点写值,发现回调函数呗调用了,然后我推断是不是不能在一个进程里面做这些事情,可能etcetra在多线程方面还是有些问题,所以我又一次创建了一个project,在这个project里面写一个程序给上面监视的两个节点写值,像这样:

    static void Main(string[] args)
            {            
                IRegistryCenterClient rCenter = new RegistryCenterClient(registryCenterIP, registryCenterPort, sysFlag);
                rCenter.SetConfigItem("user1DBConstr", "v7");
    发现监视回调函数被调用了。

    果然是同一个进程里面不能同一时候监视多个节点,然后在还有一个线程中给节点写值。

    这个问题測试的时候必需要注意!!!


    etcetera的Bug???

    (1)同一个进程以下不能同一时候监视三个及以上节点

    同一个进/线程中使用etcd 的这个C#client同一时候监视三个或者以上的节点不行,开启终端设置这几个节点的值仅仅有监视的前两个节点的回调函数会触发,第三个节点以及之后的节点的回调函数都不会触发了。

    (2)Clustering模式以下无法创建文件夹:

    我有这么一段代码:

    try
                {
                    if (source != null && source is ServiceInfo)
                    {
                        ServiceInfo serviceInfo = source as ServiceInfo;
    
                        string serviceDir = sysFlag + "/service/" + serviceInfo.serviceName;
                        client.CreateDir(serviceDir);
    
                        string key = serviceDir + "/" + serviceInfo.serviceIP;
                        string value = JsonSerializer.GetJsonByObject(serviceInfo);
                        client.Set(key, value, nodeTTL);
                        logger.Info("注冊服务成功:[key]" + key + ",[value]" + value);
                    }
                }
                catch (Exception ee)
                {
                    logger.Info("[KeepAliveTimer]注冊服务失败,原因是:" + ee.Message);
                }
    启动单个etcdserver节点跑起来没有问题,在创建文件夹的时候也都可以创建。

    可是当使用Clustering模式的时候,创建文件夹这段代码就会抛出异常,原因是Invalid JSON string。

    可是我打开Linux终端执行以下命令:

    curl -L http://127.0.0.1:4001/v2/keys/CBIP/service/HelloService -XPUT -d dir=true
    创建文件夹是没有不论什么问题的。

    我预计就是etcetera的问题,须要查看源代码确认调试一下。


    这些问题须要在深入研究其源代码进行确认!

    未完待续。。。


    By 季义钦 jiq408694711@163.com

  • 相关阅读:
    EFCore实践教程三
    EFCore实践测试二
    EFCore实践测试一
    git学习3
    git学习2
    git学习1
    ABP学习
    autofac笔记
    时间计算本质理论3-平行宇宙,对未来的子线程计算
    时间计算本质理论2-时间计算速度的不同步
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4022207.html
Copyright © 2011-2022 走看看