RESTful API与NodeJS在同一服务器上的通信(Communication between RESTful API's on same server with NodeJS)

编程入门 行业动态 更新时间:2024-10-10 15:22:17
RESTful API与NodeJS在同一服务器上的通信(Communication between RESTful API's on same server with NodeJS)

我在一个网站上构建了两套服务(都是在服务器上用NodeJS编写的),两者都使用RESTful方法。 为了模块化,我决定将两个服务分开。 第一项服务涉及网站的产品,第二项服务涉及用户相关的功能。 所以第一个可能有getProducts,deleteProduct等函数......第二个函数有isLoggedIn,register,hasAccessTo等函数......产品模块会多次调用用户模块,以确保拨打电话的人有这样做的特权。

现在我将它们分开的原因是因为在不久的将来我预见到一个单独的产品系列开放,但需要使用与第一个相同的用户系统(甚至共享相同的数据库)。 用户系统将使用跨越整个站点和所有后续产品的数据库

我的问题是这些项目和用户项目之间的沟通。 什么是保持用户模块分离而不会遭受任何显着速度命中的最有效方法。 如果产品API在同一服务器(localhost)上调用用户API,那么是否需要花费相当大的成本,而不是在每个后​​续项目中构建用户API? 有没有更好的方法通过进程间通信来做到这一点? 只是让用户API作为自己的服务运行一个有效的解决方案?

I am building two sets of services on a website (all written in NodeJS on the server), both are using a RESTful approach. For the sake of modularity I decided to make both services separate entities. The first service deals with the products of the site and the second specifically deals with user related functions. So the first might have functions like getProducts, deleteProduct etc... The second would have functions like isLoggedIn, register, hasAccessTo etc... The product module will make several calls to the user module to make sure that the person making the calls has the privilege to do so.

Now the reason I separated them like this, was because in the near future I foresee a separate product range opening up, but will need to use the same user system as the first (even sharing the same database). The user system will use a database that spans the entire site and all subsequent products

My question is about communication between these projects and the users project. What is the most effective way of keeping the users module separate without suffering any significant speed hits. If the product API made a call to the user API on the same server (localhost), is there a signifcant cost to this, versus building the user API into each of the subsequent projects? Is there a better way to do this through interprocess communication maybe? Is simply having the users API run as its own service an effective solution?

最满意答案

如果您在同一台服务器(计算机)上有两个节点,那么您在网络延迟方面的性能并不差,因为它们都在localhost上。 然后,节点将使用rest api进行通信,因此在地下,您将使用节点js套接字。 您可以使用unix套接字而不是http套接字,因为它们更快但是调试最差,所以我建议你不要这样做(但是可以知道替代方案)。

最后,您的系统看起来像一个“演员设计模式”。 乍一看这个设计模式有点难以理解,但如果你想了解更多关于actor模型模式的信息,你可以看看这个:

NodeJS的演员模型https://github.com/benlau/nactor 演员模特说明http://en.wikipedia.org/wiki/Actor_model

If you have two nodes on same server (machine) then you have not bad performance in terms of network latency because both are on localhost. Then, nodes will be communicating using a rest api, so on the underground, you will use node js sockets. You could use unix sockets instead of http sockets because are faster BUT are worst to debug, so I recommend you don't to that (but it's ok know alternatives).

And finally, your system looks like an "actor design pattern". At first glance this design patter is a little difficult to understand but you could have a look at this if you want more info about actor model pattern:

Actor model for NodeJS https://github.com/benlau/nactor Actor model explanation http://en.wikipedia.org/wiki/Actor_model

更多推荐

本文发布于:2023-08-07 17:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器上   通信   在同一   API   RESTful

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!