NATS streaming

Detailed documentation on the NATS pubsub component

Setup NATS

You can run a NATS server locally using Docker:

  1. docker run -d -name nats-streaming -p 4222:4222 -p 8222:8222 nats-streaming

You can then interact with the server using the client port: localhost:4222.

Install NATS on Kubernetes by using the kubectl:

  1. # Single server NATS
  2. kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/master/nats-server/single-server-nats.yml
  3. kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/master/nats-streaming-server/single-server-stan.yml

This will install a single NATS-Streaming and Nats into the default namespace. To interact with NATS, find the service with: kubectl get svc stan.

For example, if installing using the example above, the NATS Streaming address would be:

<YOUR-HOST>:4222

Create a Dapr component

The next step is to create a Dapr component for NATS-Streaming.

Create the following YAML file named nats-stan.yaml:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: pubsub.natsstreaming
  8. version: v1
  9. metadata:
  10. - name: natsURL
  11. value: <REPLACE-WITH-NATS-SERVER-ADDRESS> # Required. example nats://localhost:4222
  12. - name: natsStreamingClusterID
  13. value: <REPLACE-WITH-NATS-CLUSTERID> # Required.
  14. # blow are subscription configuration.
  15. - name: subscriptionType
  16. value: <REPLACE-WITH-SUBSCRIPTION-TYPE> # Required. Allowed values: topic, queue.
  17. - name: deliverNew
  18. value: true
  19. # - name: ackWaitTime
  20. # value: "" # Optional. See: https://docs.nats.io/developing-with-nats-streaming/acks#acknowledgements
  21. # - name: maxInFlight
  22. # value: "" # Optional. See: https://docs.nats.io/developing-with-nats-streaming/acks#acknowledgements
  23. # - name: durableSubscriptionName
  24. # value: ""
  25. # following subscription options - only one can be used
  26. # - name: startAtSequence
  27. # value: 1
  28. # - name: startWithLastReceived
  29. # value: false
  30. # - name: deliverAll
  31. # value: false
  32. # - name: startAtTimeDelta
  33. # value: ""
  34. # - name: startAtTime
  35. # value: ""
  36. # - name: startAtTimeFormat
  37. # value: ""

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.

Apply the configuration

Visit this guide for instructions on configuring pub/sub components.

Related links

Last modified February 16, 2021: Merge pull request #1235 from dapr/update-v0.11 (b4e9fbb)