Building multiple microservices using Spring Boot, and deploying and running them using docker compose.
The project follows the microservices architectural style. We have three independent microservices (User, Site and Organization), which connect to MySql database and communicate with each other using RestTemplate. We use Docker Compose for deploying and running them on docker containers.
This project is created using the following technologies:
-
Java 8
-
Maven Dependency Management
-
Spring Boot in microservices development:
- Spring Web
- Spring Data JPA
- Spring Devtools
- Spring Actuator
-
MySql database
-
Docker
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
You need to install the following software:
- Java JDK 1.8+
- Maven 3.0+
- Git client
- Docker Compose: To install docker-compose
The steps to be taken in order to create working microservices-based system using Spring boot and Docker:
Step 1. Building the microservices using Spring Boot and communication between them by RestTemplate:
Step 2. Creating the Dockerfile for each service. Docker file is a list of commands that we want the docker engine to execute.
Go to the directory for each spring boot project and create a Dockerfile:
- User microservice Dockerfile
- Site microservice Dockerfile
- Organization microservice Dockerfile
For example Dockerfile for User service:
From openjdk:8
copy ./target/users-service-0.0.1-SNAPSHOT.jar user-service.jar
CMD ["java","-jar","user-service.jar"]Step 3. Creating docker-compose.yml as follows:
version: "3"
services:
organization:
build: ./organization-service
ports:
- "8083:8083"
networks:
- organization-mysql
- organization-user
- organization-site
depends_on:
- mysqldb
- user
- site
site:
build: ./site-service
ports:
- "8082:8082"
networks:
- organization-mysql
- site-user
- organization-site
depends_on:
- mysqldb
- user
user:
build: ./user-service
ports:
- "8081:8081"
networks:
- site-user
- organization-user
- organization-mysql
depends_on:
- mysqldb
mysqldb:
image: mysql:8
networks:
- organization-mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=organizationdb
networks:
organization-mysql:
site-user:
organization-user:
organization-site:Step 4. Running and testing applications:
Run docker-compose up and Compose starts and runs your entire services.
To run this project, install it locally as follow:
-
Clone the application
git clone https://github.com/SayedBaladoh/Deploying-and-Running-Multiple-Spring-Boot-Microservices-with-MySql-using-Docker-Compose.git
-
Create a JAR file for each service
Run maven command - clean install, and a jar file gets created in the target directory for each service like so:
cd service_directory maven clean install- service_directory: the directory of the service (user-service, site-service and organization-service).
-
Start the User, Site, Organization and MySql using docker-compose
The project includes a docker-compose.yml file so you can Run docker-compose up to start entire services, no installation needed.
cd solution_directory docker-compose up -dYou can see -
- Building an image from Dockerfile for each service and image for MySql database If it does not exist.
- Building containers (user, site, organization, and mysql) using the images.
- Starting the services (user, site, organization, and MySql).
-
Check the created images
Use the following command to check the created images:
docker images
-
Check the created containers
Use the following command to check the created containers:
docker ps -a
-
Check the logs
Use the following command to check the logs for each container:
docker container logs CONTAINER_ID
To access the services use the following endpoints
Run the User/Site/Organization microservices
The user microservice will start on port 8081, So you'll be able to visit the user microservice under address http://localhost:8081.
The site microservice will start on port 8082, So you'll be able to visit the site microservice under address http://localhost:8082.
The organization microservice will start on port 8083, So you'll be able to visit the organization microservice under address http://localhost:8083.
-
View info about microservices (Site, User and Organization)
-
Check Health for every microservice
-
Access services' APIs:
CURL GET command samples for different (User, Site, Organization) microservices:
-
Display info about User microservice
curl -X GET 'http://localhost:8081/actuator/info' -
Check Health for User microservice
curl -X GET 'http://localhost:8081/actuator/health' -
List all users
curl -X GET 'http://localhost:8081/api/users' -
Find user by Id
curl -X GET 'http://localhost:8081/api/users/1' -
Filter users by SiteId
curl -X GET 'http://localhost:8081/api/users/site/3' -
Filter users by OrganizationId
curl -X GET 'http://localhost:8081/api/users/organization/1'
-
Display info about Site microservice
curl -X GET 'http://localhost:8082/actuator/info' -
Check Health for Site microservice
curl -X GET 'http://localhost:8082/actuator/health' -
List all sites
curl -X GET 'http://localhost:8082/api/sites' -
Find site by Id
curl -X GET 'http://localhost:8082/api/sites/1' -
Filter sites by OrganizationId
curl -X GET 'http://localhost:8082/api/sites/organization/1' -
Filter sites with users by OrganizationId
curl -X GET 'http://localhost:8082/api/sites/organization/1/with-users'
-
Display info about Organization microservice
curl -X GET 'http://localhost:8083/actuator/info' -
Check Health for Organization microservice
curl -X GET 'http://localhost:8083/actuator/health' -
List all organizations
curl -X GET 'http://localhost:8083/api/organizations' -
Find organization by Id
curl -X GET 'http://localhost:8083/api/organizations/1' -
Find organization with sites by Id
curl -X GET 'http://localhost:8083/api/organizations/1/with-sites' -
Find organization with sites and users by Id
curl -X GET 'http://localhost:8083/api/organizations/1/with-sites-with-users' -
Find organization with users by Id
curl -X GET 'http://localhost:8083/api/organizations/1/with-users'
I am Sayed Baladoh - Phd. Senior Software Engineer. I like software development. You can contact me via:
Any improvement or comment about the project is always welcome! As well as others shared their code publicly I want to share mine! Thanks!
Thanks for reading. Did I help you?
- Share it with someone you think it might be helpful.
- Give a star to this project

