SpringBoot使用Undertow代替Tomcat

Undertow使用

spring boot內嵌容器默認爲tomcat,想要換成undertow,非常容易,只需修改spring-boot-starter-web依賴,移除tomcat的依賴:

 <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>  

然後,添加undertow依賴

    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-undertow</artifactId>  
    </dependency>  

這樣即可,使用默認參數啓動undertow服務器。

參數設置

server:  
    port: 8084  
    http2:  
        enabled: true  
    undertow:  
        io-threads: 16  
        worker-threads: 256  
        buffer-size: 1024  
        buffers-per-region: 1024  
        direct-buffers: true 

io-threads:IO線程數, 它主要執行非阻塞的任務,它們會負責多個連接,默認設置每個CPU核心一個線程,不可設置過大,否則啓動項目會報錯:打開文件數過多。

worker-threads:阻塞任務線程池,當執行類似servlet請求阻塞IO操作,undertow會從這個線程池中取得線程。它的值取決於系統線程執行任務的阻塞係數,默認值是 io-threads*8

以下配置會影響buffer,這些buffer會用於服務器連接的IO操作,有點類似netty的池化內存管理。

buffer-size:每塊buffer的空間大小,越小的空間被利用越充分,不要設置太大,以免影響其他應用,合適即可

buffers-per-region:每個區分配的buffer數量,所以pool的大小是buffer-size * buffers-per-region

direct-buffers:是否分配的直接內存(NIO直接分配的堆外內存)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章