无法使用 apache .NET 库从 C# 应用程序连接到 ZooKeeper
Posted
技术标签:
【中文标题】无法使用 apache .NET 库从 C# 应用程序连接到 ZooKeeper【英文标题】:Unable to connect to ZooKeeper from C# application using apache .NET library 【发布时间】:2012-04-18 21:27:37 【问题描述】:我正在使用 Zookeeper v3.3.3+dfsg2-1ubuntu1,在 ubuntu vm 上运行。 (VM 正在使用 NAT 网络连接运行)
在我的开发机器(Windows 7)上,如果我运行:zkCli.cmd -server 10.10.135.19:2181
,它连接良好,我可以执行create
s、get
s 等。
我有一个 C# 4 应用程序,其 NuGet 依赖于 Org.Apache.ZooKeeper v1.0.0.0。
我的使用方式如下:
class watcher : IWatcher
private readonly ManualResetEventSlim _connected = new ManualResetEventSlim(false);
private WatchedEvent _event;
public void WaitUntilConnected()
_connected.Wait();
if (_event == null) throw new ApplicationException("bad state");
if (_event.State != KeeperState.SyncConnected)
throw new ApplicationException("cannot connect");
public void Process(WatchedEvent @event)
_event = @event;
_connected.Set();
...
public void TestZooKeeper()
_countdownWatcher = new watcher();
_zk = new ZooKeeper(
Settings.Default.ZookeeperConnectionString, // 10.10.135.19:2181
new TimeSpan(Settings.Default.ZookeeperConnectionTimeout), // 10000
_countdownWatcher);
_countdownWatcher.WaitUntilConnected();
问题是这只是挂起。在 zookeeper 日志中,我看到以下内容:
2012-04-05 08:12:21,376 - INFO [NioserverCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@251] - Accepted socket connection from /10.0.2.2:51057
2012-04-05 08:12:21,379 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@777] - Client attempting to establish new session at /10.0.2.2:51057
2012-04-05 08:12:21,383 - INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1367c91bf580047 with negotiated timeout 4000 for client /10.0.2.2:51057
2012-04-05 08:12:22,500 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@251] - Accepted socket connection from /10.0.2.2:51059
2012-04-05 08:12:22,502 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@777] - Client attempting to establish new session at /10.0.2.2:51059
2012-04-05 08:12:22,505 - INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1367c91bf580048 with negotiated timeout 4000 for client /10.0.2.2:51059
2012-04-05 08:12:26,000 - INFO [SessionTracker:ZooKeeperServer@314] - Expiring session 0x1367c91bf580047, timeout of 4000ms exceeded
2012-04-05 08:12:26,001 - INFO [ProcessThread:-1:PrepRequestProcessor@387] - Processed session termination for sessionid: 0x1367c91bf580047
2012-04-05 08:12:26,004 - INFO [SyncThread:0:NIOServerCnxn@1435] - Closed socket connection for client /10.0.2.2:51057 which had sessionid 0x1367c91bf580047
2012-04-05 08:12:28,001 - INFO [SessionTracker:ZooKeeperServer@314] - Expiring session 0x1367c91bf580048, timeout of 4000ms exceeded
2012-04-05 08:12:28,002 - INFO [ProcessThread:-1:PrepRequestProcessor@387] - Processed session termination for sessionid: 0x1367c91bf580048
2012-04-05 08:12:28,004 - INFO [SyncThread:0:NIOServerCnxn@1435] - Closed socket connection for client /10.0.2.2:51059 which had sessionid 0x1367c91bf580048
这一直持续到我手动终止该进程,即WaitUntilConnected()
方法永远不会返回。 (经过调试验证)
似乎客户端连接可以很好地到达服务器,但观察者从未意识到这一点,该通道上没有进一步发生,服务器终止连接,只是让客户端重试。有什么想法我在这里做错了吗?
【问题讨论】:
曾经调用过 watcher.Process 吗? 没有。我已经逐步浏览了源代码。在内部,连接状态永远不会超过 CONNECTING 状态,因为当它尝试从 ZK 服务器读取套接字上的第一个响应时,它什么也找不到。 您可以尝试使用诸如wireshark之类的工具来查看客户端和您的代码连接之间的不同之处。 我试过了,但是有线协议是二进制的(认为它叫做黄麻) 我在这里胡乱猜测:协议版本不匹配? 【参考方案1】:事实证明,ZooKeeper 的 C# 客户端库的规范版本位于 https://github.com/ExactTargetDev/zookeeper/tree/et-develop,这与 ZooKeeper wiki 指向的不同。
通过从 github 获取源来构建源。确保获得et-develop
分支。
在顶层运行 ant
(这会调用 jute
,它会生成用于 C# 项目的 RPC 类)
打开src/dotnet
文件夹和其中的解决方案,然后
构建它
我运行了连接到本地 ZooKeeper 的单元测试,它们运行良好。
【讨论】:
以上是关于无法使用 apache .NET 库从 C# 应用程序连接到 ZooKeeper的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft.SqlServer.Types 与 .NET 标准不兼容
c# .NEt 3.5 以用户形式运行进程时无法隐藏 CMD 窗口 - 窗体应用程序
如何在 iOS 中使用 Tesseract OCR 库从图像中识别准确的文本?
通过 C# .NET 应用程序调用时无法识别命令,但直接调用时有效
Apache Active MQ .Net 客户端 (Apache NMS) 和 Visual Studio 2010 C# Express