in my team we use NestJS for our backend. We want to use microservices architecture and I've seen there is a built-in microservices support in NestJS. After read the documentation I've got a few questions about this feature:
- Should I create microservices in the same project or in separate projects?
- Why even use
createMicroservice
overNestFactory.create
? UsingcreateMicroservice
also makes me to use@MessagePattern
and use an emitter to send messages - which is okay if my microservice gets messages using queue (like RabbitMQ), but I can't create REST API in my microservice, so it makes it harder (or impossible) to call NestJS microservice from non-NestJS microservice since it relies on NestJSMessagePattern
.
Thanks ahead.
CodePudding user response:
- It is not necessary but I personally create separate projects within a workspace.
- Calling
createMicroservice
is compulsory because nest needs to know what kind of controller you have and what behavior do you expect. In addition, you should avoid implementing rest API in all services; it should be implemented just in gateway service which is responsible for API layer, and it is a bond between microservices and outworld.
For having a good notion about microservice implementation with NestJS I highly recommend you to take a look to this repo: https://github.com/Denrox/nestjs-microservices-example