Skip to Content
ZooKeeper
book

ZooKeeper

by Flavio Junqueira, Benjamin Reed
May 2025
Intermediate to advanced
246 pages
3h 4m
Chinese
O'Reilly Media, Inc.
Content preview from ZooKeeper

第 7 章 C 客户端 C 客户端

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

尽管 ZooKeeper 的 Java 接口是最主要的接口,但 C ZooKeeper 客户端绑定也深受 ZooKeeper 开发人员的欢迎,并构成了其他语言绑定的基础。本章将重点介绍这种绑定。为了说明如何使用 C API 开发 ZooKeeper 应用程序,我们将用 C 语言重新实现我们的 master-worker 示例中的 master。

C API 的主要参考资料是ZooKeeper发行版中的zookeeper.h文件,而构建客户端库的说明则在项目发行版的README文件中给出。另外,你也可以使用ant compile-native,它能自动完成所有工作。在介绍代码片段之前,我们先简要介绍一下如何设置开发环境,以帮助你入门。

当我们构建 C 客户端时,会生成两个库:一个用于多线程客户端,另一个用于单线程客户端。本章大部分内容都假定使用的是多线程库;我们将在本章末尾讨论单线程版本,但我们鼓励读者专注于多线程实现。

设置开发环境

在ZooKeeper发行版中,我们可以发送预编译好的JAR文件,这些文件可以在任何平台上运行。要使用C语言进行本地编译,我们需要先构建所需的共享库,然后才能构建本地C语言的ZooKeeper应用程序。幸运的是,ZooKeeper提供了构建这些库的简便方法。

编译 ZooKeeper 原生库最简单的方法是使用ant 编译工具。 在解压 ZooKeeper 发行版的目录下,有一个名为build.xml 的文件,该文件包含ant编译所有内容所需的说明。 此外,你还需要automakeautoconfcppunit。 如果你使用 Linux,这些软件包应该可以在你的主机发行版中找到。 在 Windows 上,Cygwin 提供了这些软件包。 在 Mac OS X 上,你可以使用 Fink、Brew 或 MacPorts 等开源软件包管理器。

安装好所有需要的应用程序后,就可以使用 ZooKeeper 来构建库了:

ant compile-native

编译完成后,你会在build/c/build/usr/lib中找到需要链接的库,并在build/c/build/usr/include/zookeeper 中找到需要的包含文件。

开始会议

要使用 ZooKeeper 做任何事情,我们首先需要一个zhandle_t 句柄。 要获取一个句柄,我们需要调用zookeeper_init ,其签名如下:

ZOOAPI zhandle_t *zookeeper_init(const char *host, 1
                                 watcher_fn fn, 2
                                 int recv_timeout, 3
                                 const
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

ZooKeeper

ZooKeeper

Benjamin Reed, Flavio Junqueira
R在数据科学中的应用,第2版

R在数据科学中的应用,第2版

Hadley Wickham, Mine Cetinkaya-Rundel, Garrett Grolemund

Publisher Resources

ISBN: 9798341659599