In this article, we are going to create two microservices using Ocelot API Gateway using Client Application, which is designed in Angular 14.
Agenda
- Introduction of Microservice
- Introduction of Ocelot API Gateway
- Step-by-step implementation of microservices
- Implementation of Ocelot API Gateway
- Client Application using Angular 14
Prerequisites:
- Understanding of .NET Core API, C#, and Angular
- Visual Studio 2022
- VS Code
- Node Js
- Angular 14
- .NET Core 6 SDK
Introduction of Microservice
There are two types of architecture for design applications: the first is monolithic, and the second one is microservices.
1. Monolithic Architecture
- In a Monolithic Application, all components are tightly coupled to each other and run using a single process.
- It’s very difficult to make some changes in the future when monolithic architecture is used, because the components of the application will be tightly coupled to each other. When trying to modify or introduce new things into the existing component, this may impact all applications.
- Suppose we have an E-commerce application and in that application, we used multiple components, like Login, Posts, and News Feed. In that case, when we introduce or change something in one of the components, it will impact the application because the application is tightly coupled and interconnected to each other using a single service.
- If we have deployed this type of application on the cloud, and in the future we need to add something into the Posts Component, then we will be required to stop the whole application. This will impact performance and delivery services' efficiency. Also, it’s difficult to manage.
2. Microservice Architecture
- In the Microservice Architecture, we create application components that will work independently, without being tightly coupled to each other, in their own environments.
- Also, we can easily communicate with each other with the help of API Gateways and other such programs.
- So, basically, when we use microservices, they will run their own environment, and they will not impact one another when we change something in one of the services.
- Also, this is easy to manage while the application is in a development phase. Each developer will work independently on each service, and because of that, it is easy to manage and integrate components.
Introduction of Ocelot API Gateway
- Ocelot is the API Gateway for the .NET Platform and which is work as the entry point of our application when we use microservice architecture.
- The following diagram is from Microsoft Technical Documentation.
- API gateway is present between the client and microservices and acts as middleware for communication between different microservices as shown in the above diagram.
Step-by-step implementation of microservices
Let’s start.
Step 1
Create a Blank Empty solution using a visual studio.
Step 2
Configure your project.
Step 3
Project Structure:
Step 4
Create a new folder inside the solution and name it Microservices. Next, create ProductAPI and UserAPI projects inside that.
Step 5
Configure ProductAPI project.
Step 6
Provide additional information related to project.
Step 7
Next, create the UserAPI project inside the Microservices folder and follow the same steps as we used to create ProductAPI.
Step 8
Create an Ocelot API Gateway project outside the Microservices folder.
Let’s start the implementation of ProductAPI
Project Structure
Step 1
Install the following NuGet packages.
Step 2
Create Product Class inside the Model folder.
Step 3
Next, Create DBContextClass inside the Data folder.
Step 4
After that, create IProductService and ProductService inside the services folder.
IProductService
ProductService
Step 5
Configure the connection string inside the appsetting.json file.
(Note: I used the same database for demo purposes in both Product and User Microservice but in a real-time scenario you can use the separate database for each microservice as per microservice guidelines and protocols.)
Step 6
Later on, configure a few services inside Program.cs class.
Step 7
Next, Create a ProductController.cs.
Step 8
Finally, execute the following command in the package manager console to create migration and create the database.
Implementation of UserAPI service
Project Structure:
Step 1
Install the following NuGet packages:
Step 2
Create a User Class inside the Model folder.
Step 3
Next, Create DBContextClass inside the Data folder.
Step 4
After that, create IUserService and UserService inside the services folder.
IUserService
UserService
Step 5
Configure the connection string inside the appsetting.json file.
(Note: I used the same database for demo purposes in both Product and User Microservice but in a real-time scenario you can use the separate database for each microservice as per microservice guidelines and protocols.)
Step 6
Later on, configure a few services inside Program.cs class.
Step 7
Next, Create a UserController.cs.
Step 8
Finally, execute the following command in the package manager console to create migration, and create the database.
Implementation of Ocelot API Gateway
Step 1
Install Ocelot NuGet Package.
Step 2
Project Structure:
Step 3
Create an ocelot.json file for routing.
- The Ocelot file has two sections: one is Global Configuration, which acts as an entry point of our application; and the other is Routes, which is used to define routes of our microservices.
- UpstreamPathTemplate is used to receive client requests and redirects them to the particular microservice.
- UpstreamHttpMethod is used to define HTTP attributes, which helps to the gateway to getting the type of request.
- DownstreamTemplatePath is the microservice endpoint that takes the request from UpstreamPathTemplate.
- DownstreamScheme defines the scheme of a request.
- DownstreamHostAndPorts defines the hostname and port number of microservices that are present inside the lauchSetting.json file.
Step 4
Configure the ocelot.json file inside Program.cs class and register services related to that. Then, apply CORS policy for client communication, which we are going to create using Angular.
Step 5
Right-click on the solution and open the property. Then, apply to start on all three projects that we have created.
Step 6
Run the project.
This is all about backend application using microservices, let’s start the client application.
Client Application using Angular 14
Step 1
Create a new angular project.
Step 2
We use bootstrap in this application. So, use the following command to install bootstrap.
Next, add the bootstrap script inside the angular.json file inside the scripts and styles section.
Step 3
Install Toaster module for pop-up and notification.
Then, add the toaster in the styles section inside the angular.json file.
Step 4
Create Product and User class inside the Model folder.
Product.ts
User.ts
Step 5
Next, create three components using the following command:
Step 6
After that, create a Product angular service.
Step 7
Next, create Users angular service.
Step 8
Create app-routing.module.ts to define the routes of the angular component.
Step 9
Configure and define all modules in the app.module.ts.
Step 10
Create a config folder inside the assets folder, and then create a config.json file to define the backend server API URL.
Step 11
App Component:
app.component.ts
app.component.html
Homepage Component
homepage.component.ts
homepage.component.html
Product Component
product.component.ts
prouct.component.html
User Component
user.component.ts
user.component.html
Step 12
Finally, run the application
Homepage Component
Product Component
User Component
Conclusion
In this article we discussed working of microservice architecture and ocelot API gateway using .NET Core 6 API, and went over step-by-step implementation of client applications using Angular 14.
Happy coding!
No comments:
Post a Comment