This application demonstrates the microservice architecture using Spring Cloud. It is a basic application where we mimic the working of a bookstore platform, where in users can create their accounts, browse books, add/remove books from cart. Admins would be able to see all data about users and books. They will also be able to modify the books and authors on the platform.
- User Service - Stores information about a user.
- Inventory Service - Stores business logic about books and authors.
- Cart Service - Stores information about the cart of a user.
- Eureka Server - Service discovery server.
- Config Server - Configuration server.
- API Gateway - API gateway.
Each of the microservices have their own README.md file that provides more details about the microservice. The details include working, api documentation, events, etc.
- Spring Cloud Config Server for storing configurations
- Spring Cloud Netflix Eureka for service discovery
- Spring Cloud Gateway as the api gateway
- Resiliance4J for implementing circuit breaker pattern
- Micrometer, OpenTelemetry, Zipkin for observability
- RabbitMQ for inter service communication
- MySQL for
Product Service
- MongoDB for
Auth Service
,User Service
andCart Service
- Redis to store details about books and authors in memory.
- Docker for containerizing the services
- Docker Compose to start the all the services and peripherals at once
- Java 17
- SpringBoot 3
- Spring Cloud 2
- Microservice Architecture
admin
- Can access all the APIsuser
- Can access all the APIs except the admin APIs
To get admin privileges, you need to create an account with the email admin@bookstore.com
.
This project can be run in two profiles:
- Development
- Production For development, you have to individually run all the pre-requisites and then run the services one by one. For production, you can use docker-compose to start all the services and peripherals at once.
-
We need to set up the pre-requisites before we do anything. For that, we need to fire up a few docker containers.
- Start a mysql instance
docker run --name bms-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql
- Start a mongodb instance
docker run --name bms-mongodb -p 27017:27017 -e MONGODB_INIT_ROOT_USERNAME=admin -e MONGODB_INIT_ROOT_PASSWORD=admin -d mongo
- Start a redis instance
docker run -d --name bms-redis -p 6379:6379 -e REDIS_USER=root -e REDIS_PASSWORD=root redis
- Start a zipkin instance
docker run --name bms-zipkin -p 9411:9411 -d openzipkin/zipkin
- Start a rabbitmq instance
docker run --name bms-rmq -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=guest -e RABBITMQ_DEFAULT_PASS=guest -d rabbitmq
- Start a mysql instance
-
Once done, we need to set an environmental variable.
export BMS_PROFILE=development (i) export CONFIG_REPO_PATH=<path/to/config-repo> (ii)
- This will make the services start up in a development mode. We need this because the services will be fetching data from the config server based on the
spring.profile.active
property. - This is the path to the folder where you have the config-repo cloned. This is required because the config server will be fetching the configurations from this folder. Normally, it would be under
{USER_HOME}/path/to/bookstore-microservice/config-repo
. But, you can change it to any other location. Just make sure you set theCONFIG_REPO_PATH
to that location.
- This will make the services start up in a development mode. We need this because the services will be fetching data from the config server based on the
-
Now, we need to install the dependencies. Make sure you have
Java 17
installed locally. Once sure, you can proceed.cd bookstore-microservice folders=("api-gateway" "eureka-server" "config-server" "user-service" "cart-service" "inventory-service") for folder in "${folders[@]}"; do cd "$element" && ./mvnw clean install && cd ../ done
-
Finally, we can start the services one by one. Make sure you launch the services from the same terminal where you have set the temporary variables or from some IDE that has the same environmental variables set in the launch configuration.
cd bookstore-microservice folders=("eureka-server" "config-server" "api-gateway" "user-service" "cart-service" "inventory-service") for folder in "${folders[@]}"; do cd "$element" && ./mvnw spring-boot:run & && cd ../ done
-
You can now access the services at the following urls:
Eureka Server
- http://localhost:8761Config Server
- http://localhost:8888API Gateway
- http://localhost:8765User Service
- http://localhost:8001Cart Service
- http://localhost:8002Inventory Service
- http://localhost:8004Zipkin
- http://localhost:9411RabbitMQ
- http://localhost:15672MySQL
- http://localhost:3306MongoDB
- http://localhost:27017Redis
- http://localhost:6379
Make sure that you make the API calls to
API Gateway
ONLY. Otherwise the authentication won't work. Enjoy!
TODO