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

Popular posts from this blog

Serialize and Deserialize JSON data with C#

This post is about example for serialize and deserialize json data with C#. This example using Newtonsoft.Json to be a library to work with json data. You can add Newtonsoft.Json by download from  https://www.newtonsoft.com/json  or using Nuget to add it into your project. In this example is Bill object that hold billing data about Car object. Serialize Object to String Below is example code to serialize object to json string. At line 39 is code to convert object into json string with beautiful json format. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace JSON { class JSonExample { static void Main( string [] args) { List<Car> cars = new List<Car>(); float to...

Make a scheduling job in Spring Boot

You can make a class to run as a schedule job in Spring Boot. By using @EnableSchedule in your project. Below is example code to enable schedule in Spring Boot. Declare Spring Boot application to support schedule By declare @EnableSchedule at the main class (see at line 10). This enables detection of @ Scheduled annotations on any Spring-managed bean in the container. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.myexample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @SpringBootApplication @EnableScheduling public class MyExampleApplication { public static void main(String[] args) { SpringApplication.run(MyExampleApplication.class, args); } } Declare schedule configuration at...

Install Spring Boot application as a Windows services.

I using  WinSW  to be wrapper for Spring Boot application to run as a Windows service (following section 61.3 of Spring Boot document). There few easy step to setup. Download WinSW binary distribution from website  https://github.com/kohsuke/winsw/releases Copy WinSW.exe into Spring Boot application folder (ex: my file is WinSW.Net4.exe) Rename your WinSW.exe to same as your jar file (for easy to remember). Create XML file name same as jar file. This file is using for configuration of Windows services. Put configuration for services wrapper in your xml file. 1 2 3 4 5 6 7 8 9 <?xml version="1.0" encoding="UTF-8"?> <service> <id>my-application-0.0.1</id> <name>my-application-0.0.1</name> <description>My Exaple Spring Boot Services</description> <executable>java</executable> <arguments>-jar -Xmx1024M -Xms128M "my-application-0.0.1.jar"</arguments> <logmode>rotate...