The Microservices Architecture contains
- Broker Service
- Authentication Service
- Logger Service
- Mail Service
- Listener Service AMQP with RabbitMQ
-
www.gnu.org/software/make is a tool which controls the generation of executables and other non-source files of a program from the program's source files
-
Make gets its knowledge of how to build your program from a file called the
makefile
, which lists each of the non-source files and how to compute it from other files. When you write a program, you should write a makefile for it, so that it is possible to use Make to build and install the program -
All the
Docker
images control forLinux/MacOS
are in theproject/Makefile
fileWindows
are in theproject/Makefile.windows
file
- The Test Page is used for test the Services in this Architecture
- The Test Page will send an example request to the specific Service that user choose and then receive the response from that Service
- Technologies
- bootstrap
- javaScript for fetching data from the services
- html
- Broker Service is an entry point for redirecting the request to appropriate service and receiving the response from that service
- Package index
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
Go
HTTP services- Installation
go get github.com/go-chi/chi/v5 go get github.com/go-chi/chi/middleware go get github.com/go-chi/cors
- Installation
- pkg.go.dev/net/rpc provides access to the exported methods of an object across a network or other I/O connection. A server registers an object, making it visible as a service with the name of the type of the object. After registration, exported methods of the object will be accessible remotely. A server may register multiple objects (services) of different types but it is an error to register multiple objects of the same type
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
- File
broker-service/broker-service.dockerfile
is thedockerfile
for the service
- Authentication Service is an API for determining if the credentials from the request body is matching the data in the database. All the credentials are stored in a PostgreSQL image
- Package index
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
Go
HTTP services- Installation
go get github.com/go-chi/chi/v5 go get github.com/go-chi/chi/middleware go get github.com/go-chi/cors
- Installation
- github.com/jackc/pgconn is a low-level
PostgreSQL
database driver. It operates at nearly the same level as theC
librarylibpq
- Installation
go get github.com/jackc/pgconn
- Installation
- github.com/jackc/pgx is a higher level libraries, high performance interface that exposes PostgreSQL-specific features such as
LISTEN
/NOTIFY
andCOPY
. It also includes an adapter for the standarddatabase/sql
interface. The toolkit component is a related set of packages that implementPostgreSQL
functionality such as parsing the wire protocol and type mapping betweenPostgreSQL
andGo
- Installation
go get github.com/jackc/pgx/v4 go get github.com/jackc/pgx/v4/stdlib
- Installation
- github.com/golang/crypto/bcrypt is used for hashing passwords
- pkg.go.dev/database/sql provides a generic interface around SQL (or SQL-like) databases. The sql package must be used in conjunction with a database driver. See golang.org/s/sqldrivers for a list of drivers
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
- File
authentication-service/authentication-service.dockerfile
is thedockerfile
for the service
- Logger Service is an API for saving logs whenever one Service receives and processes a response. All the
LogEntry
struct contains 2 fields Name and Data. The collections are stored in a MongoDB image - Package index
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
Go
HTTP services- Installation
go get github.com/go-chi/chi/v5 go get github.com/go-chi/chi/middleware go get github.com/go-chi/cors
- Installation
- github.com/mongodb/mongo-go-driver is the
MongoDB
supported driver forGo
- Installation
go get go.mongodb.org/mongo-driver/mongo go get go.mongodb.org/mongo-driver/mongo/options
- Installation
- pkg.go.dev/net/rpc provides access to the exported methods of an object across a network or other I/O connection. A server registers an object, making it visible as a service with the name of the type of the object. After registration, exported methods of the object will be accessible remotely. A server may register multiple objects (services) of different types but it is an error to register multiple objects of the same type.
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
- File
logger-service/logger-service.dockerfile
is thedockerfile
for the service
- Mail Service connects directly with Broker Service in the development version which you shouldn't do in production. In production, Mail Service can not be connected by User, just be connected to other Service except Broker Service. The Service sends tested mails to the MailHog server
- Package index
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
Go
HTTP services- Installation
go get github.com/go-chi/chi/v5 go get github.com/go-chi/chi/middleware go get github.com/go-chi/cors
- Installation
- github.com/mailhog/MailHog is an email testing tool for developers
- Overview
- Configure your application to use
MailHog
for SMTP delivery - View messages in the web UI, or retrieve them with the JSON API
- Optionally release messages to real SMTP servers for delivery
- Configure your application to use
- Installation
go get github.com/mailhog/MailHog
- The local server of
MailHog
runs onhttp://localhost:1025
, check this server for web UI and see all the tested mails in theInbox
section - A
Docker
image forMailHog
is configured in fileproject/docker-compose.yml
- Overview
- github.com/vanng822/go-premailer is an inline styling for HTML mail in
Go
- Styling mail with both
HTML
andplain
formats before sending - Installation
go get github.com/vanng822/go-premailer/premailer
- Styling mail with both
- github.com/xhit/go-simple-mail is the best way to send emails in
Go
with SMTP Keep Alive and Timeout for Connect and Send- You can find more information in the documentation
- Installation
go get github.com/xhit/go-simple-mail/v2
- github.com/go-chi/chi is a lightweight, idiomatic and composable router for building
- File
mail-service/mail-service.dockerfile
is thedockerfile
for the service
- Listener Service AMQP listens for any data (requests) pushed to RabbitMQ server to consume it as soon as possible
- Any requests after being routed by Broker Service is pushed directly to RabbitMQ server. The Listener Service AMQP connects to the RabbitMQ server and listens to any requests in the message queue and then sends requests to the appropriate Services
- Package index
- github.com/rabbitmq/amqp091-go is a
Go AMQP 0.9.1
client maintained by the RabbitMQ core team. The package provides a functional interface that closely represents theAMQP 0.9.1
model targeted toRabbitMQ
as a server- Installation
go get github.com/rabbitmq/amqp091-go
- Installation
- github.com/rabbitmq/amqp091-go is a
- File
listener-service/listener-service.dockerfile
is thedockerfile
for the service
- The first option for Broker Service to communicate with Logger Service
logEventViaRabbit(w http.ResponseWriter, l LogPayload)
just pushes requests from the client to theRabbitMQ
Server for Listener Service to consume
- Image for the
RabbitMQ
server israbbitmq:3.9-alpine
, runs on port5672
ondocker
server - Uses the
topics
as exchange type - Does not delete data until it is consumed successfully
- References
- Find more information about
RabbitMQ
at RabbitMQ website - Godoc API reference
- RabbitMQ tutorials in Go
- Find more information about
- The second option for Broker Service to communicate with Logger Service
logItemViaRPC(w http.ResponseWriter, l LogPayload)
connects to theRPC
Server of Logger Service on port5001
and calls the function of theRPC
Server using pkg.go.dev/net/rpc package- When it comes to the
RPC
option, the Logger Service has to always listen to theRPC
requests - Broker Service and Logger Service use pkg.go.dev/net/rpc package to communicate. The Broker Service is the client and the Logger Service is the server
- When it comes to the
- The third option for Broker Service to communicate with Logger Service
LogViaGRPC(w http.ResponseWriter, r *http.Request)
creates a client and connects the client to thegRPC
server that is located in thelogger-service/cmd/api/grpc.go
file. the gRPC server runs on port50001
- Information about packages and how to work and generate codes with
Protocol Buffers
andgRPC
-
All the
Docker
configurations are in theproject/docker-compose.yml
file -
Build all the
Docker
images and services's binariesmake up-build
-
Build one
Docker
image of one specific servicemake service-build
-
Pull and Start all the
Docker
imagesmake up
-
Stop docker compose
make down
-
Start the
frontend
binary listening onhttp://localhost:80
make start
-
Stop the
frontend
binary listening onhttp://localhost:80
make stop
-
Docker Swarm
- Build images for microservice
docker build -f servicename.dockerfile -t your_docker_hub_username/servicename:1.0.0 .
- Push images to
docker hub
docker push your_docker_hub_username/servicename:1.0.0
- Configurations of
Docker Swarm
are inproject/swarm.yml
file - Go to the
project
folder - Initiate
Swarm
(set the manager node is theproject
folder)docker swarm init
- In order to set the current folder as the
manager/worker
nodedocker swarm join-token manager/worker
- Create all the services following the configurations of
Docker Swarm
docker stack deploy -c swarm.yml go-microservices
- List the running services
docker service ls
- Scaling services
docker service scale go-microservices_servicename=number_of_tasks
- If one service suddenly dies,
Docker Swarm
will create another instance and bring it back up
- Build images for microservice
-
Updating Services
- Build a new tagged version of the service
docker build -f servicename.dockerfile -t your_docker_hub_username/servicename:1.0.1 .
- Push images again to
docker hub
docker push your_docker_hub_username/servicename:1.0.1
- Update the version of the service currently running in
Swarm
- Any time updating a service to a new version, at least 2 tasks of that service have to be running in order to achieve no downtime
docker service scale go-microservices_servicename=2
- Update one image to a new version
docker service update --image your_docker_hub_username/servicename:1.0.1 go-microservices_servicename
- Downgrade
docker service update --image your_docker_hub_username/servicename:1.0.0 go-microservices_servicename
- Any time updating a service to a new version, at least 2 tasks of that service have to be running in order to achieve no downtime
- Build a new tagged version of the service
-
Stopping Docker Swarm
docker service scale go-microservices_servicename=0
-
Removing Docker Swarm
docker stack rm go-microservices
-
Leaving Docker Swarm (use
--force
on a node that is participating as manager)docker swarm leave
-
Caddy
- Added to the mix as a Proxy to the Front end and the Broker
- Configurations in the
project/Caddyfile
file - Build the
caddy
docker imagedocker build -f caddy.dockerfile -t your_docker_hub_username/micro-caddy:1.0.0 . docker push your_docker_hub_username/micro-caddy:1.0.0
-
Bringing up Swarm
docker stack deploy -c swarm.yml go-microservices
- Minh Tran (Me)