Supported platforms
Pulsar C ++客户端已在MacOS和Linux上成功测试。
Linux
安装
Since the 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can choose to download and install those packages instead of building them yourself.
RPM
链接 | 加密文件 |
---|---|
client | asc, sha512 |
client-debuginfo | asc, sha512 |
client-devel | asc, sha512 |
安装RPM包的命令如下所示:
$ rpm -ivh apache-pulsar-client*.rpm
DEB
链接 | 加密文件 |
---|---|
client | asc, sha512 |
client-devel | asc, sha512 |
你可以使用如下命令来下载并安装DEB包:
$ apt install ./apache-pulsar-client*.deb
编译
If you want to build RPM and Debian packages off latest master, you can follow the instructions below to do so. All the instructions are run at the root directory of your cloned Pulsar repo.
你可以通过下面的方式来编译包含libpulsar.so
/ libpulsar.a
静态库以及所有必要依赖的二进制包。
请先编译Java库,然后再编译C++库。
mvn install -DskipTests
RPM
pulsar-client-cpp/pkg/rpm/docker-build-rpm.sh
上面的命令将会在Docker中编译RPM包并且在pulsar-client-cpp/pkg/rpm/RPMS/x86_64/
.生成你需要的那些RPM包。
包名 | 内容 |
---|---|
pulsar-client | libpulsar.so 共享库 |
pulsar-client-devel | 静态库 libpulsar.a 以及 C++ 和 C 头文件 |
pulsar-client-debuginfo | Debug symbols for libpulsar.so |
Deb
如果需要编译Debian包:
pulsar-client-cpp/pkg/deb/docker-build-deb.sh
编译好的DEB包会被保存在文件夹 pulsar-client-cpp/pkg/deb/BUILD/DEB/
中
包名 | 内容 |
---|---|
pulsar-client | libpulsar.so 共享库 |
pulsar-client-dev | 静态库 libpulsar.a 以及 C++ 和 C 头文件 |
MacOS
Pulsar releases are available through the Homebrew core repository. You can install the C++ client library with:
brew install libpulsar
This will install the package with the library and headers.
连接URL
To connect to Pulsar using client libraries, you need to specify a Pulsar protocol URL.
Pulsar protocol URLs are assigned to specific clusters, use the pulsar URI scheme and have a default port of 6650. Here’s an example for localhost:
pulsar://localhost:6650
A URL for a production Pulsar cluster may look something like this:
pulsar://pulsar.us-west.example.com:6650
If you’re using TLS authentication, the URL will look like something like this:
pulsar+ssl://pulsar.us-west.example.com:6651
Consumer
Client client("pulsar://localhost:6650");
Consumer consumer;
Result result = client.subscribe("my-topic", "my-subscribtion-name", consumer);
if (result != ResultOk) {
LOG_ERROR("Failed to subscribe: " << result);
return -1;
}
Message msg;
while (true) {
consumer.receive(msg);
LOG_INFO("Received: " << msg
<< " with payload '" << msg.getDataAsString() << "'");
consumer.acknowledge(msg);
}
client.close();
Producer
Client client("pulsar://localhost:6650");
Producer producer;
Result result = client.createProducer("my-topic", producer);
if (result != ResultOk) {
LOG_ERROR("Error creating producer: " << result);
return -1;
}
// Publish 10 messages to the topic
for (int i = 0; i < 10; i++){
Message msg = MessageBuilder().setContent("my-message").build();
Result res = producer.send(msg);
LOG_INFO("Message sent: " << res);
}
client.close();
Authentication
ClientConfiguration config = ClientConfiguration();
config.setUseTls(true);
config.setTlsTrustCertsFilePath("/path/to/cacert.pem");
config.setTlsAllowInsecureConnection(false);
config.setAuth(pulsar::AuthTls::create(
"/path/to/client-cert.pem", "/path/to/client-key.pem"););
Client client("pulsar+ssl://my-broker.com:6651", config);