Example app composed by a rabbitmq message broker instance and two golang apps: a message publisher and a message consumer. The example is meant to show how the publisher application can create its own exchange (a notification channel) and publish its messages on this exchange. At the same time there can be more than 1 consumer applications: each consumer creates its own queue subscribing it to the exchange. The queues receive a copy of each one of the messages in the subscribed exchange so this system can perfectly work as a Pub/Sub system.
In the example is explained how to start the apps using either Minikube (k8s) and Golang and Docker.
-
Start minikube with:
minikube start
-
Clone the Github project
-
enter the folder
cd rabbit-go
You can skip this step if the images are already present to the public repository of Dockerhub according to the name specified in the yaml files.
# docker login
docker login -u=<username-here> -p=<password-here>
# create publisher image and push it on DockerHub
cd publisher
docker build -f Dockerfile -t alessandroargentieri/rabbit-go-publisher .
docker push alessandroargentieri/rabbit-go-publisher
cd ..
# create consumer image and push it on DockerHub
cd consumer
docker build -f Dockerfile -t alessandroargentieri/rabbit-go-consumer .
docker push alessandroargentieri/rabbit-go-consumer
cd ..
# create secret
kubectl create secret generic rabbit-credentials --from-literal=password=password --from-literal=user=myuser
# get yaml of the created secret
kubectl get secret rabbit-credentials -o yaml
kubectl apply -f k8s/yamls/rabbit.yaml
kubectl apply -f k8s/yamls/publisher.yaml
kubectl apply -f k8s/yamls/consumer.yaml
# copy the addresses for the publisher nodeport and the consumer nodeport
minikube service list
curl http://<publisher-copied-ip>:32000/publisher | jq
curl http://<consumer-copied-ip>:32100/consumer | jq
-
Go to Katacoda Ubuntu environment:
-
Clone the Github project
-
enter the folder
cd rabbit-go
-
start everything with the script
./init.sh
curl http://localhost:8080/publisher | jq
curl http://localhost:8081/consumer | jq
curl http://localhost:8082/consumer | jq
curl http://localhost:8083/consumer | jq
# open the producer log file
tail -f publisher-logs.txt
# Ctrl+C
# open the consumer-1 log file
tail -f consumer-1-logs.txt
# Ctrl+C
# open the consumer-2 log file
tail -f consumer-1-logs.txt
# Ctrl+C
# open the consumer-3 log file
tail -f consumer-1-logs.txt
# Ctrl+C