Java
E-commerce Microservice
Design a microservices-based e-commerce backend with Product and Order services.
By TechCoder TeamLast updated: 2026-06-02
In a Nutshell
Design a microservices-based e-commerce backend with Product and Order services. This hands-on tutorial focuses on practical implementation of e-commerce microservice concepts.
Project: E-commerce Microservice Architecture
We will design a system with two services: Product Service and Order Service.
Architecture
- Eureka Server: Service Discovery.
- Product Service: Manages inventory.
- Order Service: Places orders and communicates with Product Service.
- API Gateway: Entry point.
1. Product Service
ProductController
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) {
return repository.findById(id).orElseThrow();
}
2. Order Service
Needs to call Product Service to check availability/price.
Feign Client
Declarative REST client.
@FeignClient(name = "product-service")
public interface ProductClient {
@GetMapping("/products/{id}")
ProductDTO getProduct(@PathVariable Long id);
}
OrderService
@Service
public class OrderService {
@Autowired
private ProductClient productClient;
public Order placeOrder(Long productId, int quantity) {
// Call Product Service
ProductDTO product = productClient.getProduct(productId);
// Logic
Order order = new Order();
order.setProductId(productId);
order.setAmount(product.getPrice() * quantity);
return repository.save(order);
}
}
3. Communication
Using Spring Cloud OpenFeign allows services to talk to each other like calling a local method.
4. Resilience
Add Resilience4j Circuit Breaker if Product Service is down.
@CircuitBreaker(name = "productService", fallbackMethod = "fallbackProduct")
public ProductDTO getProduct(Long id) {
return productClient.getProduct(id);
}
public ProductDTO fallbackProduct(Long id, Throwable t) {
return new ProductDTO(id, "Default Product", 0.0);
}
AI Mentor
Confused about "Microservices architecture communication using Feign Client"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3What is Eureka used for?
Database
Service Discovery
Logging