Building a Scalable eCommerce Platform with Microservices using .NET Core and Azure Kubernetes Service (AKS)
Rahul Sahay
Architect @IBM Software Labs (ISL) | Microservices | Arch/Design | Service Mesh | Cloud | Full Stack | DevOps( Docker,K8s) | ELK Stack| Azure | Dotnet | SpringBoot | Python | Open AI | Gen AI
Introduction
In today's competitive digital marketplace, building a scalable and resilient eCommerce platform is a top priority for businesses. This comprehensive guide dives deep into how to architect and implement a cutting-edge eCommerce solution using microservices architecture, .NET Core, and Azure Kubernetes Service (AKS).
If you're looking to transition from a monolithic to a microservices design, or want to understand how container orchestration with Kubernetes enhances scalability, you've come to the right place. This blog post is a one-stop resource for software architects, DevOps engineers, and .NET developers aiming to leverage cloud-native technologies and microservices patterns for eCommerce. Get ready to unlock the full potential of microservices, containerization, and cloud computing to build an eCommerce platform that's not just robust but also future-proof.
2. Data Storage
3. Communication Between Services
eCommerce Microservices Templates
Product Catalog Service
Inventory Service
Order Management Service
User and Authentication Service
Shopping Cart Service
Low-Level Design and Implementation
Setting Up the Development Environment
Creating Microservices Templates
Dockerizing the Microservices
Deploying to AKS
Implementing CI/CD
Monitoring and Logging
Case Study: Building an eCommerce Platform
Model Classes
public class Product
{
? ? public int Id { get; set; }
? ? public string Name { get; set; }
? ? public string Description { get; set; }
? ? public decimal Price { get; set; }
? ? public string ImageUrl { get; set; }
? ? public int CategoryId { get; set; }
? ? public Category Category { get; set; }
? ? // Additional properties like StockQuantity, SKU, etc.
}
Products can belong to different categories like Electronics, Clothing, etc.
public class Category
{
? ? public int Id { get; set; }
? ? public string Name { get; set; }
? ? public List<Product> Products { get; set; }
}
Information about the brand of the product.
public class Brand
{
? ? public int Id { get; set; }
? ? public string Name { get; set; }
? ? public List<Product> Products { get; set; }
}
Tags can be used to add additional categorization and search functionality.
public class Tag
{
? ? public int Id { get; set; }
? ? public string Name { get; set; }
? ? public List<ProductTag> ProductTags { get; set; }
}
This is a join entity for the many-to-many relationship between Products and Tags.
public class ProductTag
{
? ? public int ProductId { get; set; }
? ? public Product Product { get; set; }
? ? public int TagId { get; set; }
? ? public Tag Tag { get; set; }
}
Entity Framework Core Configuration
You can use Entity Framework Core to configure these models and their relationships.
// Inside your DbContext class
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
? ? modelBuilder.Entity<ProductTag>()
? ? ? ? .HasKey(pt => new { pt.ProductId, pt.TagId });
? ? modelBuilder.Entity<ProductTag>()
? ? ? ? .HasOne(pt => pt.Product)
? ? ? ? .WithMany(p => p.ProductTags)
? ? ? ? .HasForeignKey(pt => pt.ProductId);
? ? modelBuilder.Entity<ProductTag>()
? ? ? ? .HasOne(pt => pt.Tag)
? ? ? ? .WithMany(t => t.ProductTags)
? ? ? ? .HasForeignKey(pt => pt.TagId);
}
API Endpoints
You can expose several RESTful API endpoints to interact with the Product Catalog:
Service Implementation for Product Catalog in .NET Core
Setting up the DbContext
First, let's set up the DbContext to manage the entities.
using Microsoft.EntityFrameworkCore;
public class ProductCatalogDbContext : DbContext
{
? ? public DbSet<Product> Products { get; set; }
? ? public DbSet<Category> Categories { get; set; }
? ? public DbSet<Brand> Brands { get; set; }
? ? public DbSet<Tag> Tags { get; set; }
? ? public DbSet<ProductTag> ProductTags { get; set; }
? ? // ... OnModelCreating method to configure relationships
}
Product Service
Create an interface for the product service.
领英推荐
public interface IProductService
{
? ? IEnumerable<Product> GetAllProducts();
? ? Product GetProductById(int id);
? ? void AddProduct(Product product);
? ? void UpdateProduct(Product product);
? ? void DeleteProduct(int id);
}
Now, implement the service.
public class ProductService : IProductService
{
? ? private readonly ProductCatalogDbContext _dbContext;
? ? public ProductService(ProductCatalogDbContext dbContext)
? ? {
? ? ? ? _dbContext = dbContext;
? ? }
? ? public IEnumerable<Product> GetAllProducts()
? ? {
? ? ? ? return _dbContext.Products.Include(p => p.Category).ToList();
? ? }
? ? public Product GetProductById(int id)
? ? {
? ? ? ? return _dbContext.Products
? ? ? ? ? ? ? ? ? ? ? ? ?.Include(p => p.Category)
? ? ? ? ? ? ? ? ? ? ? ? ?.FirstOrDefault(p => p.Id == id);
? ? }
? ? public void AddProduct(Product product)
? ? {
? ? ? ? _dbContext.Products.Add(product);
? ? ? ? _dbContext.SaveChanges();
? ? }
? ? public void UpdateProduct(Product product)
? ? {
? ? ? ? _dbContext.Products.Update(product);
? ? ? ? _dbContext.SaveChanges();
? ? }
? ? public void DeleteProduct(int id)
? ? {
? ? ? ? var product = GetProductById(id);
? ? ? ? if (product != null)
? ? ? ? {
? ? ? ? ? ? _dbContext.Products.Remove(product);
? ? ? ? ? ? _dbContext.SaveChanges();
? ? ? ? }
? ? }
}
Category Service
Similarly, you can create services for categories.
public interface ICategoryService
{
? ? IEnumerable<Category> GetAllCategories();
? ? // ... other methods
}
public class CategoryService : ICategoryService
{
? ? private readonly ProductCatalogDbContext _dbContext;
? ? public CategoryService(ProductCatalogDbContext dbContext)
? ? {
? ? ? ? _dbContext = dbContext;
? ? }
? ? public IEnumerable<Category> GetAllCategories()
? ? {
? ? ? ? return _dbContext.Categories.ToList();
? ? }
? ? // ... other methods
}
Controllers
Finally, you can create controllers to expose these services via RESTful APIs.
[ApiController]
[Route("[controller]")]
public class ProductsController : ControllerBase
{
? ? private readonly IProductService _productService;
? ? public ProductsController(IProductService productService)
? ? {
? ? ? ? _productService = productService;
? ? }
? ? [HttpGet]
? ? public IEnumerable<Product> GetAll()
? ? {
? ? ? ? return _productService.GetAllProducts();
? ? }
? ? [HttpGet("{id}")]
? ? public Product GetById(int id)
? ? {
? ? ? ? return _productService.GetProductById(id);
? ? }
? ? [HttpPost]
? ? public void Add(Product product)
? ? {
? ? ? ? _productService.AddProduct(product);
? ? }
? ? [HttpPut("{id}")]
? ? public void Update(int id, Product product)
? ? {
? ? ? ? if (id != product.Id)
? ? ? ? {
? ? ? ? ? ? // Handle the ID mismatch
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? _productService.UpdateProduct(product);
? ? }
? ? [HttpDelete("{id}")]
? ? public void Delete(int id)
? ? {
? ? ? ? _productService.DeleteProduct(id);
? ? }
}
Dependency Injection
Don't forget to register your services and DbContext in the Startup.cs file.
public void ConfigureServices(IServiceCollection services)
{
? ? services.AddDbContext<ProductCatalogDbContext>(options =>
? ? ? ? options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
? ? services.AddScoped<IProductService, ProductService>();
? ? services.AddScoped<ICategoryService, CategoryService>();
? ? // ... other configurations
}
Docker Configuration
Create a Dockerfile
Create a Dockerfile in the root directory of your .NET Core project.
# Use the SDK image to build the app
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
Build Docker Image
docker build -t myapp:latest .
Push to Azure Container Registry (ACR)
docker tag myapp:latest myacr.azurecr.io/myapp:latest
docker push myacr.azurecr.io/myapp:latest
Kubernetes Configuration
Create a Deployment YAML File (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
? name: myapp-deployment
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: myapp
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: myapp
? ? spec:
? ? ? containers:
? ? ? - name: myapp
? ? ? ? image: myacr.azurecr.io/myapp:latest
? ? ? ? ports:
? ? ? ? - containerPort: 80
Apply the Deployment
Create a Deployment YAML File (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
? name: myapp-deployment
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: myapp
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: myapp
? ? spec:
? ? ? containers:
? ? ? - name: myapp
? ? ? ? image: myacr.azurecr.io/myapp:latest
? ? ? ? ports:
? ? ? ? - containerPort: 80
Apply the Deployment
kubectl apply -f deployment.yaml
Azure DevOps CI/CD Pipeline
trigger:
- main
pool:
? vmImage: 'ubuntu-latest'
variables:
? dockerRegistryServiceConnection: '<Your Service Connection>'
? imageRepository: 'myapp'
? containerRegistry: 'myacr.azurecr.io'
? dockerfilePath: '$(Build.SourcesDirectory)/MyApp/Dockerfile'
? tag: '$(Build.BuildId)'
steps:
- task: Docker@2
? inputs:
? ? containerRegistry: $(dockerRegistryServiceConnection)
? ? repository: $(imageRepository)
? ? command: 'buildAndPush'
? ? Dockerfile: $(dockerfilePath)
? ? tags: $(tag)
Configure the Release Pipeline
- task: Kubernetes@1
? inputs:
? ? connectionType: 'Azure Resource Manager'
? ? azureSubscriptionEndpoint: '<Your Azure Subscription>'
? ? azureResourceGroup: '<Your Resource Group>'
? ? kubernetesCluster: '<Your AKS Cluster>'
? ? command: 'apply'
? ? useConfigurationFile: true
? ? configurationPath: 'deployment.yaml'
Trigger the Pipeline
Once everything is set up, you can manually trigger the pipeline or push a change to your repository to trigger it automatically.
This should give you a comprehensive overview of how to Dockerize your .NET Core application, deploy it to an Azure Kubernetes Service (AKS) cluster, and set up a CI/CD pipeline using Azure DevOps.
This is a simplified example but should give you a good starting point for implementing any MicroService in a .NET Core application. You can extend this further by adding validation, logging, and other features as needed.
Now, to get started with the advanced version of Microservices using clean architecture. You guys can try first understanding the clean architecture with this free course .
Once you have understood the gist of clean architecture, then you guys can get started with Microservices learning series as explained below.
To give you an snapshot of this, below I have pasted the architecture diagram for the same. This is the application, which we are going to build during our learning journey from clean slate.
This is the ultimate course for developers who want to learn how to build scalable, efficient, and robust Microservices using the .Net Core platform along with Docker, Kubernetes, Identity Server 4, Rabbit MQ, Angular 15, GRPC, Istio Service Mesh, SQL Server, MongoDB, PostGreSQL, Dapper, Redis, Ocelot, Nginx, Azure, Helm Charts, and Auto Scale.
In this comprehensive course, you'll gain hands-on experience with Docker and Kubernetes to deploy and manage your Microservices. You'll learn how to integrate Identity Server 4 for secure authentication and authorization, Rabbit MQ for messaging, and GRPC for efficient communication between Microservices.
You'll also learn how to use Istio Service Mesh to manage Microservices traffic, and how to configure and optimize SQL Server, MongoDB, PostGreSQL, and Redis for your Microservices. You'll use Ocelot and Nginx to manage your Microservices API gateway and deploy your Microservices to Azure using Helm Charts.
By the end of this course, you'll have a solid understanding of how to design, develop, and deploy Microservices using the latest industry-standard tools and practices, including auto-scaling.
Who Should Take This Course?
In this comprehensive course, you'll gain invaluable knowledge and hands-on experience as we explore the following exciting topics:
1?? Implementing clean architecture principles in .Net Core Microservices
2?? Building scalable and maintainable Microservices using industry best practices
3?? Applying Containerization techniques with Docker for Microservice deployment
4?? Orchestrating Microservices using Kubernetes for seamless scalability
?? Enroll here? ?? Creating .Net Core Microservices using Clean Architecture
But wait, there's more! Expand your expertise even further with our additional courses tailored to enhance your Microservices journey:
Thanks for Joining me.
Happy Coding
This is an extraordinary endeavor, showcasing the power of cutting-edge technology in the realm of eCommerce. The integration of Microservices with .NET Core and Azure Kubernetes Service is a testament to your forward-thinking approach. Scalability is the name of the game, and your expertise in this domain is truly commendable. Kudos to you and your team for pushing the boundaries of what's possible in the eCommerce landscape! ???? #eCommerce #Innovation #TechnologyAdvancements Regenerate
Cloud Native Enthusiast | ASP.NET | C# | AZURE | SQL | M365 | AGILE | XP | DDD
1 年Nice case study Rahul Sahay , would be a great reference
Solution Architect | 7 Times Microsoft MVP for Developer Technologies From India | .Net Core Expert | C# Corner MVP | Code project MVP | Author | Speaker | Love .Net | Full Stack developer | Open source contributor.
1 年Good work ??