Spring Boot 内嵌 Web 容器介绍及常见运行参数

Spring Boot 支持的内嵌容器有 Tomcat(默认) 、Jetty 、Undertow 和 Reactor Netty (v2.0+), 借助可插拔 (SPI) 机制的实现,开发者可以轻松进行容器间的切换。

 <dependencies>
        <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 或 Undertow 或 Reactor Netty -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-reactor-netty</artifactId>
        </dependency> -->
    </dependencies>

Spring Boot 配置有很多,具体可以参考这里。下面列出一些常见的、与容器相关的配置:

  • 地址:
server.port=8080 # Server HTTP port.
server.address= # Network address to which the server should bind.
  • 压缩:
server.compression.enabled=false # Whether response compression is enabled.
server.compression.excluded-user-agents= # Comma-separated list of user agents for which responses should not be compressed.
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json,application/xml # Comma-separated list of MIME types that should be compressed.
server.compression.min-response-size=2KB # Minimum "Content-Length" value that is required for compression to be performed.
  • 错误:
server.error.include-exception=false # Include the "exception" attribute.
server.error.include-stacktrace=never # When to include a "stacktrace" attribute.
server.error.path=/error # Path of the error controller.
server.error.whitelabel.enabled=true # Whether to enable the default error page displayed in browsers in case of a server error.
  • 其他:
server.connection-timeout= # Time that connectors wait for another HTTP request before closing the connection. When not set, the connector's container-specific default is used. Use a value of -1 to indicate no (that is, an infinite) timeout.
server.http2.enabled=false # Whether to enable HTTP/2 support, if the current environment supports it.
server.max-http-header-size=8KB # Maximum size of the HTTP message header.

我们还可以借助编程的方式来修改这些配置项,即通过实现 WebServerFactoryCustomizer<T> 接口方法:

WebServerFactoryCustomizer

  • 压缩配置
@SpringBootApplication
public class SpringBootConfigurationApplication implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConfigurationApplication.class, args);
    }

    @Override
    public void customize(JettyServletWebServerFactory factory) {
        Compression compression = new Compression();
        compression.setEnabled(true);
        compression.setMinResponseSize(DataSize.ofBytes(512));
        factory.setCompression(compression);
    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券