Spring Boot Undertow - Spring Boot 168 EP 6-1

Spring Boot Undertow – Spring Boot 168 EP 6-1

Spring Boot Undertow 提供了另一種選擇,可以取代 Tomcat 內嵌 Server , Spring Boot 168 EP 6-1 增加了 Undertow 的相依套件,並排除 Tomcat 套件。

前言

Undertow 採用 Java 語言開發,是一款靈活的高性能 Web 服務器,支持阻塞 IO 和非阻塞 IO,也是 Wildfly 的預設 Web 容器。

Spring Boot Undertow

檔案目錄

./
   +- build.gradle
       +- src
           +- main
               +- resources
               |   +- application.properties

Gradle

build.gradle

增加 Undertow ,排除 Tomcat 。

修改完後,點右鍵,Gradle -> Refresh Gradle Project 。

buildscript {
	group 'org.ruoxue.spring-boot-168'
	version = '0.0.1-SNAPSHOT'
	ext {
            springBootVersion = '2.1.7.RELEASE'
	}
}

plugins {
    id 'java-library'
}

repositories {
    jcenter()
}

configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:27.0.1-jre'
    implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
	
	implementation "org.springframework.boot:spring-boot-starter-undertow:${springBootVersion}"

    testImplementation 'junit:junit:4.12'
}

設定

application.properties

增加 Undertow 設定。

server.undertow.accesslog.dir=
server.undertow.accesslog.enabled=false 
server.undertow.accesslog.pattern=common
server.undertow.accesslog.prefix=access
server.undertow.accesslog.suffix=log
server.undertow.max-http-post-size=5000
server.undertow.io-threads=12
server.undertow.worker-threads=20
server.undertow.buffer-size=1024
server.undertow.buffers-per-region=1024
server.undertow.direct-buffers=true

測試

Application.main

在主程式點右鍵執行 Run As -> Java Application,查看 console,Server 已成功啟動,監聽 10000 port。

Spring Boot Web 執行主程式
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

[GC (Metadata GC Threshold)  245269K->23302K(4024320K), 0.0322055 secs]
[Full GC (Metadata GC Threshold)  23302K->22573K(4024320K), 0.0628815 secs]
2022-11-17T15:50:22.702+0800 [main] WARN StartupInfoLogger#appendOn:117 - InetAddress.getLocalHost().getHostName() took 5003 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2022-11-17T15:50:17.670+0800 [main] INFO Application#logStarting:55 - Starting Application using Java 1.8.0_331 on chengdeMacBook-Pro.local with PID 67343 (/Users/cheng/Documents/dev/gitlab/openyu/example/bin/main started by cheng in /Users/cheng/Documents/dev/gitlab/openyu/example)
2022-11-17T15:50:27.744+0800 [main] INFO Application#logStartupProfileInfo:634 - No active profile set, falling back to 1 default profile: "default"
2022-11-17T15:50:29.497+0800 [main] INFO RepositoryConfigurationDelegate#multipleStoresDetected:262 - Multiple Spring Data modules found, entering strict repository configuration mode!
2022-11-17T15:50:29.500+0800 [main] INFO RepositoryConfigurationDelegate#registerRepositoriesIn:132 - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-11-17T15:50:29.531+0800 [main] INFO RepositoryConfigurationDelegate#registerRepositoriesIn:201 - Finished Spring Data repository scanning in 18 ms. Found 0 Redis repository interfaces.
[GC (Metadata GC Threshold)  254216K->41317K(4024320K), 0.0307554 secs]
[Full GC (Metadata GC Threshold)  41317K->32033K(4024320K), 0.0713360 secs]
2022-11-17T15:50:30.414+0800 [main] WARN jsr#handleDeployment:68 - UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2022-11-17T15:50:30.441+0800 [main] INFO servlet#log:382 - Initializing Spring embedded WebApplicationContext
2022-11-17T15:50:30.441+0800 [main] INFO ServletWebServerApplicationContext#prepareWebApplicationContext:292 - Root WebApplicationContext: initialization completed in 2583 ms
2022-11-17T15:50:31.760+0800 [main] WARN config#setReconnectionTimeout:278 - 'reconnectionTimeout' setting in unavailable. Please use 'failedSlaveReconnectionInterval' setting instead!
2022-11-17T15:50:31.760+0800 [main] WARN config#setFailedAttempts:287 - 'failedAttempts' setting in unavailable. Please use 'failedSlaveCheckInterval' setting instead!
2022-11-17T15:50:31.891+0800 [main] INFO Version#logVersion:41 - Redisson 3.11.5
2022-11-17T15:50:31.963+0800 [main] WARN DnsServerAddressStreamProviders#<clinit>:70 - Can not find io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider in the classpath, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS.
2022-11-17T15:50:37.325+0800 [redisson-netty-2-3] INFO MasterPubSubConnectionPool#lambda$run$0:168 - 1 connections initialized for 127.0.0.1/127.0.0.1:6379
2022-11-17T15:50:37.325+0800 [redisson-netty-2-2] INFO MasterConnectionPool#lambda$run$0:168 - 32 connections initialized for 127.0.0.1/127.0.0.1:6379
2022-11-17T15:50:38.177+0800 [main] INFO undertow#start:120 - starting server: Undertow - 2.2.17.Final
2022-11-17T15:50:38.191+0800 [main] INFO xnio#<clinit>:95 - XNIO version 3.8.6.Final
2022-11-17T15:50:38.212+0800 [main] INFO nio#<clinit>:58 - XNIO NIO Implementation Version 3.8.6.Final
2022-11-17T15:50:38.253+0800 [main] INFO threads#<clinit>:52 - JBoss Threads version 3.1.0.Final
2022-11-17T15:50:38.322+0800 [main] INFO UndertowWebServer#start:119 - Undertow started on port(s) 10084 (http)
2022-11-17T15:50:38.569+0800 [main] INFO servlet#log:382 - Initializing Spring embedded WebApplicationContext
2022-11-17T15:50:38.570+0800 [main] INFO ServletWebServerApplicationContext#prepareWebApplicationContext:292 - Root WebApplicationContext: initialization completed in 234 ms
2022-11-17T15:50:38.593+0800 [main] INFO EndpointLinksResolver#<init>:58 - Exposing 14 endpoint(s) beneath base path ''
2022-11-17T15:50:38.688+0800 [main] INFO undertow#start:120 - starting server: Undertow - 2.2.17.Final
2022-11-17T15:50:38.696+0800 [main] INFO UndertowWebServer#start:119 - Undertow started on port(s) 11084 (http)
2022-11-17T15:50:38.719+0800 [main] INFO Application#logStarted:61 - Started Application in 31.931 seconds (JVM running for 38.205)

心得分享

在高併發系統中,Tomcat 相對來說比較弱,在相同的機器配置下,模擬相等的請求數,Undertow 在性能和內存使用方面表現是比較好的。

發佈留言