Jetty

Eclipse Jetty is a Java HTTP (Web) server and Java Servlet container. While Web Servers are usually associated with serving documents to people, Jetty is now often used for machine to machine communications, usually within larger software frameworks. Jetty is developed as a free and open source project as part of the Eclipse Foundation. The web server is used in products such as Apache ActiveMQ,[2] Alfresco,[3] ScalatraApache Geronimo,[4] Apache Maven,  Apache SparkGoogle App Engine,[5] Eclipse,[6] FUSE,[7] iDempiere,[8] Twitter's Streaming API[9] and Zimbra.[10] Jetty is also the server in open source projects such as LiftEucalyptusOpenNMSRed5Hadoop and I2P.[11] Jetty supports the latest Java Servlet API (with JSP support) as well as protocols HTTP/2 and WebSocket.

官网链接直达下载:

https://www.eclipse.org/jetty/download.html

Start with Document:

https://www.eclipse.org/jetty/documentation/9.4.x/introduction.html#what-is-jetty

0.设置自己的JETTY_BASE、JETTY_HOME

> JETTY_BASE=/tmp/mybase
> mkdir $JETTY_BASE
> cd $JETTY_BASE
> java -jar $JETTY_HOME/start.jar --create-startd
> java -jar $JETTY_HOME/start.jar --add-to-start=http,deploy
> java -jar $JETTY_HOME/start.jar

打开http://localhost:8080 查看内容。

1.修改默认端口命令

默认端口号是8080,可以通过以下命令启动时修改默认端口

>java -jar $JETTY_HOME/start.jar jetty.http.port=8083

或者直接修改$JETTY_BASE/start.d/http.ini配置文件

2.Configuration Files

Configuring Web Applications

The servlet specification defines a web application, which when packaged as a zip is called WAR file (Web application ARchive). Jetty implements both WAR files and unpacked web applications as a specialized context that is configured by means of:

  • A standard layout which sets the location of the resourceBase (the root of the WAR) and initializes the classpath from jars found in WEB-INF/lib and classes found in WEB-INF/classes.
  • The standard WEB-INF/web.xml deployment descriptor which is parsed to define and configure init parameters, filters, servlets, listeners, security constraints, welcome files and resources to be injected.
  • A default web.xml format deployment descriptor provided either by Jetty or in configuration configures the JSP servlet and the default servlet for handling static content. The standard web.xml may override the default web.xml.
  • Annotations discovered on classes in Jars contained in WEB-INF/lib can declare additional filters, servlets and listeners.
  • Standard deployment descriptor fragments discovered in Jars contained in WEB-INF/lib can declare additional init parameters, filters, servlets, listeners, security constraints, welcome files and resources to be injected.
  • An optional WEB-INF/jetty-web.xml file may contain Jetty IoC configuration to configure the Jetty specific APIs of the context and handlers.

Web Application Deployment

Jetty is capable of deploying a variety of Web Application formats. This is accomplished via scans of the ${jetty.base}/webapps directory for contexts to deploy.

A Context can be any of the following:

  • A standard WAR file. (must in ".war").
  • A directory containing an expanded WAR file. (must contain {dir}/WEB-INF/web.xml file).
  • A directory containing static content.
  • A XML descriptor in Jetty XML Syntax that configures a ContextHandler instance (Such as a WebAppContext).

Quickstart Webapps

The auto discovery features of the Servlet specification can make deployments slow and uncertain. Auto discovery of Web Application configuration can be useful during the development of a webapp as it allows new features and frameworks to be enabled simply by dropping in a jar file. However, for deployment, the need to scan the contents of many jars can have a significant impact of the start time of a webapp.

With the release of Jetty 9.2, a quickstart module was included which allows a webapp to be pre-scanned and preconfigured. This means that all the scanning is done prior to deployment and all configuration is encoded into an effective web.xml, called WEB-INF/quickstart-web.xml, which can be inspected to understand what will be deployed before deploying. Not only does the quickstart-web.xml contain all the discovered Servlets, Filters and Constraints, but it also encodes as context parameters all discovered:

  • ServletContainerInitializers
  • HandlesTypes classes
  • Taglib Descriptors

With the quickstart mechanism, Jetty is able to entirely bypass all scanning and discovery modes and start a webapp in a predictable and fast way. Tests have shown that webapps that took many seconds to scan and deploy can now be deployed in a few hundred milliseconds.

In a standard Jetty distribution the quickstart module can be configured with the following command:

$ java -jar $JETTY_HOME/start.jar --add-to-start=quickstart

Pre-generating the quickstart-web.xml file

Rather than use the autoPreconfigure feature of the QuickStartWebApp - which lazily generates the quickstart-web.xml file - you can eagerly pre-generate it for an existing war by invoking as a main class org.eclipse.jetty.quickstart.PreconfigureQuickStartWar. Note that you will need to provide all necessary jetty jars on the command line classpath. This will unpack the war if necessary, and create the quickstart-web.xml before the first deployment:

$ java -cp [jetty classpath] org.eclipse.jetty.quickstart.PreconfigureQuickStartWar myapp.war

Starting Jetty Using start.jar

https://www.eclipse.org/jetty/documentation/9.4.x/start-jar.html

Debugg and Start Logging

--debug

Enables debugging output of the startup procedure.

Note: This does not set up debug logging for Jetty itself. For information on logging, please see the section on Configuring Jetty Logging.]

--start-log-file=<filename>

Sends all startup output to the filename specified. Filename is relative to ${jetty.base}. This is useful for capturing startup issues where the Jetty-specific logger has not yet kicked in due to a possible startup configuration error.

Startup / Shutdown Command Line

--stop

Sends a stop signal to the running Jetty instance.

Note: The server must have been started with various stop properties for this to work.

STOP.PORT=<number>

The port to use to stop the running Jetty server. This is an internal port, opened on localhost, used solely for stopping the running Jetty server. Choose a port that you do not use to serve web traffic.

Required for --stop to function.

STOP.KEY=<alphanumeric>

The passphrase defined to stop the server.

Required for --stop to function.

STOP.WAIT=<number>

The time (in seconds) to wait for confirmation that the running Jetty server has stopped. If not specified, the stopper waits indefinitely for the server to stop.

If the time specified elapses, without a confirmation of server stop, then the --stop command exits with a non-zero return code.

You can configure a port number for Jetty to listen on for a stop command, so you are able to stop it from a different terminal. This requires the use of a "secret" key, to prevent malicious or accidental termination. Use the STOP.PORT and STOP.KEY (or -DSTOP.PORT= and -DSTOP.KEY=, respectively, which will set these as system parameters) parameters as arguments to the start.jar:

> java -jar ${JETTY_HOME}/start.jar STOP.PORT=1234 STOP.KEY=secretpassword

Then, to stop Jetty from a different terminal, you need to supply this port and key information. You can either use a copy of the Jetty distribution, the jetty-maven-plugin, the jetty-ant plugin, or a custom class to accomplish this. Here’s how to use the Jetty distribution, leveraging start.jar, to perform a stop:

> java -jar start.jar STOP.PORT=8181 STOP.KEY=abc123 --stop

Startup a Unix Service using jetty.sh

https://www.eclipse.org/jetty/documentation/9.4.x/startup-unix-service.html

Jetty Runner

https://www.eclipse.org/jetty/documentation/9.4.x/runner.html

The idea of the jetty-runner is extremely simple – run a webapp directly from the command line using a single jar file and as much default configuration as possible.

> java -jar jetty-runner.jar --port 9090 simple.war
> java -jar jetty-runner.jar --host 192.168.22.19 my.war
> java -jar jetty-runner.jar simple
> java -jar jetty-runner.jar --help

Ant and Jetty

https://www.eclipse.org/jetty/documentation/9.4.x/ant-and-jetty.html#jetty-ant

参考链接:

https://en.wikipedia.org/wiki/Jetty_(web_server)

03-08
### Jetty 服务器介绍 Jetty 是一款轻量级的 Java HTTP(S) 服务器和 Servlet 容器,广泛应用于开发环境以及生产环境中部署 Web 应用程序。其设计目标是为了提供高效能、可扩展性和灵活性,在处理高并发请求方面表现出色[^1]。 ### 使用 Spring Boot 集成 Jetty Spring Boot 默认采用 Tomcat 作为嵌入式的 Servlet 容器,但也可以轻松切换至其他容器如 Jetty 或 Undertow。为了在 Spring Boot 中使用 Jetty 而不是默认的 Tomcat,可以通过排除 `spring-boot-starter-tomcat` 并引入 `spring-boot-starter-jetty` 来实现这一点: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除Tomcat依赖 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加Jetty依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> ``` 通过上述配置更改后,应用程序启动时将会自动加载 Jetty 作为内嵌式 Web 服务器来运行应用实例。 对于更高级别的定制化需求,比如调整线程池大小或其他性能参数,则可以自定义 `ServletWebServerFactory` 的 Bean 实现。 ### 创建 WAR 文件用于外部 Jetty 部署 如果希望构建一个可以在独立安装版 Jetty 上部署的应用程序包 (WAR),则需确保项目的打包方式设置为 war,并且正确指定主类入口点以便于命令行执行。下面是一个典型的 Maven POM 设置片段展示如何创建这样的项目结构[^2]: ```xml <packaging>war</packaging> <properties> ... <start-class>com.example.MyApplication</start-class> ... </properties> ``` 此外还需要注意的是当以这种方式发布时应移除任何关于内置容器的支持代码,因为此时将由外部 Jetty 处理所有的HTTP 请求路由工作。 ### 日志记录工具 Log4j 在 Maven 项目中的集成 Log4j 是 Apache 提供的一个开源日志框架库,允许开发者方便地控制不同级别的消息输出行为。要在基于 Maven 构建系统的Java工程里加入此功能支持,首先要做的就是在 pom.xml 文件中声明相应的依赖关系项[^3]: ```xml <dependencies> ... <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> ... </dependencies> ``` 接着按照官方文档指导完成必要的初始化过程即可开始利用它来进行有效的错误跟踪与调试辅助操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值