Set up a standalone Pulsar in Docker

For local development and testing, you can run Pulsar in standalonemode on your own machine within a Docker container.

If you have not installed Docker, download the Community editionand follow the instructions for your OS.

Start Pulsar in Docker

  • For MacOS and Linux:
  1. $ docker run -it \
  2. -p 6650:6650 \
  3. -p 8080:8080 \
  4. -v $PWD/data:/pulsar/data \
  5. apachepulsar/pulsar:2.4.0 \
  6. bin/pulsar standalone
  • For Windows:
  1. $ docker run -it \
  2. -p 6650:6650 \
  3. -p 8080:8080 \
  4. -v "$PWD/data:/pulsar/data".ToLower() \
  5. apachepulsar/pulsar:2.4.0 \
  6. 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 thedata 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:
  1. 2017-08-09 22:34:04,030 - INFO - [main:WebService@213] - Web Service started at http://127.0.0.1:8080
  2. 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
  3. ...

Tip

When you start a local standalone cluster, a public/defaultnamespace is created automatically. The namespace is used for development purposes. All Pulsar topics are managed within namespaces.For more information, see Topics.

Use Pulsar in Docker

Pulsar offers client libraries for Java, Go, Pythonand C++. If you're running a local standalone cluster, you canuse one of these root URLs to interact with your cluster:

  • pulsar://localhost:6650
  • http://localhost:8080The following example will guide you get started with Pulsar quickly by using the Pythonclient API.

Install the Pulsar Python client library directly from PyPI:

  1. $ pip install pulsar-client

Consume a message

Create a consumer and subscribe to the topic:

  1. import pulsar
  2. client = pulsar.Client('pulsar://localhost:6650')
  3. consumer = client.subscribe('my-topic',
  4. subscription_name='my-sub')
  5. while True:
  6. msg = consumer.receive()
  7. print("Received message: '%s'" % msg.data())
  8. consumer.acknowledge(msg)
  9. client.close()

Produce a message

Now start a producer to send some test messages:

  1. import pulsar
  2. client = pulsar.Client('pulsar://localhost:6650')
  3. producer = client.create_producer('my-topic')
  4. for i in range(10):
  5. producer.send(('hello-pulsar-%d' % i).encode('utf-8'))
  6. client.close()

Get the topic statistics

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.

In the simplest example, you can use curl to probe the stats for a particular topic:

  1. $ curl http://localhost:8080/admin/v2/persistent/public/default/my-topic/stats | python -m json.tool

The output is something like this:

  1. {
  2. "averageMsgSize": 0.0,
  3. "msgRateIn": 0.0,
  4. "msgRateOut": 0.0,
  5. "msgThroughputIn": 0.0,
  6. "msgThroughputOut": 0.0,
  7. "publishers": [
  8. {
  9. "address": "/172.17.0.1:35048",
  10. "averageMsgSize": 0.0,
  11. "clientVersion": "1.19.0-incubating",
  12. "connectedSince": "2017-08-09 20:59:34.621+0000",
  13. "msgRateIn": 0.0,
  14. "msgThroughputIn": 0.0,
  15. "producerId": 0,
  16. "producerName": "standalone-0-1"
  17. }
  18. ],
  19. "replication": {},
  20. "storageSize": 16,
  21. "subscriptions": {
  22. "my-sub": {
  23. "blockedSubscriptionOnUnackedMsgs": false,
  24. "consumers": [
  25. {
  26. "address": "/172.17.0.1:35064",
  27. "availablePermits": 996,
  28. "blockedConsumerOnUnackedMsgs": false,
  29. "clientVersion": "1.19.0-incubating",
  30. "connectedSince": "2017-08-09 21:05:39.222+0000",
  31. "consumerName": "166111",
  32. "msgRateOut": 0.0,
  33. "msgRateRedeliver": 0.0,
  34. "msgThroughputOut": 0.0,
  35. "unackedMessages": 0
  36. }
  37. ],
  38. "msgBacklog": 0,
  39. "msgRateExpired": 0.0,
  40. "msgRateOut": 0.0,
  41. "msgRateRedeliver": 0.0,
  42. "msgThroughputOut": 0.0,
  43. "type": "Exclusive",
  44. "unackedMessages": 0
  45. }
  46. }
  47. }