Ktor?

Ktor는 강력한 Kotlin 프로그래밍 언어를 사용하여 비동기 서버 및 클라이언트를 구축하기 위한 프레임워크
웹 애플리케이션을 쉽게 만들 수 있도록 설계되어있다.
kotlin 베이스 인것을 봐서 유추해볼 수 있겠지만 해당 프레임워크를 만든 곳이 바로 JetBrain..

Ktor에 관심이 생긴 이유

  • 독점적인 프레임워크로 스프링이 이미 있지만, kotlin 베이스의 경량화 프레임워크인 ktor을 사용해 보고 싶었음.

  • 기본적으로 비동기 처리 (spring reactor/webflux 와 얼마나 차이점이 있는지.. 궁금했음)

  • 커스터마이징 가능 (spring은 사실상 프레임워크 자체는 커스터마이징이 어려운 구조여서.. ktor은 자유도가 다른가? 궁금했음)

  • Kotlin과 쉽게 사용 가능 (java / kotlin 모두 사용하지만 kotlin 위주로 개발을 더 많이 해보고 싶음)

Ktor 프로젝트 생성 방법

다음 단계에 따라 간단한 Ktor 프로젝트를 생성:

1단계: Gradle 설정

프로젝트를 위한 새 디렉토리를 만들고 다음 build.gradle.kts 파일을 추가:

plugins {
    kotlin("jvm") version "1.5.21"
    id("io.ktor.plugin") version "1.6.3"
}
 
group = "com.example"
version = "0.0.1"
 
application {
    mainClass.set("com.example.ApplicationKt")
}
 
repositories {
    mavenCentral()
}
 
dependencies {
    implementation("io.ktor:ktor-server-core:1.6.3")
    implementation("io.ktor:ktor-server-netty:1.6.3")
    testImplementation("io.ktor:ktor-server-tests:1.6.3")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.5.21")
}

2단계: 애플리케이션 컨트롤러(main) 생성

src/main/kotlin/com/example 디렉토리에 Application.kt 파일을 생성:

package com.example
 
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
 
fun main() {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                call.respondText("Hello, Ktor!", ContentType.Text.Plain)
            }
        }
    }.start(wait = true)
}

3단계: 애플리케이션 실행

다음 명령어를 사용하여 애플리케이션을 실행:

./gradlew run

다음과 같은 출력을 확인해볼 수 있다.:

> Task :run
[main] INFO Application - Responding at http://0.0.0.0:8080

우선적인 프로젝트 create는 위의 과정을 끝.

조만간 Ktor와 Nuxt.js를 결합한 토이 프로젝트를 진행해볼 예정..