springboot容器配置源码解读

springboot内嵌了三个容器,tomcat(默认),jetty,undertow;如何替换容器可以见这篇文章 springboot将tomcat容器换为jetty容器 该文只从源码上面讨论容器的配置
相关包为spring-boot-qutoconfigure:2.4.3

第一个入口类为:
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration

该类会加载配置类:ServerProperties

若是使用默认tomcat容器,则会创建TomcatWebServerFactoryCustomizer这个bean

ServerProperties类里面有三个非常重要的内部类Jetty,Tomcat,Undertow三个类;该类部分代码如下:

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

    /**
     * Server HTTP port.
     */
    private Integer port;

    /**
     * Network address to which the server should bind.
     */
    private InetAddress address;

    @NestedConfigurationProperty
    private final ErrorProperties error = new ErrorProperties();

    /**
     * Strategy for handling X-Forwarded-* headers.
     */
    private ForwardHeadersStrategy forwardHeadersStrategy;

    /**
     * Value to use for the Server response header (if empty, no header is sent).
     */
    private String serverHeader;

    /**
     * Maximum size of the HTTP message header.
     */
    private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8);

    /**
     * Type of shutdown that the server will support.
     */
    private Shutdown shutdown = Shutdown.IMMEDIATE;

    @NestedConfigurationProperty
    private Ssl ssl;

    @NestedConfigurationProperty
    private final Compression compression = new Compression();

    @NestedConfigurationProperty
    private final Http2 http2 = new Http2();

    private final Servlet servlet = new Servlet();

    private final Tomcat tomcat = new Tomcat();

    private final Jetty jetty = new Jetty();

    private final Netty netty = new Netty();

    private final Undertow undertow = new Undertow();
}

配置以server.开头,然后接上属性名,如下所示:
server.port 配置启动端口

默认容器Tomcat内部类代码如下:

    public static class Tomcat {

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

        /**
         * Thread related configuration.
         */
        private final Threads threads = new Threads();

        /**
         * Tomcat base directory. If not specified, a temporary directory is used.
         */
        private File basedir;

        /**
         * Delay between the invocation of backgroundProcess methods. If a duration suffix
         * is not specified, seconds will be used.
         */
        @DurationUnit(ChronoUnit.SECONDS)
        private Duration backgroundProcessorDelay = Duration.ofSeconds(10);

        /**
         * Maximum size of the form content in any HTTP post request.
         */
        private DataSize maxHttpFormPostSize = DataSize.ofMegabytes(2);

        /**
         * Maximum amount of request body to swallow.
         */
        private DataSize maxSwallowSize = DataSize.ofMegabytes(2);

        /**
         * Whether requests to the context root should be redirected by appending a / to
         * the path. When using SSL terminated at a proxy, this property should be set to
         * false.
         */
        private Boolean redirectContextRoot = true;

        /**
         * Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
         * will use relative or absolute redirects.
         */
        private boolean useRelativeRedirects;

        /**
         * Character encoding to use to decode the URI.
         */
        private Charset uriEncoding = StandardCharsets.UTF_8;

        /**
         * 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 = 8192;

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

        /**
         * Maximum number of idle processors that will be retained in the cache and reused
         * with a subsequent request. When set to -1 the cache will be unlimited with a
         * theoretical maximum size equal to the maximum number of connections.
         */
        private int processorCache = 200;

        /**
         * Comma-separated list of additional patterns that match jars to ignore for TLD
         * scanning. The special '?' and '*' characters can be used in the pattern to
         * match one and only one character and zero or more characters respectively.
         */
        private List<String> additionalTldSkipPatterns = new ArrayList<>();

        /**
         * Comma-separated list of additional unencoded characters that should be allowed
         * in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
         */
        private List<Character> relaxedPathChars = new ArrayList<>();

        /**
         * Comma-separated list of additional unencoded characters that should be allowed
         * in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
         */
        private List<Character> relaxedQueryChars = new ArrayList<>();

        /**
         * Amount of time the connector will wait, after accepting a connection, for the
         * request URI line to be presented.
         */
        private Duration connectionTimeout;

        /**
         * Static resource configuration.
         */
        private final Resource resource = new Resource();

        /**
         * Modeler MBean Registry configuration.
         */
        private final Mbeanregistry mbeanregistry = new Mbeanregistry();

        /**
         * Remote Ip Valve configuration.
         */
        private final Remoteip remoteip = new Remoteip();

}

默认参数基本都够用了,其中处理请求线程的配置Threads代码如下:

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

    /**
    * Maximum amount of worker threads.
    */
    private int max = 200;

    /**
    * Minimum amount of worker threads.
    */
    private int minSpare = 10;
}

若是网站并发较高,可以调max大小,
server.tomcat.threads.max = 300

总结:
1,配置都在ServerProperties这个类中
2,配置以server.开头,加类的属性名即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342