标签:des style blog http color os 使用 io strong
Etcd是一个比较新的分布式协调框架,现在才只到0.4.6版本,还没发布1.0版本
从网上搜etcd关键字,基本上就只能看到“开源中国”的介绍:
etcd 是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。etcd 的灵感来自于 ZooKeeper 和 Doozer,侧重于:
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的客户端,因为这个目前急需。
然后用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回调函数不会被触发
而是提供了一个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回调函数
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回调函数
如果要监视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"); //会触发
标签:des style blog http color os 使用 io strong
原文地址:http://blog.csdn.net/jiyiqinlovexx/article/details/38781009