Developing a Springboard Application Using Microservices Architecture

Jun 21, 2024

Developing a Springboard Application Using Microservices Architecture

Introduction

  • Objective: Build a simple online shop application using microservices with Spring Boot and Spring Cloud.
  • Pre-requisites: Basic understanding of Spring Boot and microservices architecture.

Overview

  • Key Technologies: Spring Boot, Spring Cloud, Spring Data, Spring Security, etc.
  • Architectural Patterns: Service Discovery, Centralized Configuration, Distributed Tracing, Event-Driven Architecture, Centralized Logging.

What is Spring Cloud?

  • Part of the Spring ecosystem (like Spring Boot, Spring Data, etc.) focused on building reliable microservices.
  • Implements common design patterns: Configuration Management, Service Discovery, Circuit Breakers.
  • Main tool for developing microservices in this tutorial.

Services in the Application

  1. Product Service: API to create/view products, acts as a product catalog, uses MongoDB.
  2. Order Service: Allows ordering products, uses MySQL for storage.
  3. Inventory Service: Checks if products are in stock before order submission, uses MySQL.
  4. Notification Service: Sends email notifications after successful orders, stateless service.

Solution Architecture

  • Product Service talks to MongoDB.
  • Order Service and Inventory Service talk to MySQL database.
  • Notification Service sends emails, no database.
  • Service Communication: Order Service communicates synchronously with Inventory Service and asynchronously with Notification Service using RabbitMQ and Kafka.
  • External Services: API Gateway, Keycloak (Authorization Server), Config Server, Vault, Zipkin, Elasticsearch, Logstash, Kibana.

Microservice Design - Product Service

  1. Startup: Generate initial Spring Boot project using start.spring.io with dependencies for Lombok, Spring Web, Spring Data MongoDB.
  2. Database Configuration: Set up MongoDB URI in application.properties.
  3. Domain Model: Create Product class annotated with @Document for MongoDB, fields: id, name, description, price.
  4. Repository Layer: ProductRepository interface extending MongoRepository<Product, String>.
  5. Service Layer: ProductService with methods to create products and transform ProductRequest DTO to Product entity.
  6. Controller Layer: ProductController with REST endpoints to create and view products.
  7. Testing: Use Postman to manually test endpoints, implement integration tests using JUnit5, MockMvc, and Testcontainers for MongoDB.

Microservice Design - Order Service

  1. Startup: Generate project with Lombok, Spring Web, Spring Data JPA, MySQL Driver dependencies.
  2. Database Configuration: Define database connection properties in application.properties.
  3. Domain Model: Create Order class with OrderLineItems, establish one-to-many relationship, annotate with JPA annotations.
  4. Repository Layer: OrderRepository interface extending JpaRepository<Order, Long>.
  5. Service Layer: OrderService for business logic to place orders, map DTOs to entities.
  6. Controller Layer: OrderController with REST endpoint to place orders.
  7. Testing: Verify functionality using Postman to create orders and confirm through the console.

Microservice Design - Inventory Service

  1. Startup: Generate project with Lombok, Spring Web, Spring Data JPA, MySQL Driver dependencies.
  2. Database Configuration: Define database connection properties in application.properties.
  3. Domain Model: Create Inventory class annotated with JPA, fields: id, skuCode, quantity.
  4. Repository Layer: InventoryRepository interface extending JpaRepository<Inventory, Long>.
  5. Service Layer: InventoryService for business logic to check product stock.
  6. Controller Layer: InventoryController with REST endpoint to check stock status.
  7. Testing: Verify endpoints manually using Postman.

Setting Up Maven Multi-Module Project

  1. Project Setup: Create a parent Maven project to include Product, Order, and Inventory services as modules.
  2. Dependencies Management: Use parent POM for dependency versions, add Spring Boot starter parent for transitive dependencies.
  3. Module Creation: Define each service as a module within the parent project, configure with dependencies and plugins.

Coding Steps

  1. Product Service: Implement controllers, service methods, and repositories. Write integration tests and validate using Postman and automated tests.
  2. Order Service: Similarly implement controllers, service methods, and repositories. Validate functionality using manual and automated tests.
  3. Inventory Service: Implement stock check functionality, create controllers, service methods, and repositories. Verify using manual tests.
  4. Multi-Module Project Configuration: Ensure dependency management and plugin configuration is centralized in the parent POM. Verify the setup by running all services together.
  5. Data Initialization: Modify Inventory Service to initialize database on application startup.

Conclusion

  • Practice & Exercises: Implement tests for Get Product endpoint and other services yourself. Refer to the provided GitHub repository for complete setup and examples.
  • Next Steps: Follow along with further tutorials for more features and deeper understanding of microservices architecture.