英文:
node.js golang composite architecture for web application
问题
我目前正在设计一个使用node.js进行基本路由的Web应用程序。应用程序的某些部分需要更多的处理器资源,我想使用golang来处理这些部分。然而,我不确定在这两种语言之间安装和通信的最佳方法。我正在使用Amazon Elastic Beanstalk进行初步测试,因此可以针对该平台提供具体的指导。
基本上可以归结为以下两个问题:
1)如何在Amazon EC2上同时安装node.js和golang的Docker镜像?亚马逊有关于其中一种的指南,但没有关于两者同时安装的指南。
2)将处理器密集型任务从node.js转移到golang代码库的最佳方法是什么(我可以想象使用RPC,或者在本地主机上运行golang,但我对这种类型的事情还不熟悉)?golang的任务可能涉及严重的数值计算或复杂的图搜索。
感谢您的任何指导。
英文:
I am currently architecting a web app that will use node.js for basic routing. Some parts of the app are more processor intensive and I wanted to use golang for those parts. 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.
In essence it boils down to the following 2 questions:
-
How do you install both node.js and a golang docker image on Amazon EC2? Amazon has guides for one or the other, but not both.
-
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)? The golang tasks might be things like serious number crunching or complex graph searches.
Thanks for any guidance.
答案1
得分: 1
-
Go部署起来非常简单。只需在Linux系统上构建它(或使用gox),然后部署二进制文件。(在服务器上运行Go程序时不需要安装Go)
-
在Go和Node.js之间有许多通信选项。以下是几种方法:
- 如果你的工作需要很长时间,让用户等待响应可能不合适。对于后台任务,你可以使用队列(如Redis的rpoplpush或真正的队列,如Kafka或RabbitMQ,或者由于你使用的是Amazon:SQS)。将作业作为JSON对象推送到队列中,然后编写一个Go程序从队列中获取作业,进行处理,然后将最终结果写入某个地方。
- Go有一个jsonrpc库。你可以通过TCP进行通信,在Node中序列化请求,在Go中读取请求,然后在Node中反序列化响应。这是jsonrpc 1.0协议,对于TCP,你只需添加一些消息分帧(在json字符串前加上长度)或使用换行符分隔每个请求/响应。
- 在Go中编写一个标准的HTTP服务,然后从Node.js发起HTTP调用。(PUT/POST/GET)
英文:
-
Go is trivial to deploy. Just build it on a linux box (or use gox) and deploy the binary. (You don't need go installed on the server to run a go program)
-
There are many options for communicating between Go and Node.js. Here are a few:
- 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). 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.
- Go has a jsonrpc library. You can communicate over TCP, serialize a request in Node, read it in Go, then deserialize the response in 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.
- Write a standard HTTP service in Go and just make HTTP calls from NodeJS. (PUT/POST/GET)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论