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.
当然可以更换别的。
the spring-boot-starter-web includes Tomcat by including spring-boot-starter-tomcat
Use Another Web Server
Many Spring Boot starters include default embedded containers.
许多Spring Boot启动器包含默认的嵌入式容器。
-
For servlet stack applications, the
spring-boot-starter-webincludes Tomcat by includingspring-boot-starter-tomcat, but you can usespring-boot-starter-jettyinstead. -
For reactive stack applications, the
spring-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-tomcat或spring-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 your application.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 main HTTP port defaults to 8080 but can be set with server.port (for example, in application.properties or as a System property). Thanks to relaxed binding of Environment values, you can also use SERVER_PORT (for example, as an OS environment variable).
To switch off the HTTP endpoints completely but still create a WebApplicationContext, use server.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 your
application.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.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 (see
server.tomcat). For instance, access logs can be configured with specific features of the embedded servlet container.自定义嵌入式Servlet容器
常见的Servlet容器设置可以通过Spring Environment属性进行配置。通常,您会在application.properties或application.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) use server.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 type ApplicationListener<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端口
您可以从日志输出或通过WebServerApplicationContext的WebServer访问服务器运行的端口。确保端口已初始化的最佳方式是添加一个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 in application.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 the server.compression.min-response-size property.
默认情况下,只有当响应内容至少达到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 the server.compression.mime-types property.
Configure SSL
SSL can be configured declaratively by setting the various server.ssl.* properties, typically in application.properties or application.yaml. See Ssl for details of all of the supported properties.
配置SSL
可以通过声明式地设置各种server.ssl.*属性来配置SSL,通常在application.properties或application.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 through application.properties. If you want to have both, you need to configure one of them programmatically. We recommend using application.properties to 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

1354

被折叠的 条评论
为什么被折叠?



