Spring Boot中三款内嵌容器的使用

amang2000
发布于 2022-4-28 18:01
浏览
0收藏

做Spring Boot的盆友应该都知道,Spring Boot支持三种内嵌容器,有了这几个容器,我们就不需要打成War包再去部署,而是直接执行java -jar jar包即可启动,真是太方便了。

Tomcat

在我们使用Spring Boot开发WebApi时,会引入spring-boot-starter-web这个starter组件,其自带了Tomcat容器,Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

所以我们平时新建项目启动起来,会看见Tomcat相关的一些信息。Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

tomcat常用参数:

  • server.tomcat.uri-encoding:Tomcat 请求编码
  • server.tomcat.threads.max:Tomcat 的最大线程数
  • server.tomcat.basedir:Tomcat 运行日志和临时文件的目录。若不配置,则默认使用系统的临时目录。
  • server.tomcat.max-connections: 最大连接数
  • server.tomcat.accept-count: 超过该请求数则排队
  • server.tomcat.threads.min-spare: 最小工作空闲线程数
  • server.tomcat.max-http-form-post-size: Post数据大小

当然,还有很多其他的参数,有需要的可以了解一下Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

Jetty

因为spring-boot-starter-web自带了Tomcat,所以我们要使用其它的容器的话,需要将其依赖包排除掉并重新引入新容器的starter

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区我们还需要去掉原有的Tomcat配置,然后启动Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

常用参数:

  • server.jetty.threads.max: 最大线程数
  • server.jetty.threads.min: 最小线程数
  • server.jetty.threads.max-queue-capacity: 最大队列容量
  • server.jetty.threads.idle-timeout: 线程最大空闲时间

Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

Undertow

使用Undertow的方式跟Jetty的使用方式一样,需要先排除掉默认依赖,再引入Undertow Starter

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

 

Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

 

去掉其他容器的配置,启动我们的程序,就可以看到Undertow的相关信息Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

常用参数:

  • server.undertow.url-charset: 请求编码
  • server.undertow.threads.io: IO线程
  • server.undertow.threads.worker: 工作线程

Spring Boot中三款内嵌容器的使用-鸿蒙开发者社区

听网友说这三个的性能是这样排名的:Undertow>Jetty>Tomcat,但是我没亲自去压测过,所以不确定其真假。这三个,只有Tomcat用的最多,Undertow也用过几次,但是项目没什么并发可言,在性能上,也没有去实际对比过,后面有机会涉及到性能优化的时候再来深究吧。

各位盆友,你们是用的哪个容器呢?

收藏
回复
举报
回复
    相关推荐