Skip to main content

การใช้ Jetty แทน Tomcat ใน Spring Boot 2

วิธีการใช้ Jetty แทน Tomcat ใน Spring Boot สามารถทำได้โดย

ทำการแก้ไข POM.XML ในส่วนของ spring-boot-web-starter โดยให้ Exclude spring-boot-starter-tomcat และใส่ spring-boot-starter-jetty ลงไป ตามตัวอย่างด้านล่าง

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 <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>

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

จากนั้นสามารถทดสอบรัน Spring Boot App ที่ใช้ Jetty ได้ด้วยคำสั่ง 


> mvn spring-boot:run

ได้ตามปกติ และสามารถถกำหนดค่าของ Address ที่ใช้ได้ในไฟล์ application.properties โดยให้กำหนดค่าดังนี้


1
2
3
4
5
# Set server's listen address
server.address=0.0.0.0

# Set server's listen port
server.port=80

จากตัวอย่างหมายถึงให้ Spring Boot App รอรับการเรียกใช้งานได้จากทุก IP ในเครื่องที่ Port 80 (HTTP)

Comments