Microservices architecture has become a popular approach for building scalable, distributed applications. With .NET Core, developers can create microservices that are lightweight, cross-platform, and easy to containerize. This guide will walk through setting up multiple .NET Core microservices, running them locally using Docker Compose, and enabling service discovery with Consul.
Prerequisites
Before proceeding, ensure you have the following installed on your machine:
- .NET SDK (Download Here)
- Docker and Docker Compose (Download Here)
- Consul (Download Here)
- Postman or any API testing tool
Step 1: Create Multiple .NET Core Microservices
We will create two simple microservices:
- Product Service – Manages product-related operations.
- Order Service – Manages order-related operations.
Creating the Product Service
- Open a terminal and create a new .NET Web API project:
dotnet new webapi -n ProductService
- Navigate into the project directory:
cd ProductService
- Install required NuGet packages:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Design
- Create a
Product
model:public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
- Add a
ProductController
:[Route("api/[controller]")] [ApiController] public class ProductController : ControllerBase { [HttpGet] public IActionResult GetProducts() { return Ok(new List<Product> { new Product { Id = 1, Name = "Laptop", Price = 1000 }, new Product { Id = 2, Name = "Mouse", Price = 50 } }); } }
Creating the Order Service
Repeat similar steps to create an OrderService
project:
dotnet new webapi -n OrderService
cd OrderService
Add an OrderController
:
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase {
[HttpGet]
public IActionResult GetOrders() {
return Ok(new List<object> {
new { Id = 1, Product = "Laptop", Quantity = 2 },
new { Id = 2, Product = "Mouse", Quantity = 5 }
});
}
}
Step 2: Run Them Locally Using Docker Compose
Writing Dockerfiles
For each microservice, create a Dockerfile
in the project root:
ProductService/Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
COPY . .
EXPOSE 5001
ENTRYPOINT ["dotnet", "ProductService.dll"]
OrderService/Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
COPY . .
EXPOSE 5002
ENTRYPOINT ["dotnet", "OrderService.dll"]
Creating docker-compose.yml
Create a docker-compose.yml
file in the root directory:
version: '3.8'
services:
product-service:
build: ./ProductService
ports:
- "5001:5001"
order-service:
build: ./OrderService
ports:
- "5002:5002"
Run the services using:
docker-compose up --build
Step 3: Enable Service Discovery Using Consul
Install and Start Consul
- Download and extract Consul.
- Start Consul in development mode:
consul agent -dev
Register Microservices with Consul
Modify each microservice’s Program.cs
to register with Consul:
ProductService/Program.cs
using Consul;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
using (var client = new ConsulClient()) {
client.Agent.ServiceRegister(new AgentServiceRegistration {
ID = "product-service",
Name = "product-service",
Address = "localhost",
Port = 5001
}).Wait();
}
app.Run();
Repeat the same for OrderService
.
Verify Service Discovery
Visit http://localhost:8500/ui
to check registered services.
Conclusion
By following these steps, you can successfully run a microservices architecture locally using .NET Core, Docker Compose, and Consul for service discovery. This setup provides a solid foundation for developing and testing microservices-based applications before deploying to a production environment.