如何将 Java 与 nodejs 集成以处理 CPU 繁重的任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18018236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:49:31  来源:igfitidea点击:

How to integrate Java with nodejs for handling CPU-heavy tasks?

javanode.jsintegration

提问by Gevorg Soghomonyan

I am trying to pick a right web technology both for I/O heavy and CPU heavy tasks. NodeJs is perfect for handling large load and it also can be scaled out. However, I am stuck with the cpu heavy part. Is it possible to integrate another technology (e.g. Java) into node, so that I will have it running my algorithms in other threads and then use the results again in node. Is there any existing solution? Any other suggestions will be very good.

我正在尝试为 I/O 繁重和 CPU 繁重的任务选择合适的 Web 技术。NodeJs 非常适合处理大负载,也可以横向扩展。但是,我坚持使用 CPU 重的部分。是否可以将另一种技术(例如 Java)集成到 node 中,以便我让它在其他线程中运行我的算法,然后在 node.js 中再次使用结果。有没有现有的解决方案?任何其他建议都会非常好。

采纳答案by JustDanyul

You can intergrate NodeJS with Java using node-java.

您可以使用node-java 将NodeJS 与 Java 集成。

回答by irbull

As mentioned in a previous answer, you can use node-java which is an npm module that talks to Java. You can also use J2V8which wraps Node.js as a Java library and provides a Node.js API in Java.

正如之前的回答中提到的,您可以使用 node-java,它是一个与 Java 对话的 npm 模块。您还可以使用J2V8,它将 Node.js 包装为 Java 库并在 Java 中提供 Node.js API。

回答by Gevorg Soghomonyan

The answer is lambda architecture.

答案是lambda 架构

NodeJs is nice by itself - handling fast queries in a lightweight manner, not doing any extra computations on data.

NodeJs 本身就很好——以轻量级的方式处理快速查询,而不是对数据做任何额外的计算。

The CPU heavy tasks can be easily delegated to specialized components based on JVM (well, the most famous ones are on JVM). This is nicely implemented by using message brokers and microservices.

CPU 繁重的任务可以很容易地委托给基于 JVM 的专门组件(嗯,最著名的组件是在 JVM 上)。这是通过使用消息代理和微服务很好地实现的。

An event-based architecture, where nodejs can be hooked up to databases like Cassandra or Mongodb and cluster computing frameworks like Apache Spark (not necessarily, though, it depends on the problem) to handle the cpu-heavy parts of the system. And lightweight containers add an icing to the cake by providing nice isolated runtime environments for each of the components to live in.

一种基于事件的架构,其中 nodejs 可以连接到像 Cassandra 或 Mongodb 这样的数据库和像 Apache Spark 这样的集群计算框架(不一定,但这取决于问题)来处理系统的 CPU 密集型部分。轻量级容器通过为每个组件提供良好的隔离运行时环境来锦上添花。

That's my conclusion so far regarding this question. I think the suggestions above sort of eliminate the need to wrap node under java or other JVM based solution for cpu-heavy tasks.

到目前为止,这是我对这个问题的结论。我认为上述建议消除了在 java 或其他基于 JVM 的解决方案下为 cpu-heavy 任务包装节点的需要。

回答by Luke the Geek

NodeJS is based on the v8 javascript engine which is written in c++.

NodeJS 基于用 C++ 编写的 v8 javascript 引擎。

It is therefore possible to write fully native addons in c++ for NodeJS. Check out some of these resources:

因此,可以用 C++ 为 NodeJS 编写完全原生的插件。查看其中一些资源: