资讯专栏INFORMATION COLUMN

Spring Boot内嵌的Web容器

megatron / 3342人阅读

摘要:内嵌容器主要有三种默认容器在中,有关内嵌的默认属性配置如下,前缀为参考官方文档另外还可以通过源码来查看的相关配置属性,位于包下中,部分源码如下

Spring Boot内嵌Web容器主要有三种:

tomcat(默认)

jetty

undertow

1. tomcat容器

在spring boot中,有关内嵌tomcat的默认属性配置如下,前缀为 server.tomcat (参考官方文档):

server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=10s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.
server.tomcat.internal-proxies=10.d{1,3}.d{1,3}.d{1,3}|
        192.168.d{1,3}.d{1,3}|
        169.254.d{1,3}.d{1,3}|
        127.d{1,3}.d{1,3}.d{1,3}|
        172.1[6-9]{1}.d{1,3}.d{1,3}|
        172.2[0-9]{1}.d{1,3}.d{1,3}|
        172.3[0-1]{1}.d{1,3}.d{1,3}
        0:0:0:0:0:0:0:1
        ::1 # Regular expression that matches proxies that are to be trusted.
server.tomcat.max-connections=10000 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-post-size=2MB # Maximum size of the HTTP post content.
server.tomcat.max-swallow-size=2MB # Maximum amount of request body to swallow.
server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.allow-caching=true # Whether static resource caching is permitted for this web application.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.

另外还可以通过源码来查看tomcat的相关配置属性,位于org.springframework.boot.autoconfigure包下org.springframework.boot.autoconfigure.web.ServerProperties 中,部分源码如下:

    /**
     * Tomcat properties.
     */
    public static class Tomcat {

        /**
         * Access log configuration.
         */
        private final Accesslog accesslog = new Accesslog();

        /**
         * Regular expression that matches proxies that are to be trusted.
         */
        private String internalProxies = "10.d{1,3}.d{1,3}.d{1,3}|" // 10/8
                + "192.168.d{1,3}.d{1,3}|" // 192.168/16
                + "169.254.d{1,3}.d{1,3}|" // 169.254/16
                + "127.d{1,3}.d{1,3}.d{1,3}|" // 127/8
                + "172.1[6-9]{1}.d{1,3}.d{1,3}|" // 172.16/12
                + "172.2[0-9]{1}.d{1,3}.d{1,3}|"
                + "172.3[0-1]{1}.d{1,3}.d{1,3}|" //
                + "0:0:0:0:0:0:0:1|::1";
        
        .......(省略)
        
        /**
         * Maximum amount of worker threads.
         */
        private int maxThreads = 200;

        /**
         * Minimum amount of worker threads.
         */
        private int minSpareThreads = 10;
        
        .......(省略)
        
        /**
         * Maximum number of connections that the server accepts and processes at any
         * given time. Once the limit has been reached, the operating system may still
         * accept connections based on the "acceptCount" property.
         */
        private int maxConnections = 10000;

        /**
         * Maximum queue length for incoming connection requests when all possible request
         * processing threads are in use.
         */
        private int acceptCount = 100;
        
        .......(省略)
    }
        

在默认情况下,经常会关注的几个属性值:

server.tomcat.max-threads=200:最大工作线程数是200

server.tomcat.min-spare-threads=10:最小工作线程数是10

server.tomcat.max-connections = 10000:最大连接数为10000

如果需要覆盖默认的配置,可以在application.properties或者application.yml中指定相关属性值

server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 20
        min-spare-threads: 5
        max-connections: 1000
2. jetty容器

有关jetty的默认属性配置如下:

server.jetty.acceptors=-1 # Number of acceptor threads to use. When the value is -1, the default, the number of acceptors is derived from the operating environment.
server.jetty.accesslog.append=false # Append to log.
server.jetty.accesslog.date-format=dd/MMM/yyyy:HH:mm:ss Z # Timestamp format of the request log.
server.jetty.accesslog.enabled=false # Enable access log.
server.jetty.accesslog.extended-format=false # Enable extended NCSA format.
server.jetty.accesslog.file-date-format= # Date format to place in log file name.
server.jetty.accesslog.filename= # Log filename. If not specified, logs redirect to "System.err".
server.jetty.accesslog.locale= # Locale of the request log.
server.jetty.accesslog.log-cookies=false # Enable logging of the request cookies.
server.jetty.accesslog.log-latency=false # Enable logging of request processing time.
server.jetty.accesslog.log-server=false # Enable logging of the request hostname.
server.jetty.accesslog.retention-period=31 # Number of days before rotated log files are deleted.
server.jetty.accesslog.time-zone=GMT # Timezone of the request log.
server.jetty.max-http-post-size=200000B # Maximum size of the HTTP post or put content.
server.jetty.selectors=-1 # Number of selector threads to use. When the value is -1, the default, the number of selectors is derived from the operating environment.

默认情况下,spring-boot-starter-web 使用的容器为 tomcat,如果想要替换成jetty,只需要在pom文件中移除tomcat依赖,然后引入jetty依赖即可

    
        org.springframework.boot
        spring-boot-starter-web
        
        
            
                org.springframework.boot
                spring-boot-starter-tomcat
            
        
    

    
    
        org.springframework.boot
        spring-boot-starter-jetty
    
3. undertow容器

有关内嵌的undertow的默认属性配置如下:

server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
server.undertow.accesslog.prefix=access_log. # Log file name prefix.
server.undertow.accesslog.rotate=true # Whether to enable access log rotation.
server.undertow.accesslog.suffix=log # Log file name suffix.
server.undertow.buffer-size= # Size of each buffer.
server.undertow.direct-buffers= # Whether to allocate buffers outside the Java heap. The default is derived from the maximum amount of memory that is available to the JVM.
server.undertow.eager-filter-init=true # Whether servlet filters should be initialized on startup.
server.undertow.io-threads= # Number of I/O threads to create for the worker. The default is derived from the number of available processors.
server.undertow.max-http-post-size=-1B # Maximum size of the HTTP post content. When the value is -1, the default, the size is unlimited.
server.undertow.worker-threads= # Number of worker threads. The default is 8 times the number of I/O threads.

主要关注的两个属性:

server.undertow.io-threads:默认情况下IO线程数等于CPU处理器个数

server.undertow.worker-threads:默认情况下Worker线程数等于IO线城数的8倍

同上,如果想要使用undertow容器,只需要在pom文件中移除tomcat依赖,然后引入undertow依赖即可

    
        org.springframework.boot
        spring-boot-starter-web
        
        
            
                org.springframework.boot
                spring-boot-starter-tomcat
            
        
    

    
    
        org.springframework.boot
        spring-boot-starter-undertow
    

参考资料:

https://docs.spring.io/spring...

https://github.com/spring-pro...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/35633.html

相关文章

  • 如何优雅关闭 Spring Boot 应用

    摘要:除了,还有十余种,有的是特定操作,比如转储内存日志有的是信息展示,比如显示应用健康状态。 showImg(http://ww1.sinaimg.cn/large/006tNc79gy1g5qb2coyfoj30u00k0tan.jpg); 前言 随着线上应用逐步采用 SpringBoot 构建,SpringBoot应用实例越来多,当线上某个应用需要升级部署时,常常简单粗暴地使用 kil...

    xiyang 评论0 收藏0
  • spring boot - 收藏集 - 掘金

    摘要:引入了新的环境和概要信息,是一种更揭秘与实战六消息队列篇掘金本文,讲解如何集成,实现消息队列。博客地址揭秘与实战二数据缓存篇掘金本文,讲解如何集成,实现缓存。 Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控 - 掘金Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...

    rollback 评论0 收藏0
  • Maven管理SpringBoot Profile

    摘要:的配置文件默认为或,此外仅以配置为说明。的由的标签管理。管理由于构建是基于或,此处仅以说明。管理分五步,以下详细介绍。并且为表示,会将文件内容的替换为相应的变量如文件中的会替换为属性值。 1. Spring Profile Spring可使用Profile决定程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Spring的Profile一般项目包含:dev(开发), test...

    wenzi 评论0 收藏0
  • 从Tomcat到Spring Boot

    摘要:暮夏八月是一年中最好的时节,近近地看到了凉爽的希望,却还能享用暖热的余温。距离发布已经年有余,我们尝试在这个夏天把这只已经独自在外游荡了年的野猫装入春天的长靴。总结以上就是从迁移到所需要的所有改动。 暮夏八月是一年中最好的时节,近近地看到了凉爽的希望,却还能享用暖热的余温。距离Phil Webb发布Spring Boot已经4年有余,我们尝试在这个夏天把这只已经独自在外游荡了19年的野...

    NoraXie 评论0 收藏0
  • 从Tomcat到Spring Boot

    摘要:暮夏八月是一年中最好的时节,近近地看到了凉爽的希望,却还能享用暖热的余温。距离发布已经年有余,我们尝试在这个夏天把这只已经独自在外游荡了年的野猫装入春天的长靴。总结以上就是从迁移到所需要的所有改动。 暮夏八月是一年中最好的时节,近近地看到了凉爽的希望,却还能享用暖热的余温。距离Phil Webb发布Spring Boot已经4年有余,我们尝试在这个夏天把这只已经独自在外游荡了19年的野...

    hikui 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<