Undertow · JBoss Community
Undertow
Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.
Undertow 是一個用 Java 編寫的靈活高性能 Web 服務器,提供基于 NIO 的阻塞和非阻塞 API。
Undertow has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet 4.0 container, or a low level non-blocking handler, to anything in between.
Undertow采用基于組合的架構,允許您通過組合小型單一功能處理器來構建Web服務器。這種架構讓您能夠靈活選擇——無論是完整的Java EE Servlet 4.0容器,還是底層的非阻塞處理器,抑或是介于兩者之間的任何方案。(備注: Oracle Java EE 是收訂閱費的。)
Undertow is designed to be fully embeddable, with easy to use fluent builder APIs. Undertow’s lifecycle is completely controlled by the embedding application.
Undertow 設計為完全可嵌入式,提供易于使用的流式構建器 API。其生命周期完全由嵌入應用控制。
Undertow is sponsored by JBoss and is the default web server in the Wildfly Application Server.
文檔? Online?Undertow
There are two main ways that Undertow can be used, either by directly embedding it in your code, or as part of the Wildfly Application Server. This guide mostly focuses on the embedded API’s, although a lot of the content is still relevant if you are using Wildfly, it is just that the relevant functionality will generally be exposed via XML configuration rather than programmatic configuration.
Undertow主要有兩種使用方式:一種是直接嵌入到代碼中,另一種是作為Wildfly應用服務器的一部分。本指南主要聚焦于嵌入式API,盡管大部分內容對Wildfly用戶同樣適用,只是相關功能通常通過XML配置而非編程配置來實現。
HTTP/2 Support
Undertow supports HTTP/2 out of the box, no overriding the boot class path required.
Undertow 開箱即支持 HTTP/2,無需覆蓋引導類路徑。
HTTP Upgrade Support
Support for HTTP upgrade, to allow multiple protocols to be multiplexed over the HTTP port.
支持HTTP升級,允許多種協議在HTTP端口上實現多路復用。
Web Socket Support
Undertow provides full support for Web Sockets, including JSR-356 support.
Undertow 全面支持 Web Sockets,包括對 JSR-356 的支持。
Servlet 4.0
Undertow provides support for Servlet 4.0, including support for embedded servlet. It is also possible to mix both Servlets and native undertow non-blocking handlers in the same deployment.
Undertow 支持 Servlet 4.0,包括對嵌入式 servlet 的支持。在同一部署中也可以混合使用 Servlets 和原生的 undertow 非阻塞處理程序。
Embeddable
Undertow can be embedded in an application or run standalone with just a few lines of code.
Undertow 可以嵌入到應用程序中,也可以僅用幾行代碼獨立運行。
Flexible
An Undertow server is configured by chaining handlers together. It is possible to add as much or as little functionality as you need, so you don’t pay for what you are not using.
Undertow服務器的配置通過將處理器鏈式連接來實現。您可以根據需求自由增減功能模塊,從而避免為未使用的功能付出額外開銷。
啟動
public class HelloWorldServer {public static void main(final String[] args) {Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new HttpHandler() {@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");exchange.getResponseSender().send("Hello World");}}).build();server.start();}
}
處理 500 錯誤
public class SimpleErrorPageHandler implements HttpHandler {private final HttpHandler next;public SimpleErrorPageHandler(final HttpHandler next) {this.next = next;}@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.addDefaultResponseListener(new DefaultResponseListener() {@Overridepublic boolean handleDefaultResponse(final HttpServerExchange exchange) {if (!exchange.isResponseChannelAvailable()) {return false;}Set<Integer> codes = responseCodes;if (exchange.getResponseCode() == 500) {final String errorPage = "<html><head><title>Error</title></head><body>Internal Error</body></html>";exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + errorPage.length());exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");Sender sender = exchange.getResponseSender();sender.send(errorPage);return true;}return false;}});next.handleRequest(exchange);}
}
In order to use Undertow in your maven projects just include the following section in your pom.xml, and set the undertow.version property to whatever version of Undertow you wish to use. Only the core artifact is required, if you are not using Servlet or JSR-356 then those artifacts are not required.
要在您的 Maven 項目中使用 Undertow,只需在 pom.xml 文件中包含以下部分,并將 undertow.version 屬性設置為您希望使用的 Undertow 版本。僅需核心構件(core artifact),如果您不使用 Servlet 或 JSR-356,則無需包含這些構件。
<dependency><groupId>io.undertow</groupId><artifactId>undertow-core</artifactId><version>${undertow.version}</version>
</dependency>
<dependency><groupId>io.undertow</groupId><artifactId>undertow-servlet</artifactId><version>${undertow.version}</version>
</dependency>
<dependency><groupId>io.undertow</groupId><artifactId>undertow-websockets-jsr</artifactId><version>${undertow.version}</version>
</dependency>
Undertow depends on XNIO and JBoss Logging, which will need to be downloaded as well.
Undertow依賴于XNIO和JBoss Logging,這些也需要下載。
https://github.com/jboss-logging/jboss-logging?
XNIO - JBoss Community
Bootstrapping Undertow
There are two ways to bootstrap Undertow. The first and most simple is to use the io.undertow.Undertow builder API. The second is to assemble a server using XNIO and the Undertow listener classes directly. This second approach requires more code, but gives more flexibility. It is anticipated that for most use cases the builder API will be sufficient.
有兩種方式來引導Undertow。第一種也是最簡單的方式是使用io.undertow.Undertow構建器API。第二種是直接使用XNIO和Undertow監聽器類來組裝服務器。第二種方法需要編寫更多代碼,但提供了更大的靈活性。預計對于大多數用例來說,構建器API已經足夠。
One thing that it is important to understand about Undertow is that there is not really any concept of an Undertow container. Undertow applications are assembled from multiple handler classes, and it is up to the embedding application to manage the lifecycle of all the these handlers. This was a deliberate design decision in order to give the embedding application as much control as possible. This is generally only an issue if you have handlers that hold resources that need to be cleaned up at server stop.
關于Undertow需要理解的重要一點是,它實際上并不存在"Undertow容器"的概念。Undertow應用程序由多個處理器類組裝而成,而這些處理器的生命周期管理完全取決于嵌入應用程序。這是一項經過深思熟慮的設計決策,目的是為了賦予嵌入應用程序最大限度的控制權。通常只有當您使用的處理器持有需要在服務器停止時清理的資源時,這才成為一個需要考慮的問題。
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;public class HelloWorldServer {public static void main(final String[] args) {Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new HttpHandler() {@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");exchange.getResponseSender().send("Hello World");}}).build();server.start();}
}
Why isn’t Undertow based on Mina, Netty, RNIO, Grizzly, or <insert network framework>?
In order to best achieve its goals, Undertow requires very close integration with the underlying I/O infrastructure in the Java platform. The simplicity offered by any abstraction comes from hiding the underlying mechanisms behind it. However, the dilemma is that building an extremely efficient and flexible web server requires customization and control of these mechanisms. Undertow attempts to strike the right balance by reusing a minimalistic I/O library, XNIO, that was created for WildFly’s remote invocation layer.
為了最好地實現其目標,Undertow需要與Java平臺中的底層I/O基礎設施進行非常緊密的集成。任何抽象所提供的簡單性都源于對其背后底層機制的隱藏。然而,難題在于構建一個極其高效且靈活的Web服務器需要對這些機制進行定制和控制。Undertow嘗試通過復用為WildFly遠程調用層創建的極簡I/O庫XNIO,來達到恰當的平衡。
XNIO allows us to eliminate some boiler plate, and also allows for direct I/O integration with the operating system, but it does not go further than that. In addition, XNIO offers very strong backwards compatibility which is important since this is also a concern for the Undertow project. Of course, other projects may have different needs, and thus might make different choices.
XNIO 讓我們能夠減少一些樣板代碼,并實現與操作系統的直接 I/O 集成,但它的功能也僅限于此。此外,XNIO 提供了極強的向后兼容性,這一點非常重要,因為這也是 Undertow 項目關注的重點。當然,其他項目可能有不同的需求,因此可能會做出不同的選擇。