Skip to content

伺服器引擎

為了執行 Ktor 伺服器應用程式,您需要先 建立 並設定伺服器。 伺服器設定包含不同的設定:

  • 一個用於處理網路請求的 引擎
  • 用於存取伺服器的主機和連接埠值。
  • SSL 設定。

支援的平台

下表列出了每個引擎支援的平台:

EnginePlatformsHTTP/2
NettyJVM
JettyJVM
TomcatJVM
CIO (Coroutine-based I/O)JVM, Native, GraalVM, JavaScript, WasmJs✖️
ServletApplicationEngineJVM

新增依賴項

在使用所需的引擎之前,您需要將對應的依賴項新增到您的 建置腳本 中:

  • ktor-server-netty
  • ktor-server-jetty-jakarta
  • ktor-server-tomcat-jakarta
  • ktor-server-cio

以下是為 Netty 新增依賴項的範例:

Kotlin
Groovy
XML

選擇如何建立伺服器

Ktor 伺服器應用程式可以透過 兩種方式建立和執行:使用 embeddedServer 在程式碼中快速傳遞伺服器參數,或使用 EngineMain 從外部的 application.confapplication.yaml 檔案載入設定。

embeddedServer

embeddedServer() 函式接受一個引擎工廠,用於建立特定類型的引擎。在下面的範例中,我們傳遞 Netty 工廠以 Netty 引擎執行伺服器並監聽 8080 連接埠:

kotlin
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main(args: Array<String>) {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!")
            }
        }
    }.start(wait = true)
}

EngineMain

EngineMain 代表一個用於執行伺服器的引擎。您可以使用以下引擎:

  • io.ktor.server.netty.EngineMain
  • io.ktor.server.jetty.jakarta.EngineMain
  • io.ktor.server.tomcat.jakarta.EngineMain
  • io.ktor.server.cio.EngineMain

EngineMain.main 函式用於以選定的引擎啟動伺服器,並載入外部 設定檔 中指定的 應用程式模組。在下面的範例中,我們從應用程式的 main 函式啟動伺服器:

kotlin
package com.example

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

fun Application.module() {
    routing {
        get("/") {
            call.respondText("Hello, world!")
        }
    }
}
shell
ktor {
    deployment {
        port = 8080
    }
    application {
        modules = [ com.example.ApplicationKt.module ]
    }
}
yaml
ktor:
    deployment:
        port: 8080
    application:
        modules:
            - com.example.ApplicationKt.module

如果您需要使用建置系統任務啟動伺服器,您需要將所需的 EngineMain 設定為主類別:

kotlin
application {
    mainClass.set("io.ktor.server.netty.EngineMain")
}
groovy
mainClassName = "io.ktor.server.netty.EngineMain"
xml
<properties>
    <main.class>io.ktor.server.netty.EngineMain</main.class>
</properties>

設定引擎

在本節中,我們將探討如何指定各種引擎特有的選項。

在程式碼中

embeddedServer 函式允許您使用 configure 參數傳遞引擎特有的選項。此參數包含所有引擎通用的選項,並由 ApplicationEngine.Configuration 類別公開。

下面的範例展示了如何使用 Netty 引擎設定伺服器。在 configure 區塊內,我們定義了一個 connector 來指定主機和連接埠,並自訂各種伺服器參數:

kotlin

connectors.add() 方法定義了一個包含指定主機 (127.0.0.1) 和連接埠 (8080) 的連接器。

除了這些選項之外,您還可以設定其他引擎特有的屬性。

Netty

Netty 特有的選項由 NettyApplicationEngine.Configuration 類別公開。

kotlin

Jetty

Jetty 特有的選項由 JettyApplicationEngineBase.Configuration 類別公開。

您可以在 configureServer 區塊內設定 Jetty 伺服器,該區塊提供了對 Server 實例的存取權限。

使用 idleTimeout 屬性指定連線在關閉前可以閒置的時間長度。

kotlin

CIO

CIO 特有的選項由 CIOApplicationEngine.Configuration 類別公開。

kotlin

Tomcat

如果您使用 Tomcat 作為引擎,您可以使用 configureTomcat 屬性來設定它,該屬性提供了對 Tomcat 實例的存取權限。

kotlin

在設定檔中

如果您使用 EngineMain,您可以在 ktor.deployment 群組中指定所有引擎通用的選項。

shell
yaml

Netty

您也可以在設定檔中設定 Netty 特有的選項,在 ktor.deployment 群組中:

shell
yaml