在两个NodeJS微服务之间共享自定义代码 [英] Sharing custom code between two NodeJS microservices

查看:536
本文介绍了在两个NodeJS微服务之间共享自定义代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为该应用创建网络应用和micorservice,并且都需要使用Sequelize创建相同的数据库模型。使用NodeJS处理这个任务的方法是什么?



我正在考虑创建我的数据库模型的单独模块并将其保存在私有git中,并将此私有git添加到我的网站应用程序和微服务作为npm依赖。

但我想知道是正确的方法还是什么是正确的方式来分隔几个微服务之间的共享私有模块,在我的情况下,它是DB楷模?
我应该使用DRY吗?



预先感谢!

解决方案

没有关于微服务的规则,代码不应该被重用。事实上,说明DRY在微服务方面通常是错误的,这很危险。相反,您应该提出以下问题:共享代码是否是一个单独的模块,具有专门的目的来纠正单独管理的生命周期并发布?如果是,您应该重用它作为一个单独发布的模块 - 类似于你将如何重用任何第三方库。这可以确保您在共享代码和微服务之间没有强大的耦合,因为每个微服务团队都可以自行决定是否希望保留特定版本的模块或升级到更新版本。你想避免的是一个依赖,它迫使你在重用库改变时改变你的微服务(避免连锁效应)。

还有一件事 - 因为您提到它是与数据库相关的模式,您必须提出另一个问题:模式是否会在单独的数据库中重用或他们最终会引用同一物理数据库吗?。如果他们最终会使用共享数据库,那么您实际上将两个微服务紧密结合在一起,因此他们可能应该被认为是同一服务的一部分,而不是分开。



我觉得这和我所能说的一样多,不知道更多关于你的服务和目标的信息。

I am creating web app and micorservice for that app, and both need to have same DB model created with Sequelize. What is approach for handling this task with NodeJS?

I am thinking about creating separate module of my DB models and save it in private git and add this private git to my web app and microservice as npm dependency.

But I am wondering is it right approach or what is right way to separate shared private module between few microservices, in my case it is DB models? Should I go with DRY or not?

Thanks in advance!

解决方案

There is no rule about Microservices that code should not reused. In fact stating that DRY is wrong in general with Microservices is dangerous. Rather you should ask the following questions: Will the shared code be a separate module with a dedicated purpose that rectifies a separately managed lifecycle and releases? If yes you should definitely go for reusing it as a separately released module - Similar to how you would reuse any third party library. This ensures that you do not have strong coupling between the shared code and your Microservices, because each Microservice team can decide for themselves if they want to stay with a specific release version of the module or upgrade to a newer version. What you want to avoid is a dependency that forces you to change your microservices when the reused library changes (avoid the ripple effect).

One more thing - Because you are mentioning it being DB related schemas you have to ask another question: Will the schemas being reused in separate DBs or will they refer to the same physical DB in the end?. If they will end up using a shared DB you are effectively tightly coupling the two Microservices and thus they should probably be considered to be part of the same service rather than separate.

I feel this is as much as I can say without knowing more about your services and goals.

这篇关于在两个NodeJS微服务之间共享自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆