当前位置: 首页 > news >正文

Spring boot 4.0.1 嵌入的 web服务器,以及如何更换别的 web 服务器

Spring boot 默认嵌入的 web 服务器 —— Tomcat 。

Each Spring Boot web application includes an embedded web server. This feature leads to a number of how-to questions, including how to change the embedded server and how to configure the embedded server. This section answers those questions.

当然可以更换别的。

thespring-boot-starter-webincludes Tomcat by includingspring-boot-starter-tomcat

Use Another Web Server

Many Spring Boot starters include default embedded containers.

许多Spring Boot启动器包含默认的嵌入式容器。

  • For servlet stack applications, thespring-boot-starter-webincludes Tomcat by includingspring-boot-starter-tomcat, but you can usespring-boot-starter-jettyinstead.

  • For reactive stack applications, thespring-boot-starter-webfluxincludes Reactor Netty by includingspring-boot-starter-reactor-netty, but you can usespring-boot-starter-tomcatorspring-boot-starter-jettyinstead.

对于Servlet栈应用,spring-boot-starter-web通过包含spring-boot-starter-tomcat默认引入Tomcat,但您也可以改用spring-boot-starter-jetty

对于响应式栈应用,spring-boot-starter-webflux通过包含spring-boot-starter-reactor-netty默认引入Reactor Netty,但您也可以改用spring-boot-starter-tomcatspring-boot-starter-jetty

When switching to a different HTTP server, you need to swap the default dependencies for those that you need instead. To help with this process, Spring Boot provides a separate starter for each of the supported HTTP servers.

切换到不同的 HTTP 服务器时,您需要用所需的依赖项替换默认依赖项。为了简化此过程,Spring Boot 为每个受支持的 HTTP 服务器提供了单独的启动器。

The following example shows how to exclude Tomcat and include Jetty for Spring MVC:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc</artifactId> <exclusions> <!-- Exclude the Tomcat dependency --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Use Jetty instead --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>

If you are creating a war file, you can use a similar approach, but you must indicate provided dependencies:

如果您正在创建一个war文件,可以采用类似的方法,但必须指明提供的依赖项。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc</artifactId> <exclusions> <!-- Exclude the Tomcat dependency --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Use Jetty instead --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <scope>provided</scope> </dependency>

Disabling the Web Server

禁用Web服务器

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behavior configure the WebApplicationType in yourapplication.properties, as shown in the following example:

如果您的类路径包含启动Web服务器所需的组件,Spring Boot将自动启动它。要禁用此行为,请在您的application.properties中配置WebApplicationType,如下例所示:

# YAML

spring:
main:
web-application-type: "none"

Change the HTTP Port

In a standalone application, the mainHTTP port defaults to8080but can be set withserver.port(for example, inapplication.propertiesor as a System property). Thanks to relaxed binding of Environment values, you can also useSERVER_PORT(for example, as an OS environment variable).

To switch off the HTTP endpoints completely but still create a WebApplicationContext, useserver.port=-1(doing so is sometimes useful for testing).

For more details, see Customizing Embedded Servlet Containers in the ‘Spring Boot Features’ section, or the ServerProperties class.

更改HTTP端口
在独立应用程序中,主HTTP端口默认为8080,但可通过server.port设置(例如在application.properties中或作为系统属性)。得益于环境值的宽松绑定,您也可以使用SERVER_PORT(例如作为操作系统环境变量)。

要完全关闭HTTP端点但仍创建WebApplicationContext,可使用server.port=-1(这样做有时对测试很有用)。

更多详细信息,请参阅“Spring Boot特性”部分中的[自定义嵌入式Servlet容器],或查看[ServerProperties]类。

Customizing Embedded Servlet Containers

Common servlet container settings can be configured by using Spring Environment properties. Usually, you would define the properties in yourapplication.propertiesorapplication.yamlfile.

Common server settings include:

  • Network settings: Listen port for incoming HTTP requests (server.port), interface address to bind to (server.address), and so on.

  • Session settings: Whether the session is persistent (server.servlet.session.persistent), session timeout (server.servlet.session.timeout), location of session data (server.servlet.session.store-dir), and session-cookie configuration (server.servlet.session.cookie.*).

  • Error management: Location of the error page (spring.web.error.path) and so on.

  • SSL

  • HTTP compression

Spring Boot tries as much as possible to expose common settings, but this is not always possible. For those cases, dedicated namespaces offer server-specific customizations (seeserver.tomcat). For instance, access logs can be configured with specific features of the embedded servlet container.

自定义嵌入式Servlet容器
常见的Servlet容器设置可以通过Spring Environment属性进行配置。通常,您会在application.propertiesapplication.yaml文件中定义这些属性。

常见的服务器设置包括:

网络设置:监听HTTP请求的端口(server.port)、绑定的接口地址(server.address)等。

会话设置:会话是否持久化(server.servlet.session.persistent)、会话超时时间(server.servlet.session.timeout)、会话数据存储位置(server.servlet.session.store-dir)以及会话Cookie配置(server.servlet.session.cookie.*)。

错误管理:错误页面路径(spring.web.error.path)等。

SSL
HTTP压缩

Spring Boot尽可能多地暴露通用设置,但并非所有情况都适用。对于这些情况,专用命名空间提供了针对特定服务器的自定义配置(例如server.tomcat)。例如,可以通过嵌入式Servlet容器的特定功能来配置访问日志。

https://docs.spring.io/spring-boot/reference/web/servlet.html#web.servlet.embedded-container.customizing

Use a Random Unassigned HTTP Port

To scan for a free port (using OS natives to prevent clashes) useserver.port=0.

Discover the HTTP Port at Runtime

You can access the port the server is running on from log output or from the WebServerApplicationContext through its WebServer. The best way to get that and be sure it has been initialized is to add a @Bean of typeApplicationListener<WebServerInitializedEvent>and pull the container out of the event when it is published.

Tests that use@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)can also inject the actual port into a field by using the @LocalServerPort annotation, as shown in the following example:

运行时发现HTTP端口
您可以从日志输出或通过WebServerApplicationContextWebServer访问服务器运行的端口。确保端口已初始化的最佳方式是添加一个ApplicationListener<WebServerInitializedEvent>类型的@Bean,并在事件发布时从中提取容器。

使用@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)的测试还可以通过@LocalServerPort注解将实际端口注入字段,如下例所示:

import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.server.LocalServerPort; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class MyWebIntegrationTests { @LocalServerPort int port; // ... }

Enable HTTP Response Compression

启用HTTP响应压缩

HTTP response compression is supported by Jetty, Tomcat and Reactor Netty. It can be enabled inapplication.properties, as follows:

Jetty、Tomcat和Reactor Netty均支持HTTP响应压缩功能。可通过在application.properties文件中进行如下配置来启用:

# YAML

server:
compression:
enabled: true

By default, responses must be at least 2048 bytes in length for compression to be performed. You can configure this behavior by setting theserver.compression.min-response-sizeproperty.

默认情况下,只有当响应内容至少达到2048字节时才会执行压缩。您可以通过设置server.compression.min-response-size属性来配置此行为。

By default, responses are compressed only if their content type is one of the following:

  • text/html

  • text/xml

  • text/plain

  • text/css

  • text/javascript

  • application/javascript

  • application/json

  • application/xml

You can configure this behavior by setting theserver.compression.mime-typesproperty.

Configure SSL

SSL can be configured declaratively by setting the variousserver.ssl.*properties, typically inapplication.propertiesorapplication.yaml. See Ssl for details of all of the supported properties.

配置SSL
可以通过声明式地设置各种server.ssl.*属性来配置SSL,通常在application.propertiesapplication.yaml文件中进行。有关所有支持属性的详细信息,请参阅Ssl

The following example shows setting SSL properties using a Java KeyStore file:

server:
port: 8443
ssl:
key-store: "classpath:keystore.jks"
key-store-password: "secret"
key-password: "another-secret"

Using configuration such as the preceding example means the application no longer supports a plain HTTP connector at port 8080. Spring Boot does not support the configuration of both an HTTP connector and an HTTPS connector throughapplication.properties. If you want to have both, you need to configure one of them programmatically. We recommend usingapplication.propertiesto configure HTTPS, as the HTTP connector is the easier of the two to configure programmatically.

使用如上示例的配置意味着应用程序不再支持8080端口的普通HTTP连接器。Spring Boot不支持通过application.properties同时配置HTTP和HTTPS连接器。如需同时启用这两种连接器,需要通过编程方式配置其中一种。我们建议使用application.properties配置HTTPS,因为HTTP连接器是两者中更容易通过编程方式配置的选项。

Using PEM-encoded files

You can use PEM-encoded files instead of Java KeyStore files. You should use PKCS#8 key files wherever possible. PEM-encoded PKCS#8 key files start with a-----BEGIN PRIVATE KEY-----or-----BEGIN ENCRYPTED PRIVATE KEY-----header.

If you have files in other formats, e.g., PKCS#1 (-----BEGIN RSA PRIVATE KEY-----) or SEC 1 (-----BEGIN EC PRIVATE KEY-----), you can convert them to PKCS#8 using OpenSSL:

使用PEM编码文件 您可以使用PEM编码文件替代Java密钥库文件。应尽可能使用PKCS#8格式的密钥文件。PEM编码的PKCS#8密钥文件以-----BEGIN PRIVATE KEY-----或-----BEGIN ENCRYPTED PRIVATE KEY-----开头。

如果您持有其他格式的文件,例如PKCS#1(-----BEGIN RSA PRIVATE KEY-----)或SEC 1(-----BEGIN EC PRIVATE KEY-----),可以使用OpenSSL将其转换为PKCS#8格式:

openssl pkcs8 -topk8 -nocrypt -in <input file> -out <output file>

The following example shows setting SSL properties using PEM-encoded certificate and private key files:

以下示例展示了使用PEM编码的证书和私钥文件设置SSL属性:

server: port: 8443 ssl: certificate: "classpath:my-cert.crt" certificate-private-key: "classpath:my-cert.key" trust-certificate: "classpath:ca-cert.crt"

更多内容 https://docs.spring.io/spring-boot/how-to/webserver.html

http://www.cnnetsun.cn/news/27640.html

相关文章:

  • GG3M业务核心:需求满足与问题解决 | GG3M Business Core: Demand Satisfaction and Problem Solving
  • 零基础玩转Vulhub:从安装到第一个漏洞复现
  • AI如何帮你快速解决Unexpected End of File错误
  • 企业级实战:用Vulhub构建内部攻防演练平台
  • 小白也能懂:Maven 3.6.1图文安装指南
  • 2025年Top5软件外包平台实战评测
  • React小白也能懂:useEffect入门图解指南
  • 电商网站遇到Internal Server Error的应急处理方案
  • 基于微信小程序+node.js的校园餐饮系统设计与实现
  • springboot基于vue的大学生公益活动志愿服务系统的设计与实现_nahamqu8
  • 操作系统 李治军 4 设备驱动与文件系统
  • 深度学习入门:图像分类的实战应用
  • kafka
  • 刘洋洋新歌《梁祝之三世约》上线,唱尽轮回绝恋
  • 一个完全本地运行的视频转文字工具:Vid2X
  • Java 开发最容易犯的 10 个错误
  • 用 Reader 建个私人图书馆,加上cpolar随时随地畅快阅读
  • 下一代盲盒系统核心架构解析:JAVA-S1如何打造极致公平与全球化体验
  • LangGraph深度解析:从图基础到人机交互的AI工作流框架实践
  • C++--
  • 算法练习4--数组:长度最小的子数组
  • Spring Cloud Gateway为什么要推出 WebMVC 版本?深度解析两大版本的差异与选型
  • git和github的区别
  • 小白从零开始勇闯人工智能Linux初级篇(MySQL库)
  • Bootstrap 模态框详解
  • MinerU终极安全离线部署指南:完全断网环境解决方案
  • 练题100天——DAY24:罗马数字转整数+环形链表+大小端判断
  • 网站域名:关键的战略资产
  • Airflow 做 ETL,真不是“排个 DAG 就完事儿”:那些年我踩过的坑与悟出的道
  • 数据库连接池监控最佳实践:用 Prometheus + Grafana 打造可视化监控体系