October 6, 2023
NestJS Microservices with TCP.
A microservice is an application that uses a transport layer other than HTTP. In this example microservices Communicate with each other using TCP.
Table of contents:
- General Introduction — What is Microservice
- Why Microservice
- NestJS Microservices Overview
- Microservice Client
- User Microservice
- Conclusion
General Introduction — What is Microservice:
- The microservice architecture enables an organization to deliver large, complex applications rapidly, frequently, reliably and sustainably.
- In a microservice, each software application feature is separated from the other, in most cases with their respective servers and databases.
Why Microservice:
- Microservice offers flexibility and performance benefits that can’t be achieved with a monolithic application.
- The event-driven architecture of Node.js makes it a perfect choice for microservices, being fast, highly scalable, and easy to maintain.
- Independently deployable
- Loosely coupled
- supports distributed development
- better scalability
- faster development cycles
- isolated services are easier to debug and maintain
NestJS Microservices Overview:
- It supports different approaches such asRequest ResponseandEvent-Based.
- we will be looking at Nest JS Microservice usingRequest Response approach with TCP Transporter.
- l which will be responsible for operations and we will usePacket Senderfor testing purposes, an open-source application that allows us to send network packets that supportTCP.
- Asynchronous patternwithTCPpackets which we will communicate with our microservice, and hence the choice of Nest.JS as it has many built-in features making it easier for us to create a microservices architecture.
First, you need to install Nest JS, Runnpm i -g @nestjs/cli
Microservice Client:
- Runnest new nest_producerto create the Client service. Relocate the project directory and run the project.
start:devfor watch the changes in your files. To create the app as microservice, we need to install the required package:
npm i — save @nestjs/microservices
Go tomain.tsfile and re-write the code as given below:
- NestFactoryexposes a few static methods that allow creating an application instance. The create() method returns an application object, which fulfills the INestApplication interface.
Go toapp.module.tsand replace the code as given below:
Runnest g resource usercommand generates all the NestJS building blocks (module, service, controller class, entity class, DTO classes ,testing (.spec) files).
- Clients Module exposes the static register() method.
- This method takes an argument which is an array of objects representing microservices.
- Each such object has a name property as well as a microservice-specific options object.
- In this example, Register the user details (name, email ,password ,phone number)
- we can send a message using the send()method of the Client Proxy instance. The send() method takes the message pattern and the actual data or the payload as input.
- Client service send Request with Message pattern{cmd:’register_user’}to User service.
User Microservice:
Runnest new user_service to create the User service.Relocate the project directory and run the project.
we need to install the required package:
npm i — save @nestjs/microservices
Go tomain.tsfile and re-write the code as given below:
- The INest Application instance can be connected with INest Microserviceinstances through theconnect Microservice()
- To connect multiple microservice instances, issue the call to connect Microservice() for each microservice.
- This method takes an argument which is an objects representing microservices communication transport and options.
Go toapp.module.tsand replace the code as given below:
- Runnest g resource user command generates all the NestJS building blocks. Runnpm install — save typeorm
- In this example , we choose the MySQL database and Create database name nestdb.
Now go tosrcand Createdata-source.tsfile to add code as given below:
Go to User Module folder intouser.module.tsand write the code as given below:
Add the given code User Entity folder intouser.entity.tsfile.
Go touser.controller.tsfile and re-write the code as given below:
Add given below code intouser.service.ts
Open your postman and send action withlocalhost:5000/register_user
Conclusion:
we have learnt how to create aNestJS Microservices with TCPusing Request Response approach.NestJS makes it possible to create outstanding, well-organized, and lightweight microservices. The framework allows you to create extensible software solutions where there is no strong coupling between the components.