This is just a simple implementation of NATS using Go Programming Language. Big thanks to official NATS documentation and Synadia.
In this repository, all are set. The key points here are:
- NATS server implemented as a Supercluster that consist of AP, NA, and EU. Each cluster is represented a geography and each cluster has 3 nodes. (we don't really need NA cluster in this simulation, but the best practice for redudancy are 3 clusters)
- This implementation use creds for the authentication. You can change it like using username and password (not recommended), token or a nkeys. Securing NATS
- To generate the creds and also the resolver, i use nsc tool. you can check it here: NSC Tool Documentation
- Current Implementation are using JetStream and Consumer. You can also explore more about Pub-Sub, Request and Reply, Queque Groups and other JetStream feature.
- The monitoring uses several components addition, which are prometheus-nats-exporter, Prometheus, and Grafana. We use monitoring dashboard template for Grafana that also provided by prometheus-nats-exporter repository.
Even though the repo includes the creds and resolver configuration, you still need to add/generate all of the account credentials to your local nsc and then push it to the nats server. Don't worry. The repo has a script to help you. This repo provides a preparation script. run this from your root of project.
(I'm sorry that this is just available with unix and unix-like base. So for Windows user, i suggest you to use WSL with docker support instead)
before run the script. clone this repo first to your host by using below command
git clone https://github.com/Daffadon/nats-example --depth 1After clone go to your project and execute below command
chmod +x ./deployment/bin/prepare.sh
./deployment/bin/prepare.shp.s. if it's not working, make an issue and i'll guide you or you can check my github profile, go to my website, you'll found my email and send an email to me.
The script will install NSC and generate operator, account, user, and configuration that we need to authenticate and authorize the client to use nats and jetstream (publisher and consumer). Also it's install the docker and docker-compose if your system doesn't have that.
After preparation, you can run start script from the root of your project.
chmod +x ./deployment/bin/start.sh
./deployment/bin/start.shStart script will up the Super Cluster, Nats Monitoring with Grafana and Prometheus, and also PostgreSQL that will be used by the consumer in this example. The url string to make a connection to the db has defined in the .env file.
For every nats server in the docker.compose.nats.yml, i've mount the config for each cluster in the service definition. I also open the port 8222 for monitoring purpose and open 2 port in 2 different cluster that we will use for the client connection in this example. The last step is push the configuration from the preparation script to the one of the nats server. In this case, we will push it to the ap_1. All of the cluster that connected to the ap_1 would know about the configuration automatically.
Before we jump the example, let's set up the monitoring dashboard. I've provide the grafana dashboard that i got from prometheus-nats-exporter example and prometheus config for scraping from what nats-prometheus-exporter exported.
- Open http://localhost:3000 to open Grafana, Default username/password is admin/admin
- Connection > datasource > prometheus > fill "http://prometheus:9090" without quote in connection input > save & test
- Go to Dashboard > New > import. Upload one of json files from deployment/grafana directory and choose previous datasource that has been built. You'll see something like this. do the same step for another json file.
Note: this is using golang version 1.24.2 as you can see in the go.mod.
This repository has example how to use jetstream publisher and consumer. You can see from the architecture, the publisher is send data to a subject from EU_1 and client get the data via consumer in AP_1. Consumer here is not a client, consumer is a component that like it's name has a goal to consume and live in the nats server. the client just call the consumer, identified by a name, to retrieve the data.
Open your terminal and run below command from the root of project to run the publisher.
cd publish && go run main.goIn your other terminal, run below command from the root of project to run the consumer/subscriber.
cd consumer && go run main.goto stop all of the services, run below command from the root of the project:
chmod +x ./deployment/bin/stop.sh
./deployment/bin/stop.sh


