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
What Is Binance Testnet and How Does It Work?
When can I buy a Medigap policy?
Netronline Taxes
Victor Spizzirri Linkedin
Warren Ohio Craigslist
Trevor Goodwin Obituary St Cloud
12 Rue Gotlib 21St Arrondissem*nt
Practical Magic 123Movies
Bustle Daily Horoscope
Rainfall Map Oklahoma
What’s the Difference Between Cash Flow and Profit?
Used Wood Cook Stoves For Sale Craigslist
Everything You Need to Know About Holly by Stephen King
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Leeks — A Dirty Little Secret (Ingredient)
Craiglist Kpr
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Uta Kinesiology Advising
The Blind Showtimes Near Amc Merchants Crossing 16
Stoney's Pizza & Gaming Parlor Danville Menu
67-72 Chevy Truck Parts Craigslist
Www Va Lottery Com Result
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Margaret Shelton Jeopardy Age
10 Best Quotes From Venom (2018)
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
County Cricket Championship, day one - scores, radio commentary & live text
R/Orangetheory
Donald Trump Assassination Gold Coin JD Vance USA Flag President FIGHT CIA FBI • $11.73
Emiri's Adventures
Wbli Playlist
Tenant Vs. Occupant: Is There Really A Difference Between Them?
Oreillys Federal And Evans
AsROck Q1900B ITX und Ramverträglichkeit
Build-A-Team: Putting together the best Cathedral basketball team
Toonily The Carry
World History Kazwire
Frcp 47
Metro Pcs Forest City Iowa
Differential Diagnosis
Foxxequeen
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
What is a lifetime maximum benefit? | healthinsurance.org
New Zero Turn Mowers For Sale Near Me
De boeken van Val McDermid op volgorde
Roller Znen ZN50QT-E
Diesel Technician/Mechanic III - Entry Level - transportation - job employment - craigslist
Epower Raley's
What Responsibilities Are Listed In Duties 2 3 And 4
Ranking 134 college football teams after Week 1, from Georgia to Temple
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.