Note:
This project has to be compared to the LABORATORY-NX-NATS
https://github.com/JustalK/LABORATORY-NX-NATS
This project has been created using the libraries NX for creating a monorepository of microservices. Each microservices has been built with the CLI of NX as a Nest.js microservice. They communicate with the client using the TCP protocol and the message pattern of Nest.js.
I explain with all the details how I build the project and my way of working.
There are 3 apps:
- client: A nest.js client
- microservice1: A nest.js microservice on port 3001
- microservice2: A nest.js microservice on port 3002
When the api call one of the endpoint of the api, the service of the client
is called. Inside the method, a message is sent to the microservices through the transporter TCP. Once the message is received, a response is sent back to the client as a response for the endpoint.
The communication is using the decentralized TCP transporter. A simple image for illustrating what is a decentralized communication:
In the app.module.ts
, we register the two microservices using the same transporter and port used in the microservices.
ClientsModule.register([
{
name: 'MICROSERVICE1',
transport: Transport.TCP,
options: {
port: 3001,
}
},
{
name: 'MICROSERVICE2',
transport: Transport.TCP,
options: {
port: 3002,
}
},
])
We inject our microservices using the ClientProxy inside our service app.service.ts
with their respective name.
constructor(
@Inject('MICROSERVICE1') private client1: ClientProxy,
@Inject('MICROSERVICE2') private client2: ClientProxy
){}
For sending a message to a particular microservice, we can next using our injected variable.
// Example: sending a message 'greeting' with 'Micro1' as a parameter to microservice1
return this.client1.send({cmd: 'greeting'}, 'Micro1')
Register, the app has a Nest.js microservice:
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
transport: Transport.TCP,
options: {
port: 3001
}
},
);
In the app.controler.ts
, we use the decorator MessagePattern
to indicate that a controller is waiting for a message from the client
@MessagePattern({cmd: 'greeting'})
getGreetingMessage(name: string): string {
return `[MICROSERVICE 1] Hello ${name}`;
}
$ nx generate @nrwl/nest:application <Name of app/microservice>
$ nx generate @nrwl/nest:service <Name of service> --project <Name of Project> --directory app
$ nx dep-graph
$ nx serve <name of the app>
# Example
$ nx serve client
$ nx run-many --parallel --target=serve --projects=client,microservice1,microservice2