当前位置: 首页 > 工具软件 > Deft JS > 使用案例 >

jvm虚拟机驱动_JVM的异步,事件驱动的Web服务器:Deft和Loft

梁丘逸仙
2023-12-01

jvm虚拟机驱动

Asynchronous, event-driven architectures have been gaining a lot of attention lately, mostly with respect to JavaScript and Node.js. Deft and Loft are two solutions that bring "asynchronous purity" to the JVM.

异步,事件驱动的体系结构最近引起了很多关注,主要是在JavaScript和Node.js方面DeftLoft是两个为JVM带来“异步纯度”的解决方案。

InfoQ had an interview with Roger Schildmeijer, one of the two creators, about these two non blocking web server frameworks:

InfoQ接受了两个创建者之一的Roger Schildmeijer的采访,涉及这两个非阻塞性Web服务器框架:

InfoQ: What is Deft and Loft?

InfoQ:什么是Deft and Loft?

Roger: So before I start to describe what Deft and Loft is I would like to start from the beginning. September 2009 Facebook open sourced a piece of software called Tornado which was a relatively simple, non-blocking Web server framework written in Python, designed to handle thousands of simultaneous connection. Tornado gained a lot of attraction pretty quickly and became quite popular because of its strength and simplistic design. At this time a lot of developers out there became aware of the "c10k problem" (from Wikipedia: The C10k problem is the numeronym given to a limitation that most web servers currently have which limits the web server's capabilities to only handle about ten thousand simultaneous connections.)

罗杰 :所以,在我开始描述什么是Deft and Loft之前,我想从头开始。 2009年9月,Facebook开源了一个名为Tornado的软件,该软件是一个相对简单的,用Python编写的无阻塞Web服务器框架,旨在处理数千个同时连接。 龙卷风很快就吸引了很多人的眼球,并因其强大的力量和简单的设计而广受欢迎。 这时,许多开发人员意识到了“ c10k问题”(来自Wikipedia: C10k问题是代名词,它限制了大多数Web服务器当前具有的限制,该限制将Web服务器的功能限制为只能同时处理大约一万个)连接 。)

In the late summer of 2010 Jim Petersson and I started started to discuss and design an asynchronous non-blocking web server/framework running on the JVM using pure Java NIO. I would say that the main reason for this initiative was our curiosity about the potential speed improvements that could be achieved with a system similar to Tornado but written in Java. We knew that we could never create an API as clean and simple as Tornado's.

在2010年夏末,我和Jim Petersson开始讨论并设计一个使用纯Java NIO在JVM上运行的异步无阻塞Web服务器/框架。 我要说的是,这项倡议的主要原因是我们对使用类似于Tornado但用Java编写的系统可能实现的速度改进感到好奇。 我们知道,我们永远无法创建像Tornado一样干净,简单的API。

(Clean APIs have never been the advantage of Java, if you ask me)

(如果您问我,干净的API从来都不是Java的优势)

We got something up and running within the first 48h and saw some extraordinary (very non-scientific) benchmark results. This was the feedback we aimed for and Deft was born.

在最初的48小时内,我们开始运作并看到了一些非凡(非常不科学)的基准测试结果。 这是我们的目标反馈,而Deft诞生了。

Just to clarify, I would be the last person to suggest that someone should throw away their current system using Tornado (or some other asynchronous non-blocking web server) and replace it with Deft. Deft is still pretty young and has a lot to learn (there are a lot of issues to be implemented). By the time this interview is published I hope that the next stable Deft release, 0.2.0, will be ready and released.

为了澄清一下,我将是最后一个建议有人使用Tornado(或其他异步非阻塞Web服务器)丢弃当前系统,并用Deft替换它的人。 Deft还很年轻,还有很多东西要学习(有很多问题需要解决)。 我希望在本次采访发布之时,我可以准备并发布下一个稳定的Deft版本0.2.0。

After a couple of weeks of Deft hacking we started to discuss how Deft would look like if we had used another language, like Scala. It would be very gratifying to create something with nice performance but also a system that had a clean and elegant syntax. This was the seed for another project, and Loft was born. To make another important clarification: Loft is still very much in its infancy and there is yet no stable release available.

经过几周的Deft骇客入侵之后,我们开始讨论如果使用其他语言(例如Scala),则Deft的外观如何。 创建具有良好性能但又具有干净优雅语法的系统将非常令人满足。 这是另一个项目的种子,Loft诞生了。 需要进一步说明的是:Loft仍处于起步阶段,还没有稳定的发行版。

The main features that set Loft apart from Deft are: 

使Loft与Deft脱颖而出的主要功能是:

  1. it's written in Scala
  2. it uses a feature in Scala called continuations. The reason for this is to make asynchronous programming easier. (Will explain that in detail below)
  3. the initial version will be a pure Tornado clone
  1. 它是用Scala编写的
  2. 它在Scala中使用了称为延续的功能。 这样做的原因是使异步编程更容易。 (下面将详细解释)
  3. 初始版本将是纯的龙卷风克隆

InfoQ: Would you like to explain to us the motivation behind those architectural decisions?

InfoQ:您想向我们解释这些架构决策背后的动机吗?

Roger: We wanted to create something similar to Tornado that runs on the Java Virtual Machine. There are a lot of good (multi-threaded) open source web servers and web frameworks (apache tomcat and jetty are two popular examples) out there already, and we didn't want to compete with those beasts. Instead we wanted to build something that was good at handling thousands of simultaneous connections. The single threaded approach was already tested (and proved successful) by frameworks like Tornado.

Roger :我们想创建类似于在Java虚拟机上运行的Tornado的东西。 已经有很多好的(多线程)开源Web服务器和Web框架(Apache Tomcat和Jetty是两个受欢迎的示例),我们不想与这些野兽竞争。 相反,我们想要构建一种擅长处理数千个同时连接的东西。 单线程方法已经通过Tornado之类的框架进行了测试(并证明是成功的)。

InfoQ: What is a typical development workflow in Deft - from coding and debugging, up to deploying and monitoring?

InfoQ:从编码和调试到部署和监视,Deft中典型的开发工作流程是什么?

Roger: This is a very good question and something that is very important to address. However, because Deft is so new and its user base is currently small, I'm afraid I can't give an example of a typical development workflow. That is something we hope a growing community will help flesh out.. 

罗杰 :这是一个很好的问题,非常重要。 但是,由于Deft太新了,并且它的用户群很小,所以我无法举一个典型的开发流程示例。 我们希望这是一个不断发展的社区能够帮助充实自己的东西。

The biggest difference from the coding that most Java developers do is that everything executes inside a single threaded environment. The benefits of coding that way are that you don't have to use explicit locks and you don't have to think about deadlocks because of inadequate synchronized code. The downside is that you are not allowed to do blocking operations. A blocking operation will stall the entire server and make it unresponsive

与大多数Java开发人员所做的编码最大的不同是,所有操作都在单个线程环境中执行。 以这种方式进行编码的好处是,由于同步代码不足,您不必使用显式锁,也不必考虑死锁。 不利的一面是不允许您执行阻止操作。 阻止操作将使整个服务器停顿并使其无响应

Deft 0.2.0 will contain an experimental JMX API used for monitoring. Some examples of things that could be monitored are the number of pending timeouts/keep-alive timeouts, number of registered IO handlers in the event loop and the select timeout of the selector.

Deft 0.2.0将包含用于监视的实验性JMX API。 可以监视的事情的一些示例是未决超时/保持活动超时的数量,事件循环中注册的IO处理程序的数量以及选择器的选择超时。

InfoQ: Are there any benchmarks that evaluate Deft's performance?

InfoQ:是否有任何评估Deft性能的基准?

Roger: We have made some (non-scientific) benchmarks against a simple web server using a single request handler that responds with a 200 OK HTTP status code and writes "hello world" to the client (The code for the benchmarks is also available on github.com/rschildmeijer). Against a simple "hello-world server" we have seen speed improvements by a factor of 8-10x (compared to Tornado). The entire results from the benchmarks are available on http://deftserver.org/.

Roger :我们已经使用一个简单的请求处理程序针对一个简单的Web服务器制定了一些(非科学的)基准测试,该处理程序以200 OK HTTP状态代码进行响应并向客户端写入“ hello world”(基准测试的代码也可以在github.com/rschildmeijer )。 在简单的“ hello-world服务器”上,我们看到速度提高了8到10倍(与Tornado相比)。 可从http://deftserver.org/获得基准测试的全部结果。

InfoQ: How does Deft compare with other solutions like Tornado, Node.js or Netty?

InfoQ:Deft与Tornado,Node.js或Netty等其他解决方案相比如何?

Roger: Tornado and Node.js are the two other asynchronous web servers that we used in the benchmark. We didn't include Netty because it felt a little bit like comparing apples with oranges. But I wouldn't doubt if Netty showed numbers equal to (or greater?) the results we have seen for Deft. Netty, the successor to apache mina, is a really cool socket framework written by a really smart guy (Trustin Lee). 

Roger :Tornado和Node.js是我们在基准测试中使用的另外两个异步Web服务器。 我们不包括Netty,因为它有点像比较苹果和桔子。 但是我不会怀疑Netty是否显示等于或大于我们为Deft看到的结果的数字。 Netty是Apache Mina的继承者,它是一个非常酷的套接字框架,由一个非常聪明的人(Trustin Lee)编写。

InfoQ: What was the motivation for Loft and how does it use continuations?

InfoQ:Loft的动机是什么?它如何使用延续性?

Roger: So finally time to show some code! (I would like to (once again) remind you that Loft is pretty much in the starting blocks and the code snippets for Loft are the proposed design)

罗杰 :那么终于可以显示一些代码了! (我想(再次)提醒您,Loft非常在起步中,Loft的代码段是建议的设计)

A simple request handler for Loft could look something like this:

Loft的一个简单请求处理程序可能看起来像这样:

@Asynchronous
  def get() {
    val http = AsyncHTTPClient()
    reset {
      val id = database get("roger_schildmeijer");   //async call
      val result = http fetch("http://127.0.0.1:8080/" + id); //async call
      write(result)
      finish 
    }
  }

  val application = Application(Map("/".r -> this))

  def main(args: Array[String]) {
    val httpServer = HTTPServer(application)
    httpServer listen(8888)
    IOLoop start
  }
}

The main method contains the canonical way to start a Loft instance. The ExampleHandler.get is where things start to get interesting. Inside the method two asynchronous calls are made. Asynchronous programming is often conducted by supplying a callback as an additional parameter, and that callback will be called when the result is ready. And if you have two or more consecutive asynchronous calls, you (usually) will have to chain these calls together. 

main方法包含启动Loft实例的规范方法。 ExampleHandler.get是让事情开始变得有趣的地方。 在该方法内部,进行了两个异步调用。 异步编程通常通过提供回调作为附加参数来进行,并且在准备好结果时将调用该回调。 而且,如果您有两个或多个连续的异步调用,您(通常)将不得不将这些调用链接在一起。

E.g.:

例如:

database get("roger_schildmeijer", (id) => http fetch("http://127.0.0.1:8080/" + id, (result) => write(result)));

So what is actually going in the ExampleHandler.get method above?

那么,上面的ExampleHandler.get方法实际上在做什么呢?

You might have noticed the "reset" word in method, this indicates that some Scala continuations are about to happen. Continuations, as a concept, are hard to grasp. But when the magic disappears, continuations are just functions representing another point in the program (It contains information such as the process's current stack). If you call the continuation, it will cause execution to automatically switch to the point that function represents. (Actually you use restricted versions of them every time you do exception handling).

您可能已经注意到method中的“ reset”字样,这表明某些Scala延续将要发生。 连续性作为一个概念很难把握。 但是,当魔力消失时,延续只是代表程序中另一点的函数(它包含诸如进程当前堆栈之类的信息)。 如果您调用延续,它将导致执行自动切换到函数表示的点。 (实际上,每次执行异常处理时,都使用它们的受限版本)。

Just so I don't confuse anyone. The two asynchronous methods "get" and "fetch" must be implemented in a certain way in order for this example to work.

只是为了让我不要混淆任何人。 为了使本示例正常工作,必须以某种方式实现两个异步方法“ get”和“ fetch”。

InfoQ:  The asynchronous, event-driven paradigm has gained lots of attention lately. Why do you think is that and how do you see this trend evolving in the future?

InfoQ:异步的,事件驱动的范例最近引起了很多关注。 您为什么这么认为?您如何看待未来的趋势?

One reason that systems like Tornado, node.js and Netty have received a lot of attention in recent years is because of big social networks that need a huge number of idle connections.

近年来,像Tornado,node.js和Netty之类的系统受到广泛关注的原因之一是由于大型社交网络需要大量的空闲连接。

As long as you and I use services like Facebook and twitter I think the need for systems like Deft, Loft and Tornado will exist.

只要您和我使用Facebook和Twitter之类的服务,我认为对Deft,Loft和Tornado之类的系统的需求就会存在。

As a final note I would like to add that we are looking for contributors that are interested in supporting Deft (current status: two committers, two contributors) and/or Loft (two committers).

最后一点,我想补充一点,我们正在寻找对支持Deft(当前状态:两个提交者,两个贡献者)和/或Loft(两个提交者)感兴趣的贡献者。

翻译自: https://www.infoq.com/articles/deft-loft/?topicPageSponsorship=c1246725-b0a7-43a6-9ef9-68102c8d48e1

jvm虚拟机驱动

 类似资料: