Denrox / nestjs-microservices-example

A working example of microservice architecture implementation using nestjs framework
883 stars 257 forks source link

sharing code between microservices #7

Closed ais-one closed 3 years ago

ais-one commented 3 years ago

How do we share code if something like the file below is used by multiple microservices?

https://github.com/Denrox/nestjs-microservices-example/blob/master/user/src/services/config/mongo-config.service.ts

Also will npm workspace be used when it is available in node 16 LTS?

Anyway thank you for this very informative example...

Denrox commented 3 years ago

Hi, such files as the mentioned one (mongo-config.service.ts) should never be shared between microservices. Because the DB config for every microservice instance should be separate and different in real life.

Regarding code sharing in general, the microservice approach for sharing large parts of functionality is to separate them into a completely separate microservice and send commands to that microservice via TCP socket, Redis pubsub, or whatever another channel.

Regarding sharing small parts of code, you have several ways for it:

But again sharing code between microservices is a very contradictory practice.