A comprehensive code refactoring tutorial demonstrating the migration of microservices from TCP to gRPC within a NestJS ecosystem. The process covers updating service modules, controllers, and Docker configurations for reservations, payments, auth, and notifications services, concluding with a verification test using Postman.
Length 21:00
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #grpc #nestjs #microservices #refactoring #docker #backend #typescript #protobuf #web development #api
0:00 All right, so we refactored our JWT Auth Guard. 0:03 Let's do the same thing for our reservations service now. 0:07 So we'll go into our reservation service, and this is gonna follow a very 0:11 similar pattern where currently we're calling out to the payment 0:15 service. We're gonna follow the same pattern we did 0:18 in the JWT Auth 0:19 Guard to properly inject the payment 0:22 service in this reservation service. 0:26 So let's start off by making sure we implement 0:30 onModuleInit, 0:33 and then we'll declare this private 0:36 paymentService 0:38 of type PaymentsServiceClient. 0:43 And now let's go ahead and change the injection token to be 0:46 paymentsServiceName. 0:48 I'll change the name to be client and the type to be 0:51 ClientGRPC from NestJS Microservices. 0:57 Now, just as we've done before, we'll implement onModuleInit 1:01 and set this.paymentService equal to 1:05 this.client.getService. I'll pass in 1:09 the paymentsServiceClient, 1:11 and we'll pass in the paymentsServiceName, which we can 1:15 see is paymentService, just a simple string which matches 1:19 our proto definition. 1:22 So just as we did in the JWT Auth Guard, instead of calling .send, 1:26 we're actually gonna call the method on the payment service, which will be 1:30 createCharge. And we can get rid of the first argument. 1:34 We'll simply just include all of the arguments that the 1:38 createCharge method expects. 1:40 Lastly, I'll go ahead and remove all of the unused imports, 1:44 and now our reservation service is properly refactored to utilize 1:48 gRPC. 1:51 All right, so it's time to actually configure our 1:53 reservations module 1:55 to set up gRPC and change from 1:59 TCP to gRPC in our clients module. 2:03 Let's go ahead and firstly change our transport to now 2:06 be Transport.gRPC, 2:11 and then our options are going to change. 2:13 So instead of a host import, we now need to specify the 2:16 package that we wanna connect to in our gRPC proto 2:20 definition. So this is where we're gonna use the
The rest of the transcript is for members.
Sign in