前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 内嵌 Web 容器介绍及常见运行参数

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

作者头像
happyJared
发布2019-05-17 12:12:12
1K0
发布2019-05-17 12:12:12
举报
文章被收录于专栏:happyJaredhappyJared

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

代码语言:javascript
复制
 <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 配置有很多,具体可以参考这里。下面列出一些常见的、与容器相关的配置:

  • 地址:
代码语言:javascript
复制
server.port=8080 # Server HTTP port.
server.address= # Network address to which the server should bind.
  • 压缩:
代码语言:javascript
复制
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.
  • 错误:
代码语言:javascript
复制
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.
  • 其他:
代码语言:javascript
复制
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

  • 压缩配置
代码语言:javascript
复制
@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);
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档