在 NodeJs 微服务架构中,我应该为每个服务使用一个 package.json 吗? [英] In a NodeJs microservices Architecture, should I use a package.json per service?

查看:50
本文介绍了在 NodeJs 微服务架构中,我应该为每个服务使用一个 package.json 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 NodeJs 中开发微服务架构.我的第一种方法是每个服务一个 package.json.但是,对于所有微服务,访问公共区域(使用日志记录或数据库实用程序)时可能会非常棘手.例如:

I'm currently developing a microservices architecture in NodeJs. My first approach, was a package.json per service. Although, it can be very tricky when accessing to a common area (with logging or database utils), for all microservices. For instance:

common-area >
    logger.js
    package.json - install module typeorm

service1 >
    app.js - use logger.js
    package.json - also install module typeorm

当运行 node app.js(服务 1)时,我们最终加载了 2 个 typeorm 模块,一旦我们进行了两个不同的安装,一个在公共区域(由记录器使用),另一个在 service1.

When running node app.js (Service 1) we end up with 2 typeorm modules loaded, once we made two different installations, one in common area (used by logger) and another in service1.

我是否应该只使用一个 package.json,用于所有微服务,导致只有一个 node_modules 文件夹?

Should I use only one package.json, for all micro-services, resulting in only one node_modules folder?

推荐答案

对于所有微服务,我是否应该只使用一个 package.json,从而导致 >只有一个 node_modules 文件夹?

Should I use only one package.json, for all micro-services, resulting >in only one node_modules folder?

当你在做微服务时,你不应该担心有多个 node_modules 目录,因为微服务的要点之一是能够将它们放在不同的主机或不同的容器中,并且它们将无法共享它们的无论如何都要依赖.

You shouldn't worry about having multiple node_modules directories when you're doing microservices because one of the points of microservices is to be able to put them on separate hosts or in separate containers and they won't be able to share their dependencies anyway.

每个微服务都应该是一个单独的服务.您应该避免因与微服务架构对抗并将它们结合在一起而导致的紧密耦合和可能的抽象泄漏.

Every microservice should be a separate service. You should avoid tight coupling and possible leaky abstractions that can result from fighting against the microservice architecture and joining them together.

当然,微服务并不是唯一在实践中有效的架构,但您想要微服务,那么每个微服务都应该完全独立.否则就不是微服务.

Of course microservices is not the only architecture that works in practice but you want microservices then every microservice should be completely independent. Otherwise it's not microservices.

如果您可以在服务之间共享任何公共代码,则将其放入所有或部分服务需要的模块中.您可以在 npm 上保留私有模块,可以托管私有 npm 注册表,也可以直接从 GitHub 或 GitLab 私有存储库安装该模块.添加托管在 GitHub 上的私有(或公共)模块所需要做的就是运行:

If there is any common code that you can share among the services then put it in a module that would be required by all or some of the services as needed. You can keep private modules on npm, you can host a private npm registry, or you can install that module directly from GitHub or GitLab private repos. All you need to do to add a private (or public) module hosted on GitHub would be to run:

npm install user/repo --save

其中 user 是您在 GitHub 上的用户(或组织)名称,repo 是存储库的名称.请记住,如果要安装这样的模块,如果它在私有仓库中,则该机器上使用的 ssh 密钥必须属于对仓库具有读取权限的用户.您还可以在 GitHub 上使用部署密钥以获得更大的灵活性.

where user is your user (or organization) name on GitHub and repo is the name of the repository. Keep in mind that to install such a module if it's in a private repo the ssh key used on that machine would have to belong to a user that has read access to the repo. You can also use deploy keys on GitHub for more flexibility.

您甚至可以通过创建一个单独的模块来简化在您的服务中加载外部依赖项,该模块包含所有依赖项并将它们一次性公开给您的服务.例如.如果您创建一个名为 dependencies 的模块,如下所示:

You can even simplify loading external dependencies in your services by creating a separate module that includes all of the dependencies and exposes them to your services all at once. E.g. if you create a module called dependencies that looks like this:

module.exports = {
  _: require('lodash'),
  P: require('bluebird'),
  fs: require('mz/fs'),
  // ...
};

然后你可以像这样在你的微服务中使用所有这些模块:

Then you can use all of those modules in your microservices like this:

const { _, P, fs } = require('dependencies');

当您拥有大量小型微服务时,尤其是将它们拆分为多个文件时,这可以大大简化事情.

When you have a large number of small microservices, especially when those are split into multiple files, this can simplify things quite a bit.

这篇关于在 NodeJs 微服务架构中,我应该为每个服务使用一个 package.json 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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