N2O Book

作者: 杰乐思 | 来源:发表于2018-05-05 21:30 被阅读31次

最近在看N2O框架,顺便将其翻译出来,方便有兴趣了解的同学。
由于本人时间有限、水平有限,无法很快译完,持续更新。。


N2O

  • 没有废话
  • 理智的框架
  • 野生的网络

1 N2O: Application Server

1 N2O: 应用服务器

N2O was started as the first Erlang Web Framework that uses WebSocket protocol only. We saved great compatibility with Nitrogen and added many improvements, such as binary page construction, binary data transfer, minimized process spawns, transmission of all events over the WebSocket and work within Cowboy processes. N2O renders pages several times faster than Nitrogen.

N2O最初只是使用WebSocket协议的Erlang Web框架。我们保存了与Nitrogen的良好兼容性,并添加了许多改进,如二进制页面构造、二进制数据传输、最小化进程生成、在WebSocket上传输所有事件以及在Cowboy进程中工作。N2O的呈现速度比Nitrogen快好几倍。

1.1 Wide Coverage

1.1 广泛覆盖

N2O is unusual in that it solves problems in different web development domains and stays small and concise at the same time. Started as a Nitrogen concept of server-side framework it can also build offline client-side applications using the same source code. This became possible with powerful Erlang JavaScript Parse Transform which enables running Erlang on JavaScript platform and brings in Erlang and JavaScript interoperability. You can use Elixir, LFE and Joxa languages for backend development as well.

N2O的不同之处在于,它解决了不同web开发领域的问题,同时保持了小而简洁。始于Nitrogen服务器端框架的概念,至于使用相同的源代码构建离线客户端应用程序。通过强大的Erlang JavaScript解析转换,可以在JavaScript平台上运行Erlang,并引入Erlang和JavaScript互操作性。您还可以使用Elixir、LFE和Joxa语言进行后端开发。

N2O supports DSL and HTML templates. It lets you build JavaScript control elements in Erlang and perform inline rendering with DSL using the same code base for both client and server-side. How to use N2O is up to you. You can build mobile applications using server-side rendering for both HTML and JavaScript thus reducing CPU cycles and saving the battery of a mobile device. Or you can create rich offline desktop applications using Erlang JavaScript compiler.

N2O支持DSL和HTML模板。它允许您在Erlang中构建JavaScript控制元素,并使用DSL对客户端和服务器端使用相同的代码库执行内联渲染。如何使用N2O取决于你。您可以使用服务器端呈现的HTML和JavaScript构建移动应用程序,从而减少CPU周期并节省移动设备的电池。或者,您可以使用Erlang JavaScript编译器创建丰富的离线桌面应用程序。

Why Erlang in Web?

We have benchmarked all the existing modern web frameworks that were built using functional languages and Cowboy was still the winner. The chart below shows raw HTTP performance of functional and C-based languages with concurrent primitives (Go, D and Rust) on a VAIO Z notebook with i7640M processor.

我们已经对所有现有的使用函数式语言构建的现代web框架进行了基准测试,而Cowboy仍然是赢家。下面的图表显示了使用i7640M处理器的VAIO Z笔记本上的基于函数式语言和基于c语言的并发原语(Go、D和Rust)的原始HTTP性能。

图1: web服务器原始性能的盛会

Erlang was built for low latency streaming of binary data in telecom systems. It’s fundamental design goal included high manageability, scalability and extreme concurrency. Thinking of WebSocket channels as binary telecom streams and web pages as user binary sessions helps to get an understanding reasons behind choosing Erlang over other alternatives for web development.

Erlang是为电信系统中二进制数据的低延迟流而构建的。它的基本设计目标包括高可管理性、可伸缩性和极端并发性。将WebSocket通道看作二进制的电信流以及将web页面作为用户二进制会话 有助于理解选择Erlang替代其他web开发的原因。

Using Erlang for web allows you to unleash the full power of tele- com systems for building web-scale, event-driven, message-passing, NoSQL, asynchronous, non-blocking, reliable, highly-available, per- formant, secure, real-time, distributed applications. See Erlang: The Movie II.

使用Erlang为web服务,您可以充分发挥tele- com系统的强大功能,用于构建可伸缩web、事件驱动、消息传递、NoSQL、异步、非阻塞、可靠、高可用性、每个性能、安全、实时、分布式应用程序。

N2O outperforms full Nitrogen stack with only 2X raw HTTP Cowboy performance downgrade thus upgrading rendering performance several times compared to any other functional web framework. And sure it’s faster than raw HTTP performance of Node.js.

N2O的性能优于全Nitrogen栈,只有Cowboy 2倍的原始HTTP性能降级,因此升级渲染性能数倍于任何其他功能的web框架。当然,它比Node.js的原始HTTP性能要快。

1.2 Rich and Lightweight Applications

1.2 丰富和轻量级的应用程序

There are two approaches for designing client/server communication. The first one is called ’data-on-wire’. With this approach only JSON, XML or binary data are transferred over RPC and REST channels. All HTML rendering is performed on the client-side. This is the most suitable approach for building desktop applications. Examples include React, Meteor and ClojureScript. This approach can also be used for building mobile clients.

有两种设计客户机/服务器通信的方法。第一个叫做“数据连接”。通过这种方法,只有JSON、XML或二进制数据通过RPC和REST通道传输。所有HTML渲染都在客户端执行。这是构建桌面应用程序最合适的方法。例子包括React,Meteor和ClojureScript。这种方法也可以用于构建移动客户端。

Another approach is sending pre-rendered parts of pages and JS and then replacing HTML and executing JavaScript on the client-side. This approach is better suited for mobile web development since the client doesn’t have much resources.

另一种方法是发送预先呈现的页面和JS部分,然后替换HTML并在客户端上执行JavaScript。这种方法更适合于移动web开发,因为客户机没有太多的资源可用。

With N2O you can create both types of applications. You can use N2O REST framework for desktop applications based on Cowboy REST API along with DTL templates for initial HTML rendering for mobile applications. You can also use Nitrogen DSL-based approach for modeling parts of pages as widgets and control elements, thanks to Nitrogen rich collection of elements provided by Nitrogen community.

使用N2O,您可以创建两种类型的应用程序。您可以使用基于Cowboy REST API的桌面应用程序的N2O REST框架,以及用于移动应用程序的初始HTML呈现的DTL模板。您还可以使用基于Nitrogen的DSL-based的方法来建模页面的部件和控件元素,这要感谢Nitrogen社区提供的丰富的元素。

In cases when your system is built around Erlang infrastructure, N2O is the best choice for fast web prototyping, bringing simplicity of use and clean codebase. Despite HTML being transfered over the wire, you still have access to all your Erlang services directly.

当您的系统构建在Erlang基础设施周围时,N2O是快速web原型开发的最佳选择,它带来了简单的使用和干净的代码库。尽管HTML被传输到网络上,您仍然可以直接访问所有的Erlang服务。

You can also create offline applications using Erlang JavaScript compiler just the way you would use ClojureScript, Scala.js, Elm, WebSharper or any other similar tool. N2O includes: REST micro frameworks, server-side and client-side rendering engines, WebSocket events streaming, JavaScript generation and JavaScript macro system along with AVZ authorization library (Facebook, Google, Twitter, Github, Microsoft), key-value storages access library KVS and MQS Message Bus client library (gproc, emqttd).

您还可以使用Erlang JavaScript编译器创建脱机应用程序,就像使用ClojureScript、Scala.js, Elm, WebSharper或者其他类似的工具那样。N2O包括:REST微框架、服务器端和客户端呈现引擎、WebSocket事件流、JavaScript生成和JavaScript宏系统以及AVZ授权库(Facebook、谷歌、Twitter、Github、Microsoft)、键值存储库KVS和MQS消息总线客户端库(gproc, emqttd)。

1.3 JSON and BERT

N2O uses JSON and BERT. All messages passed over WebSockets are encoded in native Erlang External Term Format. It is easy to parse it in JavaScript with dec(msg) and it helps to avoid complexity on the server-side. Please refer to http://bert-rpc.org1 for detailed information.

N2O使用JSON和BERT。通过WebSockets传递的所有消息都用native Erlang External Term格式进行编码。用JavaScript和dec(msg)来解析它是很容易的,它有助于避免服务器端的复杂性。详细信息请参见http://bert.rpc.org1

1.4 DSL and Templates

We like Nitrogen for the simple and elegant way it constructs typed HTML with internal DSL. This is analogous to Scala Lift, OCaml Ocsigen and Haskell Blaze approach. It lets you develop reusable control elements and components in the host language.

我们喜欢用Nitrogen作为简单而优雅的方式,用内部的DSL构建HTML。这类似于Scala Lift, OCaml Ocsigen和Haskell Blaze方法。它允许您使用宿主语言开发可重用的控制元素和组件。

Template-based approach (Yesod, ASP, PHP, JSP, Rails, Yaws and ChicagoBoss) requires developers to deal with raw HTML. It allows defining pages in terms of top-level controls, placeholders and panels. N2O also support this approach by proving bindings to DTL and ET template engines.

基于模板的方法(Yesod、ASP、PHP、JSP、Rails、Yaws和ChicagoBoss)要求开发人员处理原始HTML。它允许用顶级控件、占位符和面板来定义页面。N2O也通过向DTL和ET模板引擎提供绑定来支持这种方法。

The main N2O advantage is its suitability for large-scale projects without sacrificing simplicity and comfort of prototyping solutions in fast and dynamic manner. Below is an example of complete Web Chat implementation using WebSockets that shows how Templates, DSL and asynchronous inter-process communication work in N2O.

N2O的主要优势在于其对大型项目的适用性,同时又不牺牲快速和动态的原型解决方案的简单性和舒适性。下面是一个使用WebSockets完成的完整Web聊天实现的示例,它展示了模板、DSL和异步进程间通信在N2O中的工作方式。

Listing 1: chat.erl

  -module(chat).
  -include_lib("nitro/include/nitro.hrl").
  -compile(export_all).
  main() ->
    #dtl{file = "login",
    app = review,
    bindings = [{body, body()}]}.
  body() ->
    [#span{id = title, body = "Your nickname: "},
    #textbox{id = user, body = "Anonymous"},
    #panel{id = history},
    #textbox{id = message},
    #button{id = send, source = [user, message],
        body = "Send",
        postback = chat}].
  event(init) -> wf:reg(room), wf:async("looper", fun loop/1);
  event(chat) -> User = wf:q(user),
    Message = wf:q(message),
    n2o_async:send("looper", {chat, User, Message}).
  loop({chat, User, Message}) ->
    Terms = #panel{body = [
    #span{body = User}, ":", #span{body = Message}]},
    wf:insert_bottom(history, Terms),
    wf:flush(room).

Just try to build the similar functionality with your favorite language/framework and feel the difference! Here are one message bus, one async gen_server worker under supervision, NITRO DSL, DTL template, WebSockets, HTML and JavaScript generation in a simple file that you can put in your N2O application directory tree without restart and manual compilation. Also you can create single-file bundle which is able to run in Windows, Linux and Mac. Moreover this application is ready to run under multiplatform LING Erlang virtual machine.

试着用你最喜欢的语言/框架来构建类似的功能,感受一下区别!在一个简单的文件中,这里有一个消息总线,一个在supervision下的异步gen_server worker,NITRO DSL, DTL模板,WebSockets, HTML和JavaScript生成。你可以将其放入你的N2O应用程序目录树中,而不需要重新启动和手动编译。此外,您还可以创建能够在Windows、Linux和Mac上运行的单文件包,而且这个应用程序可以在多平台的LING Erlang虚拟机中运行。

【译者注】

Maxim Kharchenko 是LING之父,LING是另一种实现Erlang的方式。Erlang是一种编程语言以及一个相关的运行时,由爱立信公司在90年代开发出来的。一个分布式、容错的,并发的,带垃圾回收的编程语言,支持软实时保护,代码热交换、信息传递、响应式编程和自省,所有这些功能听起来,让Erlang对于物联网而言看起来就像一个自然而然的选择,因为它可以用于为新的强大的嵌入式平台写出许多高集成,需要大力维护的代码。

然而,一切事情都是带有附加条件的。就拿Erlang 案例来说,它是虚拟机(VM),这个没人敢说是小的,过去,有大量的工程试着去适应BEAM(主流的Erlang VM)为嵌入式应用程序。他们的成功是有限的,由于标准的VM强加的要求。它必须准备运行它在Linux中,有大量的内存,提供大量的磁盘空间和传统的文件系统。这样的要求不符合大多数较小的微控制器,包括基于mips PIC32单片机家族来自于微芯技术公司(Microchip )。

幸好,车到山前必有路。一家小公司——Cloudozer LLP开发了另一种的Erlang叫做LING。LING可以运行Erlang程序在云终端的裸机或虚拟机。由于简约和独立性,LING可以很容易的移植到许多架构和环境。我们正在进行以下几方面的努力:POSIX(Linux),ARM(Raspberry Pi),以及现在的MIPS(PIC32)。Cloudozer目前正工作在LING静态编译版本(代号为Project L),它不需要虚拟机来运行。这个L编译器发布unikernels(应用程序和操作系统一起编译)就像microkernel,它可以直接运行在裸机。

(待续……)

相关文章

网友评论

      本文标题:N2O Book

      本文链接:https://www.haomeiwen.com/subject/qvgerftx.html