Skip to content

AryanDevCodes/Microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Microservices Architecture with Spring Boot and Eureka

A comprehensive microservices project demonstrating service discovery, inter-service communication, and distributed architecture using Spring Boot, Spring Cloud, and Netflix Eureka.

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This project implements a microservices architecture with three core components:

  • Discovery Server: Service registry using Netflix Eureka
  • Product Service: Manages product catalog
  • Order Service: Handles order operations and communicates with Product Service

The architecture demonstrates key microservices patterns including:

  • Service Discovery and Registration
  • Client-Side Load Balancing
  • Inter-Service Communication using OpenFeign
  • Decentralized Data Management

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Discovery Server                         โ”‚
โ”‚                   (Netflix Eureka)                           โ”‚
โ”‚                   Port: 8761                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚                            โ”‚
             โ”‚ Register                   โ”‚ Register
             โ”‚                            โ”‚
     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
     โ”‚  Product Service โ”‚        โ”‚  Order Service   โ”‚
     โ”‚   Port: 8081     โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚   Port: 8082     โ”‚
     โ”‚                  โ”‚ Feign  โ”‚                  โ”‚
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ Client โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Architecture Flow:

  1. Discovery Server starts first and provides service registry
  2. Product Service registers itself with Eureka on startup
  3. Order Service registers itself with Eureka on startup
  4. Order Service discovers Product Service through Eureka
  5. Communication between services happens using OpenFeign with client-side load balancing

๐Ÿ› ๏ธ Technologies Used

Core Framework

  • Spring Boot 4.0.1: Core framework for microservices
  • Spring Cloud 2025.1.0: Cloud-native patterns and tools

Spring Cloud Components

  • Netflix Eureka: Service discovery and registration
  • OpenFeign: Declarative REST client for inter-service communication
  • Spring Cloud LoadBalancer: Client-side load balancing

Additional Technologies

  • Java 17/21: Programming language (Product/Discovery: Java 17, Order: Java 21)
  • Maven: Build automation and dependency management
  • Lombok: Reduces boilerplate code with annotations
  • Spring Web MVC: RESTful web services

๐Ÿ“ Project Structure

Microservices/
โ”œโ”€โ”€ Basic1 - Product-order-discovery/
โ”‚   โ”œโ”€โ”€ discovery-server/          # Eureka Server
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ main/
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ com/microservice/discoveryserver/
โ”‚   โ”‚   โ”‚       โ”‚       โ””โ”€โ”€ DiscoveryServerApplication.java
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ resources/
โ”‚   โ”‚   โ”‚           โ””โ”€โ”€ application.properties
โ”‚   โ”‚   โ””โ”€โ”€ pom.xml
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ Product/                   # Product Microservice
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ com/microservice/productservice/
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ ProductApplication.java
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ ProductController.java
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ Product.java
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ application.properties
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ test/
โ”‚   โ”‚   โ””โ”€โ”€ pom.xml
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ Order/                     # Order Microservice
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ main/
โ”‚       โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚       โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ com/microservice/orderservice/
โ”‚       โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ OrderApplication.java
โ”‚       โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ OrderController.java
โ”‚       โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ ProductClient.java
โ”‚       โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ ProductDTO.java
โ”‚       โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚       โ”‚   โ”‚       โ””โ”€โ”€ application.properties
โ”‚       โ”‚   โ””โ”€โ”€ test/
โ”‚       โ””โ”€โ”€ pom.xml
โ””โ”€โ”€ README.md

๐Ÿ”ง Services

1. Discovery Server (Eureka Server)

  • Port: 8761
  • Purpose: Service registry for microservices
  • URL: http://localhost:8761
  • Features:
    • Service registration
    • Service discovery
    • Health monitoring
    • Service instance management

Key Configuration:

spring.application.name=discovery-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

2. Product Service

  • Port: 8081
  • Purpose: Manages product catalog
  • Base URL: http://localhost:8081
  • Features:
    • Product listing
    • Product retrieval by ID
    • In-memory product data store

Sample Product Data:

  • Laptop ($1200.00)
  • Smartphone ($800.00)
  • Tablet ($400.00)

3. Order Service

  • Port: 8082
  • Purpose: Order management and product information retrieval
  • Base URL: http://localhost:8082
  • Features:
    • Create orders for products
    • Fetch product details via Feign Client
    • Service-to-service communication

โœ… Prerequisites

Before running this project, ensure you have:

  • Java JDK 17 or 21 installed
  • Maven 3.6+ installed
  • Git for cloning the repository
  • Basic understanding of:
    • Spring Boot
    • Microservices architecture
    • RESTful APIs

Check Prerequisites:

java -version    # Should show Java 17 or 21
mvn -version     # Should show Maven 3.6+

๐Ÿš€ Getting Started

1. Clone the Repository

git clone https://github.com/AryanDevCodes/Microservices.git
cd Microservices/Basic1\ -\ Product-order-discovery

2. Start Services in Order

Step 1: Start Discovery Server (First)

cd discovery-server
./mvnw spring-boot:run
# Or on Windows: mvnw.cmd spring-boot:run

Wait until you see: Started DiscoveryServerApplication

Access Eureka Dashboard at: http://localhost:8761

Step 2: Start Product Service

cd ../Product
./mvnw spring-boot:run

Wait until you see: Started ProductApplication

Step 3: Start Order Service

cd ../Order
./mvnw spring-boot:run

Wait until you see: Started OrderApplication

3. Verify Services are Running

Open Eureka Dashboard (http://localhost:8761) and verify:

  • PRODUCT-SERVICE is registered
  • ORDER-SERVICE is registered

๐Ÿ“ก API Endpoints

Product Service (Port 8081)

Get All Products

GET http://localhost:8081/products

Response:

[
  {
    "id": 1,
    "productName": "Laptop",
    "productPrice": 1200.0
  },
  {
    "id": 2,
    "productName": "Smartphone",
    "productPrice": 800.0
  },
  {
    "id": 3,
    "productName": "Tablet",
    "productPrice": 400.0
  }
]

Get Product by ID

GET http://localhost:8081/products/{id}

Example:

GET http://localhost:8081/products/1

Response:

{
  "id": 1,
  "productName": "Laptop",
  "productPrice": 1200.0
}

Order Service (Port 8082)

Create Order (with Product Info)

GET http://localhost:8082/orders/create/{productId}

Example:

GET http://localhost:8082/orders/create/2

Response:

Ordered product: Smartphone with price: 800.0

๐Ÿ”„ Service Communication

How Services Communicate:

  1. Order Service uses OpenFeign client to call Product Service
  2. Feign Client (ProductClient) is annotated with @FeignClient(name = "product-service")
  3. Service discovery happens through Eureka:
    • Order Service queries Eureka for product-service instances
    • Eureka returns available instances
    • Load balancer selects an instance
    • Request is routed to the selected Product Service instance

Code Example:

ProductClient.java (in Order Service):

@FeignClient(name = "product-service")
public interface ProductClient {
    @GetMapping("/products/{id}")
    ProductDTO getProductById(@PathVariable("id") Long id);
}

OrderController.java:

@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final ProductClient productClient;

    @GetMapping("/create/{productId}")
    public String getProductInfo(@PathVariable Long productId) {
        ProductDTO dto = productClient.getProductById(productId);
        return "Ordered product: " + dto.getProductName() 
             + " with price: " + dto.getProductPrice();
    }
}

โš™๏ธ Configuration

Discovery Server Configuration

spring.application.name=discovery-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false

Product Service Configuration

spring.application.name=product-service
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

Order Service Configuration

spring.application.name=order-service
server.port=8082
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

๐Ÿ—๏ธ Building and Running

Build All Services

# From the root of Basic1 - Product-order-discovery directory
cd discovery-server && ./mvnw clean install && cd ..
cd Product && ./mvnw clean install && cd ..
cd Order && ./mvnw clean install && cd ..

Run with Maven

# Terminal 1 - Discovery Server
cd discovery-server
./mvnw spring-boot:run

# Terminal 2 - Product Service
cd Product
./mvnw spring-boot:run

# Terminal 3 - Order Service
cd Order
./mvnw spring-boot:run

Package as JAR

# Build JAR files
cd discovery-server && ./mvnw clean package
cd ../Product && ./mvnw clean package
cd ../Order && ./mvnw clean package

# Run JAR files
java -jar discovery-server/target/discovery-server-0.0.1-SNAPSHOT.jar
java -jar Product/target/product-service-0.0.1-SNAPSHOT.jar
java -jar Order/target/order-service-0.0.1-SNAPSHOT.jar

๐Ÿงช Testing

Manual Testing

  1. Test Product Service:
curl http://localhost:8081/products
curl http://localhost:8081/products/1
  1. Test Order Service:
curl http://localhost:8082/orders/create/1
  1. Check Service Registration:

Run Unit Tests

# Test Product Service
cd Product
./mvnw test

# Test Order Service
cd ../Order
./mvnw test

# Test Discovery Server
cd ../discovery-server
./mvnw test

๐ŸŽ“ Learning Outcomes

This project demonstrates:

  1. Microservices Architecture: Building independent, deployable services
  2. Service Discovery: Using Netflix Eureka for dynamic service registration
  3. Inter-Service Communication: Using OpenFeign for REST API calls
  4. Load Balancing: Client-side load balancing with Spring Cloud LoadBalancer
  5. Spring Cloud: Implementing cloud-native patterns
  6. RESTful APIs: Designing and implementing REST endpoints
  7. Dependency Management: Using Maven for multi-module projects

๐Ÿ”ฎ Future Enhancements

Potential improvements and features to add:

  • API Gateway: Add Spring Cloud Gateway for routing
  • Configuration Server: Centralized configuration management
  • Circuit Breaker: Implement Resilience4j for fault tolerance
  • Distributed Tracing: Add Zipkin or Jaeger for request tracing
  • Database Integration: Add MongoDB/PostgreSQL for data persistence
  • Security: Implement OAuth2/JWT authentication
  • Docker Support: Containerize all services
  • Kubernetes: Deploy to Kubernetes cluster
  • Monitoring: Add Spring Boot Actuator and Prometheus
  • Message Queue: Integrate RabbitMQ or Kafka for async communication
  • API Documentation: Add Swagger/OpenAPI documentation

๐Ÿ“ Notes

  • Service Startup Order: Always start Discovery Server first, then other services
  • Port Conflicts: Ensure ports 8761, 8081, and 8082 are available
  • Java Version: Product and Discovery services use Java 17, Order service uses Java 21
  • Health Checks: All services expose actuator endpoints for health monitoring
  • Development Mode: Self-preservation is disabled for easier local development

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ‘ค Author

AryanDevCodes

๐Ÿ™ Acknowledgments

  • Spring Boot and Spring Cloud teams
  • Netflix OSS for Eureka
  • The microservices community

Happy Coding! ๐Ÿš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages