Etcd学习(一)安装和.NET客户端测试,etcd.net
Etcd学习(一)安装和.NET客户端测试,etcd.net
Etcd是一个比较新的分布式协调框架,现在才只到0.4.6版本,还没发布1.0版本
从网上搜etcd关键字,基本上就只能看到“开源中国”的介绍:
etcd 是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。etcd 的灵感来自于 ZooKeeper 和 Doozer,侧重于:
- 简单:支持 curl 方式的用户 API (HTTP+JSON)
- 安全:可选 SSL 客户端证书认证
- 快速:单实例可达每秒 1000 次写操作
- 可靠:使用 Raft 实现分布式
Etcd is written in Go and uses the raft consensus algorithm to manage a highly-available replicated log.
一、安装和测试
安装非常简单,大概经历以下几个步骤吧:1、下载VMWare9.0虚拟机和一个Ubuntu12 desktop版的ISO,安装好Ubuntu,以及root用户,vim软件等的初始化设置。(必须安装Linux-64bit,否则运行时etcd会出错)
2、下载Go语言编译器,用来编译Etcd的,下载地址是:http://tip.golang.so/dl/,上面有解压说明教程。
3、下载CURL,用于在Linux终端发送HTTP请求到Etcd服务器,网上有它的编译和安装教程,比如这个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
二、.NET客户端代码测试
我准备先开发.NET的客户端,因为这个目前急需。
下载 drusellers/etcetera
然后用VS202打开自行编译成DLL,将DLL加入到自己的新的工程中。
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#的etcd客户端有问题,然后立刻去掉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在多线程方面还是有些问题,所以我重新创建了一个工程,在这个工程里面写一个程序给上面监视的两个节点写值,像这样:
static void Main(string[] args) { IRegistryCenterClient rCenter = new RegistryCenterClient(registryCenterIP, registryCenterPort, sysFlag); rCenter.SetConfigItem("user1DBConstr", "v7");发现监视回调函数被调用了。
果然是同一个进程里面不能同时监视多个节点,然后在另一个线程中给节点写值。
这个问题测试的时候必须要注意!!!
By 季义钦 jiq408694711@163.com
你要下载.net 2.0 或者.net 3.5 而不是win7装过的
之前安装到一般停止过?不会吧。截图显示的系统是Vista或WIndows7,都自带了Framework,至少2.0版本,Windows7自带的是3.5版(包含了2.0),所以都不需要另外安装。
要添加或修复,去控制面板的打开或关闭Windows功能里面找。
评论暂时关闭