可以在本地机器的 Docker 容器中运行单机模式的 Pulsar,进行本地开发和测试。
如果没有安装 Docker,可以下载社区版本,并按照相应操作系统的说明进行操作。
在 Docker 中启动 Pulsar
- 在 Mac 和 Linux 上:
$ docker run -it \
-p 6650:6650 \
-p 8080:8080 \
-v $PWD/data:/pulsar/data \
apachepulsar/pulsar:2.6.1 \
bin/pulsar standalone
For Windows:
$ docker run -it \
-p 6650:6650 \
-p 8080:8080 \
-v "$PWD/data:/pulsar/data".ToLower() \
apachepulsar/pulsar:2.6.1 \
bin/pulsar standalone
A few things to note about this command:
$PWD/data
: The docker host directory in Windows operating system must be lowercase.$PWD/data
provides you with the specified directory, for example:E:/data
.-v $PWD/data:/pulsar/data
: This makes the process inside the container to store the data and metadata in the filesystem outside the container, in order not to start “fresh” every time the container is restarted.
If you start Pulsar successfully, you will see INFO
-level log messages like this:
2017-08-09 22:34:04,030 - INFO - [main:WebService@213] - Web Service started at http://127.0.0.1:8080 2017-08-09 22:34:04,038 - INFO - [main:PulsarService@335] - messaging service is ready, bootstrap service on port=8080, broker url=pulsar://127.0.0.1:6650, cluster=standalone, configs=org.apache.pulsar.broker.ServiceConfiguration@4db60246 ...
<br />> #### Tip
>
> When you start a local standalone cluster, a `public/default`
namespace is created automatically. 自动创建的命名空间将用于开发用途。 所有Pulsar的topic主题都在命名空间中进行管理。
For more information, see [Topics](concepts-messaging.md#topics).
## Use Pulsar in Docker
Pulsar offers client libraries for [Java](client-libraries-java.md), [Go](client-libraries-go.md), [Python](client-libraries-python.md)
and [C++](client-libraries-cpp.md). If you're running a local standalone cluster, you can
use one of these root URLs to interact with your cluster:
* `pulsar://localhost:6650`
* `http://localhost:8080`
The following example will guide you get started with Pulsar quickly by using the [Python](client-libraries-python.md)
client API.
Install the Pulsar Python client library directly from [PyPI](https://pypi.org/project/pulsar-client/):
```shell
$ pip install pulsar-client
### Consume 一条消息
创建 consumer 并订阅 topic:
```python
import pulsar
client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('my-topic',
subscription_name='my-sub')
while True:
msg = consumer.receive()
print("Received message: '%s'" % msg.data())
consumer.acknowledge(msg)
client.close()
Produce 一条消息
启动 producer,发送测试消息:
import pulsar
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('my-topic')
for i in range(10):
producer.send(('hello-pulsar-%d' % i).encode('utf-8'))
client.close()
获取 topic 数据
In Pulsar, you can use REST, Java, or command-line tools to control every aspect of the system. For details on APIs, refer to Admin API Overview.
在最简单的示例中,您可以使用curl来获取特定主题的统计信息:
$ curl http://localhost:8080/admin/v2/persistent/public/default/my-topic/stats | python -m json.tool
输出应如下所示:
{
"averageMsgSize": 0.0,
"msgRateIn": 0.0,
"msgRateOut": 0.0,
"msgThroughputIn": 0.0,
"msgThroughputOut": 0.0,
"publishers": [
{
"address": "/172.17.0.1:35048",
"averageMsgSize": 0.0,
"clientVersion": "1.19.0-incubating",
"connectedSince": "2017-08-09 20:59:34.621+0000",
"msgRateIn": 0.0,
"msgThroughputIn": 0.0,
"producerId": 0,
"producerName": "standalone-0-1"
}
],
"replication": {},
"storageSize": 16,
"subscriptions": {
"my-sub": {
"blockedSubscriptionOnUnackedMsgs": false,
"consumers": [
{
"address": "/172.17.0.1:35064",
"availablePermits": 996,
"blockedConsumerOnUnackedMsgs": false,
"clientVersion": "1.19.0-incubating",
"connectedSince": "2017-08-09 21:05:39.222+0000",
"consumerName": "166111",
"msgRateOut": 0.0,
"msgRateRedeliver": 0.0,
"msgThroughputOut": 0.0,
"unackedMessages": 0
}
],
"msgBacklog": 0,
"msgRateExpired": 0.0,
"msgRateOut": 0.0,
"msgRateRedeliver": 0.0,
"msgThroughputOut": 0.0,
"type": "Exclusive",
"unackedMessages": 0
}
}
}
当前内容版权归 Apache Pulsar 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Apache Pulsar .