- 下载ZooKeeper,我下载的是3.4.6,解压
- vs2008或者以上
- 在windows 环境变量中,增加ZOOKEEPER_HOME定义,指向解压目录
- 手动修改project文件,$(ZOOKEEPER_HOME)\src\c\zookeeper.vcproj,用记事本(或者ultraedit等)打开,由于该文件是用utf-8编码的,在windows下默认带BOM头,直接删除前面2个字节(即BOM头)。
- 加载zookeeper.sln文件
- $(ZOOKEEPER_HOME)/bin/zkServer.cmd,启动ZooKeeper服务器(单机standalone)
- 编译并运行ZooKeeper.sln中的cli工程,启动ZooKeeper的client
第4步,如果直接打开sln文件,会发现不能正常加载ZooKeeper工程。因为,我们windows的默认编码都是GBK,不能识别ZooKeeper.vcproj文件的BOM头。
目前,ZooKeeper自带的c版client,非常底层,易读性差和使用繁杂。
另外,JAVA版建议使用Curator,apache的顶级开源项目http://curator.apache.org/,Leader Select(领导选举)
如果在X64上编译zookeeper client,会有如下错误,使用了非标准扩展: 不支持在此结构上使用“_asm”关键字,需要修改程序
- fetch_and_add 这是一个原子操作级别函数,原型为int32_t fetch_and_add(volatile int32_t* operand, int incr),即 等价于:
result=operand; operand =operand + incr; return result;
里面包含了汇编指令。
这个函数修改为如下,并在工程编译宏上加入WIN64,这样可以保证win32下仍然与源码一致,而win64下不同:
int32_t fetch_and_add(volatile int32_t* operand, int incr) { #ifndef WIN32 int32_t result; asm __volatile__( "lock xaddl %0,%1\n" : "=r"(result), "=m"(*(int *)operand) : "0"(incr) : "memory"); return result; #else volatile int32_t result=0; //wx 修改WIN32下函数 #if !defined WIN64 _asm { mov eax, operand; //eax = v; mov ebx, incr; // ebx = i; mov ecx, 0x0; // ecx = 0; lock xadd dword ptr [eax], ecx; lock xadd dword ptr [eax], ebx; mov result, ecx; // result = ebx; } #else result = *operand; InterlockedAdd((volatile LONG*)operand,incr); #endif return result; #endif }
或者直接用InterlockedAdd函数替换,直接修改为:
int32_t fetch_and_add(volatile int32_t* operand, int incr) { #ifndef WIN32 int32_t result; asm __volatile__( "lock xaddl %0,%1\n" : "=r"(result), "=m"(*(int *)operand) : "0"(incr) : "memory"); return result; #else volatile int32_t result= *operand; InterlockedAdd((volatile LONG*)operand,incr); return result; #endif }