Skip to content

Instantly share code, notes, and snippets.

@unixfox
Last active January 8, 2023 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixfox/c51910b2b8cb8905f9d10431e0486a64 to your computer and use it in GitHub Desktop.
Save unixfox/c51910b2b8cb8905f9d10431e0486a64 to your computer and use it in GitHub Desktop.
nats jetstream cluster

NATS JetStream Playground

Playground for a Secure, Highly Available NATS Cluster with message persistence (using JetStream).

This repo contains a cluster with 3 nodes and a Go client sending/receiving messages to it.

Pre-requisites

To use this repo, please install:

  • Docker
  • mkcert (zero-config tool for locally-trusted development certificates)

Installation

Authentication for client-server and server-server (for the cluster) use X509 certificatesn. To install them locally:

make certificates

Run it

Spin up the cluster with:

docker-compose up

Run the client with:

cd client
go run client.go

You should see the following appear in your terminal:

connecting securely to cluster
getting JetStream context
stream not found
creating stream "ORDERS" and subject "ORDERS.received"
publishing an order
attempting to receive order
got order: "one big burger"

You can also use nats-io/nats-tools/nats to issue manual commands to the cluster. If you do so, you may need to change the client publish permissions in config/jetstream.conf.

Clean up

Once you are done testing, remove the CA from your local system trust store:

make cleanup
version: '3'
services:
n1:
container_name: n1
image: nats:2.9.11-alpine
#entrypoint: /nats-server
command: "-js --config /config/jetstream.conf --server_name S1"
networks:
- nats
ports:
- 4222:4222
volumes:
- ./config/server1:/config
- ./persistent-data/server-n1/:/data/nats-server/jetstream
n2:
container_name: n2
image: nats:2.9.11-alpine
#entrypoint: /nats-server
command: "-js --config /config/jetstream.conf --server_name S2"
networks:
- nats
ports:
- 4223:4222
volumes:
- ./config/server2:/config
- ./persistent-data/server-n2/:/data/nats-server/jetstream
n3:
container_name: n3
image: nats:2.9.11-alpine
#entrypoint: /nats-server
command: "-js --config /config/jetstream.conf --server_name S3"
networks:
- nats
ports:
- 4224:4222
volumes:
- ./config/server3:/config
- ./persistent-data/server-n3/:/data/nats-server/jetstream
networks:
nats: {}
debug: true
trace: true
# Each server can connect to clients on the internal port 4222
# (mapped to external ports in our docker-compose)
port: 4222
# Persistent JetStream data store
jetstream = {
# Each server persists messages within the docker container
# at /data/nats-server (mounted as ./persistent-data/server-n…
# in our docker-compose)
store_dir: "/data/nats-server/"
}
# Cluster formation
cluster = {
name: "JSC"
listen: "0.0.0.0:4245"
# Servers can connect to one another at
# the following routes
routes = [
"nats-route://n2:4245"
"nats-route://n3:4245"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment