node.js golang composite architecture for web application

huangapple go评论97阅读模式
英文:

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:

  1. 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.

  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)? The golang tasks might be things like serious number crunching or complex graph searches.

Thanks for any guidance.

答案1

得分: 1

  1. Go部署起来非常简单。只需在Linux系统上构建它(或使用gox),然后部署二进制文件。(在服务器上运行Go程序时不需要安装Go)

  2. 在Go和Node.js之间有许多通信选项。以下是几种方法:

    1. 如果你的工作需要很长时间,让用户等待响应可能不合适。对于后台任务,你可以使用队列(如Redis的rpoplpush或真正的队列,如Kafka或RabbitMQ,或者由于你使用的是Amazon:SQS)。将作业作为JSON对象推送到队列中,然后编写一个Go程序从队列中获取作业,进行处理,然后将最终结果写入某个地方。
    2. Go有一个jsonrpc库。你可以通过TCP进行通信,在Node中序列化请求,在Go中读取请求,然后在Node中反序列化响应。这是jsonrpc 1.0协议,对于TCP,你只需添加一些消息分帧(在json字符串前加上长度)或使用换行符分隔每个请求/响应。
    3. 在Go中编写一个标准的HTTP服务,然后从Node.js发起HTTP调用。(PUT/POST/GET)
英文:
  1. 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)

  2. There are many options for communicating between Go and 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). 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.
    2. 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.
    3. Write a standard HTTP service in Go and just make HTTP calls from NodeJS. (PUT/POST/GET)

huangapple
  • 本文由 发表于 2015年5月3日 00:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/30004689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定