Skip to main content

• Section 1-3 : Main Product Microservices

Section 1-3 : Build Main Product Microservices

Building squence

Microservices Building squence one should follow,

  • 1] inventory-quantity-check-service

  • 2] order-service-v1

But Listed here as per Requirements Specs.

Build order-service-v1

Build And Run Project emapi-order-svc-v1

  • Compile and Run project
mvn -version
java -version
cd C:\work\saas-multi\main-prod\emapi-order-svc-v1
mvn package
java -jar app\dbrest\target\dbrest-1.0-SNAPSHOT.jar
  • Verify Run Ok
2024-09-17T17:48:18.795+05:30  INFO 27464 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 9070 (http) with context path ''
2024-09-17T17:48:18.810+05:30 INFO 27464 --- [ main] c.e.emapi.app.EmDbRestAppRestSpringApp : Started EmDbRestAppRestSpringApp in 8.993 seconds (process running for 9.511)

Low-Code Customize to Build Microservice

  • Open Project in Explorer

  • Import Maven Project in IntelliJ IDEA

    • C:\work\saas-multi\main-prod\emapi-order-svc-v1
  • Add New API endpoint

Project View

Add New API endpoint details

  • Add API endpoint to

    • C:\work\saas-multi\main-prod\emapi-order-svc-v1\app\dbrest\src\main\java\com\example\emapi\app\Orders\OrdersDataRestController.java
  • Add new function:

  • Uncomment all imports for WebClient use, under heading:
// Uncomment For Communicate Consumer-Service and Data Mesh API 

  • CreateOrder function:
// -------------------- CreateOrder -------------------------

@PostMapping("orders/CreateOrder")
@Transactional
public ResponseEntity<Orders> OrdersCreateOrder(@RequestBody Orders OrdersTblRec1)
throws Exception
{
long productId = OrdersTblRec1.getProductId();
long orderQuantity = OrdersTblRec1.getOrderQuantity();

// verify inventory exists, call sync inventory-quantity-check-service
// ---- Consumer Service : Call Producer-Service--------------------------------------------
// Get data from REST API call (sample shows getting data from same table ViewAll API call)
// Customize to call another microservice on different server-port / URI
// - Provisioning API contract data model java class for another microservice API
//-------------------------
String get_data_rest_url = "http://127.0.0.1:9072/emdbrest/inventory/CheckQuantity?productId="+productId+"&orderQuantity="+orderQuantity;
WebClient webClientRest = WebClient.builder().baseUrl(get_data_rest_url)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

Mono<Boolean> responseRest =
webClientRest.get()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).retrieve()
.bodyToMono(new ParameterizedTypeReference<Boolean>() {});

Boolean inventoryStatus = responseRest.block();

System.out.println("Fetched From Inventory-Service - Status : "+inventoryStatus);
//-------------------------

if (inventoryStatus == true) {

Calendar currTime = Calendar.getInstance();
currTime.setTime(new java.util.Date());
OrdersTblRec1.setOrderDate(currTime);
OrdersTblRec1.setCreated(currTime);
OrdersTblRec1.setUpdated(currTime);
OrdersTblRec1.setOrderStatus("Created");
Orders _Orders = ordersService.OrdersCreate(OrdersTblRec1);
return new ResponseEntity<>(_Orders, HttpStatus.CREATED);
} else {
OrdersTblRec1.setOrderStatus("QuantityCheckFail");
return new ResponseEntity<>(OrdersTblRec1, HttpStatus.CREATED);
}
}

API call success

New API endpoint - Verify running ok details

CreateOrder service call:

curl -X 'POST' \
'http://127.0.0.1:9070/emdbrest/orders/CreateOrder' \
-H 'accept: application/hal+json' \
-H 'Content-Type: application/json' \
-d ' {
"orderId": 2,
"customerId": 1,
"tenantId": 1,
"productId": 1,
"orderQuantity": 20
}
'

Microservice Calls synchronous Inventory Service API endpoint: http://127.0.0.1:9072/emdbrest/inventory/CheckQuantity?productId=1&orderQuantity=20

  • Gets Value Return: true

Inventory Service Log entry for same:

2024-09-18T20:33:32.228+05:30  INFO 48276 --- [nio-9072-exec-1] c.e.e.a.I.InventoryDataRestController    : Inventory Consumption: productId: 1, orderQuantity: 20

The order-service returns updated Order record with status and timestamps added:

{
"orderId": 2,
"customerId": 1,
"tenantId": 1,
"productId": 1,
"orderDate": "2024-09-18",
"orderQuantity": 20,
"orderStatus": "Created",
"created": "2024-09-18 20:33",
"updated": "2024-09-18 20:33"
}
  • Quantity Check Fail test:

API call fail

CreateOrder call:

curl -X 'POST' \
'http://127.0.0.1:9070/emdbrest/orders/CreateOrder' \
-H 'accept: application/hal+json' \
-H 'Content-Type: application/json' \
-d ' {
"orderId": 2,
"customerId": 1,
"tenantId": 1,
"productId": 1,
"orderQuantity": 3000
}
'

Returns

{
"orderId": 2,
"customerId": 1,
"tenantId": 1,
"productId": 1,
"orderDate": null,
"orderQuantity": 3000,
"orderStatus": "QuantityCheckFail",
"created": null,
"updated": null
}
  • Notice "orderStatus": "QuantityCheckFail"

Build inventory-quantity-check-service

Build And Run Project emapi-inv-check-svc

  • Compile and Run project
mvn -version
java -version
cd C:\work\saas-multi\main-prod\emapi-inv-check-svc
mvn package
java -jar app\dbrest\target\dbrest-1.0-SNAPSHOT.jar
  • Verify Run Ok
2024-09-17T18:00:43.872+05:30  INFO 26564 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 9072 (http) with context path ''
2024-09-17T18:00:43.888+05:30 INFO 26564 --- [ main] c.e.emapi.app.EmDbRestAppRestSpringApp : Started EmDbRestAppRestSpringApp in 8.281 seconds (process running for 8.762)

Low-Code Customize to Build Microservice

  • Open Project in Explorer

  • Import Maven Project in IntelliJ IDEA

    • C:\work\saas-multi\main-prod\emapi-inv-check-svc
  • Add New API endpoint

Project View

Add New API endpoint details

  • Add API endpoint to
    • C:\work\saas-multi\main-prod\emapi-inv-check-svc\app\dbrest\src\main\java\com\example\emapi\app\Inventory\InventoryDataRestController.java

Add logger variable, if not present:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class InventoryDataRestController {

private final Logger logger = LogManager.getLogger(this.getClass());

Add new function:

// -------------------- CheckQuantity -------------------------

@GetMapping("inventory/CheckQuantity")
public ResponseEntity<Boolean> InventoryCheckQuantity(@RequestParam long productId, @RequestParam long orderQuantity )
throws Exception
{

Boolean checkQuantityStatus = false;

List<Inventory> InventoryList = inventoryService.InventoryQuery(productId);

if (InventoryList.isEmpty()) {
return new ResponseEntity<>(checkQuantityStatus, HttpStatus.OK);
}

Inventory inventoryRec = InventoryList.get(0);
long inventoryQty = inventoryRec.getInventoryQty();
if ( orderQuantity <= inventoryQty ) {
checkQuantityStatus = true;
// update quantity - reduce ordered qty
inventoryRec.setInventoryQty(inventoryQty - orderQuantity);
Inventory _Inventory = inventoryService.InventoryUpdate(productId, inventoryRec);

//LOG message
logger.info("Inventory Consumption: productId: "+productId+", orderQuantity: "+orderQuantity);
}

return new ResponseEntity<>(checkQuantityStatus, HttpStatus.OK);
}

API call

New API endpoint - Verify running ok details

Initial state:

curl -X 'GET' \
'http://127.0.0.1:9072/emdbrest/inventory/Query?productId=2' \
-H 'accept: application/hal+json'
[
{
"productId": 2,
"inventoryDate": "2024-08-18",
"inventoryQty": 600
}
]

Service API call:

curl -X 'GET' \
'http://127.0.0.1:9072/emdbrest/inventory/CheckQuantity?productId=2&orderQuantity=150' \
-H 'accept: application/hal+json'

Returns:

true

Again http://127.0.0.1:9072/emdbrest/inventory/Query?productId=2 :

[
{
"productId": 2,
"inventoryDate": "2024-08-18",
"inventoryQty": 450
}
]
  • Notice inventory quantity is reduced by consumed order qty.

Log write check:

2024-09-18T00:21:49.784+05:30  INFO 13776 --- [nio-9072-exec-5] c.e.e.a.I.InventoryDataRestController    : Inventory Consumption: productId: 2, orderQuantity: 150

Summary

info

Building below is complete!

  • Enterprise Microservices Development - Main Product Microservices V1