An educative example of spring cloud microservices with two services (one uses a Mongo DB), a discovery and a config server.
The repository could be expanded in the following way:
- Add a scheduler in the birthday service that will retrieve all birthday persons from the DB that celebrate their birthday today and call the notification service.
- Inside the notification service we could create an email (with a Java Email library) every time a new notification is created
- Add security to the Mongo DB and the services
- Add a Spring Cloud service gateway
- Containerize the microservices and servers, make them ready to be deployed to the cloud (pipeline, port configs etc.)
- Add a frontend
Due to lack of time, this has not been implemented yet. 😃
This is how an ideal Spring microservice architecture could look like:
Source: https://spring.io/microservices
In order to run the app, make sure you have all dependencies installed (mvn clean install) and you have a running Mongo DB on port 27017.
TIP: Use a Mongo DB docker container - it's super easy (given that you have docker already installed).
Quick Guide on using a containerized Mongo DB:
- Run
docker pull mongo:latestto pull the latest mongo DB image (be careful when using latest - it might not be the actual LATEST image - in this case we don't care) - Run
docker run -d -p 27017:27017 --name birthdaymongodb mongo:latestto start a container from the pulled image
To see the running container you may run docker container ls. Don't forget to stop the container once you're done with docker container stop birthdaymongodb.
When starting the application, follow this order:
- Start discovery server
- Start config server
- Start the notification service
- Start the birthday service
To demonstrate the load balancing of the discovery server, you can create multiple notification service instances. For IntelliJ: Run / Edit Configuration / Add new Configuration / , add a new notification service configuration with the following override parameters.
server.port=8081
service.instance.name=Notification Service Instance 1
To test the config server run: http://localhost:8888/config-client-app.properties
To change a config property manually on the fly (during runtime): Change the property in the config repo (you can make a fork from the existing repo and change the config repo url in the application.properties of the config server) and push your changes to the remote. Run a POST request to the following endpoint: http://localhost:8080/actuator/refresh. (8080 is where your config client runs).
To try the birthday service out use either Postman
or go to the Swagger UI of the API: http://localhost:8080/swagger-ui.html