Contact us : contact@digitechgenai.com
Introduction
In this case study, we’ll build a simple e-commerce platform using Spring Boot and cloud-native development principles. Our goal is to create a scalable, resilient, and efficient application that can handle high traffic and provide a seamless user experience.
Components of the E-commerce Platform
The platform will have several microservices, each responsible for a specific functionality:
- Product Service: Manages product information and inventory.
- Order Service: Handles order creation, processing, and tracking.
- Payment Service: Manages payment processing and transactions.
- User Service: Handles user registration, authentication, and profile management.
Step 1: Setting Up the Product Service
Let’s start by setting up the Product Service, which will manage product information. We’ll use Spring Boot to create this microservice.
- Create a Spring Boot Project: Use Spring Initializr (https://start.spring.io) to create a new Spring Boot project with the following dependencies:
- Spring Web
- Spring Data JPA
- H2 Database (for simplicity)
- Spring Boot Actuator
- Define the Product Entity: Create an entity
Product
to represent the product information.
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private Double price;
// Getters and Setters
}
3. Create the Product Repository: Create an interface ProductRepository
to handle CRUD operations.
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
4. Develop the Product Service: Create a Product Service
class to manage business logic.
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> getAllProducts() {
return productRepository.findAll();
}
public Product addProduct(Product product) {
return productRepository.save(product);
}
public Optional<Product> getProductById(Long id) {
return productRepository.findById(id);
}
public void deleteProduct(Long id) {
productRepository.deleteById(id);
}
}
5. Build the REST Controller: Create a ProductController
class to expose REST APIs.
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
@PostMapping
public Product addProduct(@RequestBody Product product) {
return productService.addProduct(product);
}
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.getProductById(id).orElse(null);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
}
}
6. Run the Application: Run the application and test the endpoints using Postman or any other API testing tool
Step 2: Implementing Service Discovery with Eureka
Service discovery helps microservices find each other. We’ll use Netflix Eureka for this purpose.
- Create a Eureka Server: Use Spring Initializr to create a new Spring Boot project with the Eureka Server dependency. Add the following configuration to
application.properties
:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Annotate the main application class with.@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2. Register Product Service with Eureka: Add the Eureka Client dependency to the Product Service project and configure it in application.properties
:
spring.application.name=product-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
Annotate the main application class with @EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
Step 3: Centralized Configuration with Spring Cloud Config
Centralized configuration is essential for managing multiple microservices.
- Create a Config Server: Use Spring Initializr to create a new Spring Boot project with the Spring Cloud Config Server dependency. Configure
application.properties
:
spring.cloud.config.server.git.uri=https://github.com/your-config-repo
Annotate the main application class with @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2. Configure Product Service: Add the Spring Cloud Config dependency to the Product Service project and configure it in application.properties
:
spring.cloud.config.uri=http://localhost:8888
Create a bootstrap.properties
file for environment-specific configurations.
Step 4: Distributed Tracing with Spring Cloud Sleuth and Zipkin
Distributed tracing helps monitor and troubleshoot microservices.
- Add Sleuth and Zipkin Dependencies: Add the Spring Cloud Sleuth and Zipkin dependencies to the Product Service project.
- Configure Tracing: Configure the tracing properties in
application.properties
:
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
Step 5: Containerizing with Docker and Orchestrating with Kubernetes
Docker and Kubernetes help deploy and manage cloud-native applications.
- Create a Dockerfile: Create a
Dockerfile
for the Product Service application.
FROM openjdk:11-jre-slim
VOLUME /tmp
COPY target/product-service-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
2. Build and Run Docker Image: Build and run the Docker image.
docker build -t product-service .
docker run -p 8080:8080 product-service
3. Create Kubernetes Deployment: Create a Kubernetes deployment and service file for the Product Service.
apiVersion: apps/v1
kind: Deployment
metadata:
name: product-service
spec:
replicas: 3
selector:
matchLabels:
app: product-service
template:
metadata:
labels:
app: product-service
spec:
containers:
- name: product-service
image: product-service:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: product-service
spec:
selector:
app: product-service
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
4. Deploy to Kubernetes: Deploy the product service to Kubernetes.
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
To Read more spring boots, click here.
Conclusion
By following these steps, we’ve set up a simple e-commerce platform using Spring Boot and cloud-native development principles. We created a product service, implemented service discovery with Eureka, centralized configuration with Spring Cloud Config, distributed tracing with Spring Cloud Sleuth and Zipkin, and containerized and orchestrated the application with Docker and Kubernetes. This approach ensures scalability, resilience, and efficiency for modern applications.
To know more about the courses, refer to : Courses