티스토리 뷰
Hello, this is Dev_dog.
In this post, We're going to figure out
how to apply and deploy the officially supported embedded server 'UNDERTOW'.
If you find any errors or have any questions, feel free.
한글 포스트는 아래 링크에서 보실 수 있습니다 :
[KR/JAVA] - [SpringBoot] Undertow 적용 및 배포
What is 'undertow'?
- Ultralight & High Performance Web Server
- Provide NIO-based blocking / nonblocking API
- Implemented in Java, available to all JVM-based applications in embedded mode
Development Environment |
Windows 10 STS 3.9.6 jdk 1.8 spring-boot 2.1.6 ORACLE (ojdbc9 11.2.0.3) MyBatis 2.1.0 Lombok Maven |
How to apply 'undertow'
It's easy. Let's modify pom.xml.
Original
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Modified
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude embedded Tomcat server -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
To reflect the modified 'pom.xml',
Right click on the project > Maven > Project Update
(Stop if it's running) Then, run it with Spring Boot App .
You can check the logs like below.
Tomcat → undertow
Original
2019-08-08 17:19:58.494 INFO 18444 --- [ main] tistory.thedev.api.ProductApiApplication : Starting ProductApiApplication on DEV with PID 18444 (D:\workspace_sts\PRODUCT_API\target\classes started by THEDEV in D:\workspace_sts\PRODUCT_API)
2019-08-08 17:19:58.496 DEBUG 18444 --- [ main] tistory.thedev.api.ProductApiApplication : Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-08-08 17:19:58.496 INFO 18444 --- [ main] tistory.thedev.api.ProductApiApplication : No active profile set, falling back to default profiles: default
2019-08-08 17:19:59.136 INFO 18444 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-08 17:19:59.136 INFO 18444 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-08 17:19:59.195 INFO 18444 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-08 17:19:59.626 INFO 18444 --- [ main] tistory.thedev.api.ProductApiApplication : Started ProductApiApplication in 1.389 seconds (JVM running for 1.798)
Modified
2019-08-08 16:04:05.969 INFO 24268 --- [ main] tistory.thedev.api.ProductApiApplication : Starting ProductApiApplication on DEV with PID 24268 (D:\workspace_sts\PRODUCT_API\target\classes started by THEDEV in D:\workspace_sts\PRODUCT_API)
2019-08-08 16:04:06.585 DEBUG 24268 --- [ main] tistory.thedev.api.ProductApiApplication : Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-08-08 16:04:06.586 INFO 24268 --- [ main] tistory.thedev.api.ProductApiApplication : No active profile set, falling back to default profiles: default
2019-08-08 16:04:07.183 WARN 24268 --- [ main] io.undertow.websockets.jsr : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2019-08-08 16:04:07.201 INFO 24268 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext
2019-08-08 16:04:07.618 INFO 24268 --- [ main] org.xnio : XNIO version 3.3.8.Final
2019-08-08 16:04:07.625 INFO 24268 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
2019-08-08 16:04:07.696 INFO 24268 --- [ main] tistory.thedev.api.ProductApiApplication : Started ProductApiApplication in 1.994 seconds (JVM running for 5.839)
2019-08-08 16:05:24.974 INFO 24268 --- [ XNIO-1 task-1] io.undertow.servlet : Initializing Spring DispatcherServlet 'dispatcherServlet'
How to deploy 'undertow'
Add maven-shade-plugin to make it a runnable jar.
Origianl
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Modified
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--Add maven-shade-plugin for a runnable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Reflect the modified pom.xml in the following order.
Right-click Project> Maven> Project Update
Create a runnable jar in the following way:
Right-click Project> Run As> Run Configurations> Right-click Maven Build> New configuration
Find Base difrectory: Workspace ... and register your current project
Goals: clean package
Apply> Run
If you check the target folder in the actual project directory, a jar file is created.
In my case, D:\workspace_sts\PRODUCT_API\target\PRODUCT_API-0.0.1-SNAPSHOT.jar
Local Test
Move only the jar file to another directory.
using PowerShell
Click in the directory where the jar is located > Open PowerShell here
java -jar .\PRODUCT_API-0.0.1-SNAPSHOT.jar
using Command(cmd)
Windows Key+R > cmd > move to the directory where the jar is located
java -jar PRODUCT_API-0.0.1-SNAPSHOT.jar
After it runs, you can test your API using browsers, Postman, etc...
http://localhost:8080/products?pubNum=08888881&pubNum=08888882
This post was written with the following references.
https://zepinos.tistory.com/35
https://www.daleseo.com/spring-boot-embedded-server-change/
https://www.baeldung.com/jboss-undertow