Creating and Configuring Jetty 9 Server in Java – 在Java中创建和配置Jetty 9服务器

最后修改: 2018年 1月 23日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll talk about creating and configuring a Jetty instance programmatically.

在这篇文章中,我们将讨论如何以编程方式创建和配置一个Jetty实例。

Jetty is an HTTP server and servlet container designed to be lightweight and easily embeddable. We’ll take a look at how to setup and configure one or more instances of the server.

Jetty是一个HTTP服务器和servlet容器,旨在实现轻量级和易于嵌入。我们将看看如何设置和配置该服务器的一个或多个实例。

2. Maven Dependencies

2.Maven的依赖性

To start off, we want to add Jetty 9 with the following Maven dependencies into our pom.xml:

首先,我们要将Jetty 9与以下Maven依赖项添加到我们的pom.xml

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.4.8.v20171121</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
    <version>9.4.8.v20171121</version>
</dependency>

3. Creating a Basic Server

3.创建一个基本的服务器

Spinning up an embedded server with Jetty is as easy as writing:

用Jetty启动一个嵌入式服务器就像写作一样容易。

Server server = new Server();
server.start();

Shutting it down is equally simple:

关掉它同样简单。

server.stop();

4. Handlers

4.处理者

Now that our server is up and running, we need to instruct it on what to do with the incoming requests. This can be performed using the Handler interface.

现在我们的服务器已经启动并运行,我们需要指示它如何处理进入的请求。这可以通过Handler接口来完成。

We could create one ourselves but Jetty already provides a set of implementations for the most common use cases. Let’s take a look at two of them.

我们可以自己创建一个,但Jetty已经为最常见的使用情况提供了一套实现。让我们看一下其中的两个。

4.1. WebAppContext

4.1.WebAppContext

The WebAppContext class allows you to delegate the request handling to an existing web application. The application can be provided either as a WAR file path or as a webapp folder path.

WebAppContext类允许你将请求处理委托给一个现有的Web应用程序。该应用程序可以作为WAR文件路径或webapp文件夹路径提供。

If we want to expose an application under the “myApp” context we would write:

如果我们想在 “myApp “上下文下公开一个应用程序,我们会写。

Handler webAppHandler = new WebAppContext(webAppPath, "/myApp");
server.setHandler(webAppHandler);

4.2. HandlerCollection

4.2.HandlerCollection

For complex applications, we can even specify more than one handler using the HandlerCollection class.

对于复杂的应用程序,我们甚至可以使用HandlerCollection类指定一个以上的处理程序。

Suppose we have implemented two custom handlers. The first one performs only logging operations meanwhile the second one creates and sends back an actual response to the user. We want to process each incoming request with both of them in this order.

假设我们已经实现了两个自定义处理程序。第一个处理程序只执行日志操作,同时第二个处理程序创建并向用户发送一个实际的响应。我们想用这两个处理程序来处理每个传入的请求,其顺序如下。

Here’s how to do it:

以下是如何做到这一点的。

Handler handlers = new HandlerCollection();
handlers.addHandler(loggingRequestHandler);
handlers.addHandler(customRequestHandler);
server.setHandler(handlers);

5. Connectors

5.连接器

The next thing we want to do is configuring on which addresses and ports the server will be listening and adding an idle timeout.

接下来我们要做的是配置服务器将在哪些地址和端口上进行监听,并添加一个空闲超时。

The Server class declares two convenience constructors that may be used to bind to a specific port or address.

Server类声明了两个方便的构造函数,可用于绑定到一个特定的端口或地址。

Although this may be ok when dealing with small applications, it won’t be enough if we want to open multiple connections on different sockets.

尽管在处理小型应用程序时这可能是可以的,但如果我们想在不同的套接字上打开多个连接,这就不够了。

In this situation, Jetty provides the Connector interface and more specifically the ServerConnector class which allows defining various connection configuration parameters:

在这种情况下,Jetty提供了Connector接口,更具体的是ServerConnector类,它允许定义各种连接配置参数。

ServerConnector connector = new ServerConnector(server);
connector.setPort(80);
connector.setHost("169.20.45.12");
connector.setIdleTimeout(30000);
server.addConnector(connector);

With this configuration, the server will be listening on 169.20.45.12:80. Each connection established on this address will have a timeout of 30 seconds.

通过这种配置,服务器将在169.20.45.12:80上监听。在这个地址上建立的每个连接将有一个30秒的超时。

If we need to configure other sockets we can add other connectors.

如果我们需要配置其他插座,我们可以添加其他连接器。

6. Conclusion

6.结论

In this quick tutorial, we focused on how to set up an embedded server with Jetty. We also saw how to perform further configurations using Handlers and Connectors.

在这个快速教程中,我们着重介绍了如何用Jetty设置一个嵌入式服务器。我们还看到了如何使用HandlersConnectors进行进一步配置。

As always, all the code used here can be found over on GitHub.

像往常一样,这里使用的所有代码都可以在GitHub上找到