springboot容器配置源碼解讀

springboot內(nèi)嵌了三個容器,tomcat(默認(rèn))德崭,jetty猖毫,undertow;如何替換容器可以見這篇文章 springboot將tomcat容器換為jetty容器 該文只從源碼上面討論容器的配置
相關(guān)包為spring-boot-qutoconfigure:2.4.3

第一個入口類為:
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration

該類會加載配置類:ServerProperties

若是使用默認(rèn)tomcat容器沛励,則會創(chuàng)建TomcatWebServerFactoryCustomizer這個bean

ServerProperties類里面有三個非常重要的內(nèi)部類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 配置啟動端口

默認(rèn)容器Tomcat內(nèi)部類代碼如下:

    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();

}

默認(rèn)參數(shù)基本都夠用了,其中處理請求線程的配置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;
}

若是網(wǎng)站并發(fā)較高胁赢,可以調(diào)max大小企蹭,
server.tomcat.threads.max = 300

總結(jié):
1,配置都在ServerProperties這個類中
2智末,配置以server.開頭练对,加類的屬性名即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吹害,隨后出現(xiàn)的幾起案子螟凭,更是在濱河造成了極大的恐慌,老刑警劉巖它呀,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件螺男,死亡現(xiàn)場離奇詭異,居然都是意外死亡纵穿,警方通過查閱死者的電腦和手機下隧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谓媒,“玉大人淆院,你說我怎么就攤上這事【涔撸” “怎么了土辩?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵支救,是天一觀的道長。 經(jīng)常有香客問我拷淘,道長各墨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任启涯,我火速辦了婚禮贬堵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘结洼。我一直安慰自己黎做,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布松忍。 她就那樣靜靜地躺著蒸殿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挽铁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天敞掘,我揣著相機與錄音叽掘,去河邊找鬼。 笑死玖雁,一個胖子當(dāng)著我的面吹牛更扁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赫冬,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼浓镜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了劲厌?” 一聲冷哼從身側(cè)響起膛薛,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎补鼻,沒想到半個月后哄啄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡风范,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年咨跌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硼婿。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡锌半,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出寇漫,到底是詐尸還是另有隱情刊殉,我是刑警寧澤殉摔,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站冗澈,受9級特大地震影響钦勘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜亚亲,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一彻采、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捌归,春花似錦肛响、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至巾兆,卻和暖如春猎物,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背角塑。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工蔫磨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人圃伶。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓堤如,卻偏偏與公主長得像,于是被迫代替她去往敵國和親窒朋。 傳聞我的和親對象是個殘疾皇子搀罢,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,937評論 2 361