简体   繁体   English

用于Web应用程序的node.js golang复合体系结构

[英]node.js golang composite architecture for web application

I am currently architecting a web app that will use node.js for basic routing. 我目前正在设计一个将使用node.js进行基本路由的Web应用程序。 Some parts of the app are more processor intensive and I wanted to use golang for those parts. 该应用程序的某些部分需要更多的处理器,我想在这些部分使用golang。 However, I'm not sure the best way to install and communicate between the two languages. 但是,我不确定这两种语言之间进行安装和通信的最佳方法。 I'm using Amazon Elastic Beanstalk for initial tests, so any specifics can be targeted for that platform. 我正在使用Amazon Elastic Beanstalk进行初始测试,因此可以针对该平台指定任何具体信息。

In essence it boils down to the following 2 questions: 从本质上讲,它可以归结为以下两个问题:

1) How do you install both node.js and a golang docker image on Amazon EC2? 1)如何在Amazon EC2上同时安装node.js和golang docker映像? Amazon has guides for one or the other, but not both. 亚马逊有一个或另一个指南,但没有两个。

2) What is the best way to offload processor intensive tasks from node.js to a golang codebase (I could imaging RPC, or just running golang on some localhost port, but I'm new to this type of thing)? 2)将处理器密集型任务从node.js卸载到golang代码库的最佳方法是什么(我可以对RPC进行映像,或者仅在某些localhost端口上运行golang,但是我对这种事情不熟悉)? The golang tasks might be things like serious number crunching or complex graph searches. golang任务可能是诸如严重的数字运算或复杂的图形搜索之类的事情。

Thanks for any guidance. 感谢您的指导。

  1. Go is trivial to deploy. Go部署很简单。 Just build it on a linux box (or use gox) and deploy the binary. 只需在Linux机器上构建它(或使用gox)并部署二进制文件即可。 (You don't need go installed on the server to run a go program) (您无需在服务器上安装go即可运行go程序)
  2. There are many options for communicating between Go and Node.js. Go和Node.js之间有很多通信选项。 Here are a few: 这里有一些:

    1. If the work you are doing takes a long time it may not be appropriate to have the user wait for a response. 如果您正在进行的工作需要很长时间,那么让用户等待响应可能是不合适的。 For background tasks you can use a queue (like Redis' rpoplpush or a real queue like Kafka or RabbitMQ, or since you're using Amazon: SQS ). 对于后台任务,您可以使用队列(例如Redis的rpoplpush或真实的队列,例如Kafka或RabbitMQ,或者因为您使用的是Amazon: SQS )。 Push your job as a JSON object onto the queue, then write a Go program that pulls from the queue, does its processing and then writes the final result somewhere. 将您的作业作为JSON对象推送到队列中,然后编写一个Go程序,该程序从队列中拉出,进行处理,然后将最终结果写入某个位置。
    2. Go has a jsonrpc library . Go有一个jsonrpc库 You can communicate over TCP, serialize a request in Node, read it in Go, then deserialize the response in Node. 您可以通过TCP进行通信,在Node中序列化一个请求,在Go中读取它,然后在Node中反序列化响应。 It's the jsonrpc 1.0 protocol and for TCP all you have to do is add some message framing (prefix your json string with a length) or just newline separate each request / response. 这是jsonrpc 1.0协议,对于TCP,您所要做的就是添加一些消息框架(以长度作为json字符串的前缀)或仅用换行符分隔每个请求/响应。
    3. Write a standard HTTP service in Go and just make HTTP calls from NodeJS. 在Go中编写标准的HTTP服务,然后从NodeJS进行HTTP调用。 (PUT/POST/GET) (PUT / POST / GET)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM