Erlang 风格的 Node.Js?
对于 Node.Js 和 Erlang 来说,我完全是个菜鸟。 但是是否可以构建一个模拟 Erlang 行为的 Node.js 应用程序呢?
例如,您可以在分布式 Node.js 服务器公园中传递 json 消息,甚至可以将新代码传递到这些服务器而不需要离线,就像 erlang 一样。
如果您有一个在收到消息时激活的消息处理程序回调,则该消息处理程序可以检查该消息是否是代码更新消息,从而用新代码替换其自身(当前处理程序)。
因此,应该可以让 Node.Js 服务器无需停机进行代码更新,而无需太多麻烦,对吧?
I am a complete noob when it comes to both Node.Js and Erlang.
But wouldn't it be possible to build a Node.js app that emulates Erlang behavior?
e.g. you pass json messages across an distributed node.js server park and even pass new code to those servers w/o going offline, just like erlang.
If you have a message handler callback that is activated when a message is received, then this message handler could check if the message is a code update message and thus replace itself(the current handler) with the new code.
So it should be possible to have Node.Js servers with no downtime for code updates w/o too much fuss, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设使用 脚本模块 您可以执行 javascript 而无需重新加载服务器。
主管
但话又说回来,当它检测到文件更改时,它会重新加载(非常短的离线时间)。
I assume With script module you could execute javascript without reloading the server.
supervisor
But then again it will reload(very short time offline) when it detects file changes.
不完全正确。
好吧,首先你显然需要进行验证等,这不应该是一个大问题。第一个小问题来自 JSON,它不允许其中包含任何 JS 代码/函数,您可以通过以字符串形式发送数据来解决这个问题。
下一个问题是,当您想要替换函数/方法时,您需要确保它保留其范围,以便新编译的函数可以访问相同的内容。
借助一些黑暗的
eval
魔法,这是当然可能,但不要指望它会像Erlang中那样自然:输出
这不保证在100中工作占所有案例和范围的百分比。另外,语法很糟糕,所以我建议如果你想要热插拔,就继续使用 Erlang。
请记住:用正确的工具完成正确的工作。
更新
在不久的将来不会有比这更好的事情了,请参阅:
https://github.com/ry/node/issues /issue/46#issue/46/comment/610779
Not completely right.
OK, first you obviously need to have validation etc. in place, that shouldn't be a big problem. The first small problem arises from JSON, which does not allow for any JS code/functions in it, well you can work around that by sending the data as a string.
Next problem, when you want to replace function/method you need to make sure that it keeps it's scope, so that the newly compiled functions has access to the same things.
With some dark
eval
magic this is certainly possible, but don't expect it to be anywhere near as natural as it's in Erlang:Output
This is not guaranteed to work in 100% of all cases and scopes. Also, the syntax is horrible, so I'd suggest if you want hot swapping, stay with Erlang.
Remember: Right tool for the right job.
Update
There won't be anything better than that in the near future see:
https://github.com/ry/node/issues/issue/46#issue/46/comment/610779
这是一个比较使用 Erlang 和 Node.js 的体验的博客:
http://blog.mysyncpad.com/post/2073441622/node-js-vs-erlang-syncpads-experience
这是另一个比较,故意不比较速度:
< a href="http://jlouisramblings.blogspot.com/2010/12/differences- Between-nodejs-and-erlang_14.html" rel="nofollow">http://jlouisramblings.blogspot.com/2010/12/ -nodejs-和-erlang_14.html之间的差异
Here is a blog comparing experiences using Erlang and Node.js:
http://blog.mysyncpad.com/post/2073441622/node-js-vs-erlang-syncpads-experience
Here is another comparison which purposefully does not compare speed as such:
http://jlouisramblings.blogspot.com/2010/12/differences-between-nodejs-and-erlang_14.html
没有足够的点来进行内联评论,但我想回复 Ivo Wetzel 在 rvirding 帖子中的评论。 mysyncpad 上有一个更新的博客,其中作者使用了nodejs 和v8 开发人员特别推荐的nodejs 版本。
http://blog.mysyncpad.com/post/2143658273/syncpad -node-js-服务器重访
Don't have enough points to comment inline, but I wanted to respond to Ivo Wetzel's comment above at rvirding's post. There is an updated blog on mysyncpad where the author uses the version of nodejs specifically recommended by the nodejs and v8 developers.
http://blog.mysyncpad.com/post/2143658273/syncpad-node-js-server-revisited