Using Microservices in .Net Core and Docker container
Introduction
During this article, we will learn how to work with the microservice architecture patterns and Docker containers using the .NET Core 3 platform to build a distributed system.
Monolithic vs Microservices Architecture
Before talking about Microservices, we need to understand the difference between traditional application approach and microservices application approach.
Any application is built as a collection of services can be developed, tested, versioned, deployed, and scaled.
For Monolithic applications, Scales is done by cloning the app on multiple servers — VMs.
But for microservices, scales is done by deploying each independently with multiple instances across servers — VMs.
When developing a server-side application you can start it with a modular hexagonal or layered architecture which consists of different types of components or layers:
· Presentation Layer — UI layer, this can be a web or mobile or desktop application.
· Services Layer — responsible for handling HTTP requests and responding with either HTML or JSON/XML (for web services APIs).
· Business logic Layer — the application’s business logic.
· Database access Layer — data access objects responsible for access the database.
Despite having a logically modular architecture, the application is packaged and deployed as a monolith.
Monolithic applications are more of a single complete package, having all the related needed components and services encapsulated in one package.
But for microservices, the idea is simple, it consists to split your application into a set of smaller, interconnected services instead of building a single monolithic application. Each microservice is a small application that has its own hexagonal architecture consisting of business logic along with various adapters. Some microservices would expose a REST, RPC or message-based API and most services consume APIs provided by other services. Other microservices might implement a web UI.
Microservices are deployed independently with their own database per service so the underlying.
Microservices architecture and Patterns
One thing about microservices is there are different things that you can use, those technologies for microservices or not but if we choose building and architecting an Microservices there are many patterns most of them are related or coming from domain driven design. I will talk about some of them after.
Microservices architecture style
Developers consider Microservices as architectural style that promotes the development of complex applications as a suite of small services based on business capabilities and multiple, independent subsystems in the form of autonomous services.
The following picture shows the microservices architecture style.
There are various components in a microservices architecture apart from the microservices themselves.
We start from a client that interact with a server using an authentication using Identity Provider that manages the identity information and provides authentication services within a distributed network. To ensure this interaction, we need a client’s entry point that is API Gateway. It’s a single point of contact from the client which, in turn, returns responses from underlying microservices and sometimes an aggregated response from multiple microservices ,in our schema, we have four services that use the Management that maintains the nodes for the service. And to keep the track of services and service addresses and endpoints, we use Service Discovery.
We have CDN that it a content delivery network to serve static resources. For example, pages and web content in a distributed network and the Static Content is a static resources like pages and web content.
We will talk more in detail about API Gateway:
An API gateway is located between the clients and the services. It acts as a reverse proxy, routing client requests to services. It can also perform various cross-functional tasks such as authentication, SSL termination, and rate limiting. If you do not deploy a gateway, clients must send requests directly to the front-end services.
This creates a coupling between the client and the server. The customer must know how each service has been broken down. these make customer maintenance more difficult and refactoring services.
Now, we need to deploy our services in container, to do that, we will use Docker.
Docker container
Docker is a tool that facilitates the creation, deployment, and execution of applications using a containerization approach.
These containers are lightweight and take less time to boot than traditional servers. These containers also increase performance and reduce costs, while providing appropriate management of resources. Another advantage of Docker is that it is no longer necessary to pre-allocate RAM to each container.
Microservice using ASP.NET Core
And before we start modeling our microservices architecture using the strategies and techniques in this course, we need a problem to solve, and this is where this case study comes in. And our case study is based around booking medical appointments.
Prerequisites Environment
- Visual Studio 2019 or 2017 has built-in support for Docker
- Windows 10 is required for Docker installation.
- .NET Core SDK
- Docker for Windows
- Docker Tools
Creating an Asp.NET Core 3 Application Solution
I started from this old solution, N- Layered application and it include Web APIs. And we need to change that for Microservices.
GitHub: https://github.com/didourebai/Plateforme.AlloTabib-Before
This application has this structure:
- Service Layer: include all APIs used and integrate swagger framework.
- Application and Domain layer: this is the business layer.
- Two web applications: using MVC 4 and AngularJS.
- Infrastructure layer is the Database access layer.
We create a new blank solution to include all API project that are our services:
Or if we can use Docker with an orchestrator Kubernetes, it is ready Core Web application:
Push Docker images to Azure Container Registry
HTTP Client Factory
To ensure a resilient cloud application, we need to implement resilient communication with retires.
To implement that, we use the best library for retries and sql breaker is Poly, an open source. So, you can handle or you can implement HttpClient in your application with Http Factory in .net Core.
Orchestrators in Azure
We can use Kubernetes with Docker or Service Fabric if our microservices is based on plain processes and it is more used for stateful services.
Conclusion
This article was an overview about Microservices with Docker, microservices allow to evolve deploy and scale parts of the application independently but we can’t use this architecture for small application because it is dedicated to distributed software challenges and for scalable and long term evolving applications.