spring boot中web容器配置

10 篇文章 0 订阅

web容器配置

spring boot 默认的web容器是 tomcat,如果需要换成其他的 web 容器,可以如下配置。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 默认使用的是 tomcat,这里做个排除,使用下面配置的jetty -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
 <!--支持 servlet 的容器有三个 tomcat,jetty,Undertow
     Undertow 是 Red Hat 公司的开源产品, 它完全采用 Java 语言开发,是一款灵活的高性能 Web 服务器,支持阻塞 IO 和非阻塞 IO。
     由于 Undertow 采用 Java 语言开发,可以直接嵌入到 Java 项目中使用。同时, Undertow 完全支持 Servlet 和 Web Socket,
     在高并发情况下表现非常出色。

    Jetty does not yet support Servlet 6.0. To use Jetty with Spring Boot 3.0, 
    you will have to downgrade the Servlet API to 5.0.
    <dependency>
	    <groupId>jakarta.servlet</groupId>
	    <artifactId>jakarta.servlet-api</artifactId>
	    <version>5.0.0</version>
	</dependency>
 -->
<dependency>     
   <groupId>org.springframework.boot</groupId>
   <!--<artifactId>spring-boot-starter-tomcat</artifactId>
   <artifactId>spring-boot-starter-undertow</artifactId>-->
   <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!---->

如果使用的 reactive 的话,tomcat,jetty,Undertow 之外还可以选择 netty。

正常来说,spring boot 会根据是否有spring-boot-starter-web来确定当前是项目是一个web项目(servlet还是reactive),还是一个javase项目,同时还可以在配置文件中指定是否启动web容器,或者容器的类型。
在这里插入图片描述
spring boot 端口配置

server:
  # 指定端口号
  port: 8888
  # 关闭 http 请求
  port: -1
  # 随机端口
  port: 0

如果是随机端口,可以通过自定义监听器来获取端口,然后使用。

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;

public class PortApplicationListener implements ApplicationListener<WebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        System.out.println("event.getWebServer().getPort() = " + event.getWebServer().getPort());
    }
}

配置 spring boot 的相应压缩,一般用不到,实际使用中通过 nginx 来做返回内容的压缩即可。

server:
  port: 8888
  compression:
    enabled: true
    # 大于 2kb 的内容进行压缩
    min-response-size: 2
    # 压缩的文件类型
    mime-types: application/fastsoap
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值