NestJS Microservices with TCP (2024)

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.

NestJS Microservices with TCP (1)

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 with TCP (2)

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.

NestJS Microservices with TCP (3)

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:

NestJS Microservices with TCP (4)

  • 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:

NestJS Microservices with TCP (5)

Runnest g resource usercommand generates all the NestJS building blocks (module, service, controller class, entity class, DTO classes ,testing (.spec) files).

NestJS Microservices with TCP (6)

  • 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.

NestJS Microservices with TCP (7)

  • 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.

NestJS Microservices with TCP (8)

we need to install the required package:

npm i — save @nestjs/microservices

Go tomain.tsfile and re-write the code as given below:

NestJS Microservices with TCP (9)

  • 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.

NestJS Microservices with TCP (10)

Go toapp.module.tsand replace the code as given below:

NestJS Microservices with TCP (11)

  • 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:

NestJS Microservices with TCP (12)

Go to User Module folder intouser.module.tsand write the code as given below:

NestJS Microservices with TCP (13)

Add the given code User Entity folder intouser.entity.tsfile.

NestJS Microservices with TCP (14)

Go touser.controller.tsfile and re-write the code as given below:

NestJS Microservices with TCP (15)

Add given below code intouser.service.ts

NestJS Microservices with TCP (16)

Open your postman and send action withlocalhost:5000/register_user

NestJS Microservices with TCP (17)

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.

NestJS Microservices with TCP (2024)
Top Articles
Here's How Much to Keep in Stocks, Bonds and Cash in Retirement
FAQs | Futures Brokers | R.J. O'Brien & Associates LLC
Antisis City/Antisis City Gym
Skylar Vox Bra Size
How Much Does Dr Pol Charge To Deliver A Calf
Comcast Xfinity Outage in Kipton, Ohio
About Goodwill – Goodwill NY/NJ
Craigslist Labor Gigs Albuquerque
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
Theycallmemissblue
Kinkos Whittier
Flights To Frankfort Kentucky
Viha Email Login
NHS England » Winter and H2 priorities
Hennens Chattanooga Dress Code
Indiana Wesleyan Transcripts
Icivics The Electoral Process Answer Key
Heart and Vascular Clinic in Monticello - North Memorial Health
Morristown Daily Record Obituary
Trivago Myrtle Beach Hotels
eugene bicycles - craigslist
Hctc Speed Test
Discord Nuker Bot Invite
Urban Dictionary Fov
Nearest Ups Ground Drop Off
2004 Honda Odyssey Firing Order
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
Otis Inmate Locator
Used Safari Condo Alto R1723 For Sale
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
Helloid Worthington Login
Play 1v1 LOL 66 EZ → UNBLOCKED on 66games.io
Tamil Play.com
Marine Forecast Sandy Hook To Manasquan Inlet
Edict Of Force Poe
دانلود سریال خاندان اژدها دیجی موویز
Myql Loan Login
Mvnt Merchant Services
Stanley Steemer Johnson City Tn
Xxn Abbreviation List 2023
Best GoMovies Alternatives
Tgirls Philly
فیلم گارد ساحلی زیرنویس فارسی بدون سانسور تاینی موویز
Gabrielle Abbate Obituary
15 Best Places to Visit in the Northeast During Summer
Swsnj Warehousing Inc
Pas Bcbs Prefix
Enter The Gungeon Gunther
El Patron Menu Bardstown Ky
The 5 Types of Intimacy Every Healthy Relationship Needs | All Points North
Where Is Darla-Jean Stanton Now
Divisadero Florist
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6429

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.