A comprehensive microservices project demonstrating service discovery, inter-service communication, and distributed architecture using Spring Boot, Spring Cloud, and Netflix Eureka.
- Overview
- Architecture
- Technologies Used
- Project Structure
- Services
- Prerequisites
- Getting Started
- API Endpoints
- Service Communication
- Configuration
- Building and Running
- Testing
- Future Enhancements
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
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Discovery Server โ
โ (Netflix Eureka) โ
โ Port: 8761 โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Register โ Register
โ โ
โโโโโโโโโผโโโโโโโโโโโ โโโโโโโโโผโโโโโโโโโโโ
โ Product Service โ โ Order Service โ
โ Port: 8081 โโโโโโโโโโ Port: 8082 โ
โ โ Feign โ โ
โโโโโโโโโโโโโโโโโโโโ Client โโโโโโโโโโโโโโโโโโโโ
- Discovery Server starts first and provides service registry
- Product Service registers itself with Eureka on startup
- Order Service registers itself with Eureka on startup
- Order Service discovers Product Service through Eureka
- Communication between services happens using OpenFeign with client-side load balancing
- Spring Boot 4.0.1: Core framework for microservices
- Spring Cloud 2025.1.0: Cloud-native patterns and tools
- Netflix Eureka: Service discovery and registration
- OpenFeign: Declarative REST client for inter-service communication
- Spring Cloud LoadBalancer: Client-side load balancing
- 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
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
- 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- 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)
- 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
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
java -version # Should show Java 17 or 21
mvn -version # Should show Maven 3.6+git clone https://github.com/AryanDevCodes/Microservices.git
cd Microservices/Basic1\ -\ Product-order-discoverycd discovery-server
./mvnw spring-boot:run
# Or on Windows: mvnw.cmd spring-boot:runWait until you see: Started DiscoveryServerApplication
Access Eureka Dashboard at: http://localhost:8761
cd ../Product
./mvnw spring-boot:runWait until you see: Started ProductApplication
cd ../Order
./mvnw spring-boot:runWait until you see: Started OrderApplication
Open Eureka Dashboard (http://localhost:8761) and verify:
PRODUCT-SERVICEis registeredORDER-SERVICEis registered
GET http://localhost:8081/productsResponse:
[
{
"id": 1,
"productName": "Laptop",
"productPrice": 1200.0
},
{
"id": 2,
"productName": "Smartphone",
"productPrice": 800.0
},
{
"id": 3,
"productName": "Tablet",
"productPrice": 400.0
}
]GET http://localhost:8081/products/{id}Example:
GET http://localhost:8081/products/1Response:
{
"id": 1,
"productName": "Laptop",
"productPrice": 1200.0
}GET http://localhost:8082/orders/create/{productId}Example:
GET http://localhost:8082/orders/create/2Response:
Ordered product: Smartphone with price: 800.0
- Order Service uses OpenFeign client to call Product Service
- Feign Client (
ProductClient) is annotated with@FeignClient(name = "product-service") - Service discovery happens through Eureka:
- Order Service queries Eureka for
product-serviceinstances - Eureka returns available instances
- Load balancer selects an instance
- Request is routed to the selected Product Service instance
- Order Service queries Eureka for
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();
}
}spring.application.name=discovery-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=falsespring.application.name=product-service
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=truespring.application.name=order-service
server.port=8082
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true# 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 ..# 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# 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- Test Product Service:
curl http://localhost:8081/products
curl http://localhost:8081/products/1- Test Order Service:
curl http://localhost:8082/orders/create/1- Check Service Registration:
- Open browser: http://localhost:8761
- Verify both services are listed
# Test Product Service
cd Product
./mvnw test
# Test Order Service
cd ../Order
./mvnw test
# Test Discovery Server
cd ../discovery-server
./mvnw testThis project demonstrates:
- Microservices Architecture: Building independent, deployable services
- Service Discovery: Using Netflix Eureka for dynamic service registration
- Inter-Service Communication: Using OpenFeign for REST API calls
- Load Balancing: Client-side load balancing with Spring Cloud LoadBalancer
- Spring Cloud: Implementing cloud-native patterns
- RESTful APIs: Designing and implementing REST endpoints
- Dependency Management: Using Maven for multi-module projects
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
- 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
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
AryanDevCodes
- GitHub: @AryanDevCodes
- Spring Boot and Spring Cloud teams
- Netflix OSS for Eureka
- The microservices community
Happy Coding! ๐