Sample microservices application for managing products and shopping lists using:
- Spring Boot: Framework for creating standalone Java applications.
- Jersey: JAX-RS reference implementation for creating RESTful web services in Java.
- Jackson: JSON parser for Java.
- MapStruct: Framework for mapping objects in Java.
- Netflix Zuul: API gateway.
- Netflix Eureka: Service discovery.
- RabbitMQ: Message broker.
- MongoDB: NoSQL database based on documents.
This application consists of four different services:
- Product service: Provides API for managing products. By default it runs on port
8001
. - Shopping list service: Provides API for managing shopping lists. By default it runs on port
8002
. - Service discovery: Netflix Eureka service that discovers and registers other service instances. By default it runs on port
8761
. - API gateway: Netflix Zuul API gateway that sits on the top of the product and shopping list services, providing a gateway for those services. By default it runs on port
8765
.
See the diagram below:
This application depends on external services that must be up and running before attempting to run the application:
Shopping and product services use MongoDB for persistence, but different databases are used for each service.
Before running the application, ensure that you have a MongoDB instance running on localhost
port 27017
(default port). The product
and shopping-list
databases will be created by the application if they don't exist.
RabbitMQ is used as message broker for communication between the services. When a product is deleted, a message is produced by the product service. This message contains details about the product that has been deleted. The shopping list service consumes the message and removes the deleted product from the shopping lists.
Before running the application, ensure that a RabbitMQ instance is running on localhost
port 5672
(default port).
To build and run this application, follow these steps:
- Open a command line window or terminal.
- Navigate to the root directory of the project, where the
pom.xml
resides. - Compile the project:
mvn clean compile
. - Package the application:
mvn package
. - Change into the
target
directory of thedist
module:cd dist/target
. - You should see a folder with the following or a similar name:
microservices-1.0
. Change into this folder:cd microservices-1.0
. - Start the services as indicated below (the order doesn't matter).
- Open a command line window or terminal.
- Start the
service-discovery
application:java -jar service-discovery-1.0.jar
. - A Netflix Eureka console will be available at
http://localhost:8761
.
- Open a command line window or terminal.
- Start the
product-service
application:java -jar product-service-1.0.jar
. - This service will start on the port
8001
and it will automatically register itself in the service discovery. Check the Eureka console.
- Open a command line window or terminal.
- Start the
shopping-list-service
application:java -jar shopping-list-service-1.0.jar
. - This service will start on the port
8002
and it will automatically register itself in the service discovery. Check the Eureka console.
- Open a command line window or terminal.
- Start the
api-gateway
application:java -jar api-gateway-1.0.jar
.
If you want to, you can run extra instances of product-service
and shopping-list-service
applications, just use a different port: java -DPORT=8003 -jar product-service-1.0.jar
. New instances will automatically register themselves in the service discovery.
Requests coming from the api-gateway
service will be balanced between the instances.
The application provides a REST API for managing tasks. See the curl scripts below with the supported operations:
curl -X GET \
'http://localhost:8765/api/products' \
-H 'Accept: application/json'
curl -X POST \
'http://localhost:8765/api/products' \
-H 'Content-Type: application/json' \
-d '{
"name": "Cake"
}'
curl -X GET \
'http://localhost:8765/api/products/{product-id}' \
-H 'Accept: application/json'
curl -X POST \
'http://localhost:8765/api/products/{product-id}' \
-H 'Content-Type: application/json' \
-d '{
"name": "Chocolate cake"
}'
curl -X DELETE \
'http://localhost:8765/api/products/{product-id}'
curl -X GET \
'http://localhost:8765/api/shopping-lists' \
-H 'Accept: application/json'
curl -X POST \
'http://localhost:8765/api/shopping-lists' \
-H 'Content-Type: application/json' \
-d '{
"name": "My shopping list",
"items": [
{
"id": "{product-id}"
},
{
"id": "{product-id}"
},
...
]
}'
curl -X GET \
'http://localhost:8765/api/shopping-lists/{shopping-list-id}' \
-H 'Accept: application/json'
curl -X PUT \
'http://localhost:8765/api/shopping-lists/{shopping-list-id}' \
-H 'Content-Type: application/json' \
-d '{
"name": "Birthday party",
"items": [
{
"id": "{product-id}"
},
{
"id": "{product-id}"
},
...
]
}'
curl -X DELETE \
'http://localhost:8765/api/shopping-lists/{shopping-list-id}'
Alternatively to curl, you can use Postman to target the REST API. Check the Postman collection and the environment variables as well.