Sample microservices application for managing products and shopping lists using:
This application consists of four different services:
8001
.8002
.8761
.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:
pom.xml
resides.mvn clean compile
.mvn package
.target
directory of the dist
module: cd dist/target
.microservices-1.0
. Change into this folder: cd microservices-1.0
.service-discovery
application: java -jar service-discovery-1.0.jar
.http://localhost:8761
.product-service
application: java -jar product-service-1.0.jar
.8001
and it will automatically register itself in the service discovery. Check the Eureka console.shopping-list-service
application: java -jar shopping-list-service-1.0.jar
.8002
and it will automatically register itself in the service discovery. Check the Eureka console.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.