Kotlin - spring - server demo

XYMON·2023년 6월 1일
0

코틀린

목록 보기
7/7

Kotlin spring server demo - reactive/webflux server to handle i/o. (Tomcat -> Netty) + grpc client.

Kotlin

dependencies
build.gradle.kts - dependencies

implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinXCoroutineVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlinXCoroutineVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:$kotlinXCoroutineVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinXSerializationVersion")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions:1.2.2")
testImplementation("io.projectreactor:reactor-test:3.5.6")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlinXCoroutineVersion")
  • kotlin-reflect : basic set
  • coroutines-core/jdk8/reactor : These are essential for wrking with Kotlin coroutines. (Core func, integ with Java 9's CompletableFuture API and Project Reactor). Netty doesn't have direct dependency on coroutines.
  • serialization-json: This allows app to serialize and deserialize Kotlin objects to/from JSON format.
  • io.projectreactor/* : These provide Kotlin extensions and testing utilities for project reactor(reactive programming library)

Spring

dependencies

annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework:spring-context")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("org.springframework.boot:spring-boot-starter-test")
  • spring-boot-configuration-processor : Generates metadata and configuration information. Provides features like auto-configuration, property binding, and documentation for application properties.
  • spring-context : Fundamental component of the Spring framework that provides support for dependency injection, lifecycle management, and configuration of Spring beans.
  • spring-boot-starter-actuator : Adds endpoints to your application that expose information about health, metrics, configurations, and other useful details.
  • spring-boot-starter-webflux : non-blocking, event-driven web framework built on Reactive Streams

Armeria

Armeria - light msa framework on Netty, grpc, thrift. RestAPI/gRPC/Throft can be supported on single port.
-> maybe just webflux is okay?
dependencies

// Armeria
implementation(platform("com.linecorp.armeria:armeria-bom:1.19.0"))
implementation(platform("io.netty:netty-bom:4.1.79.Final"))
implementation("com.linecorp.armeria:armeria-kotlin")
implementation("com.linecorp.armeria:armeria-spring-boot2-webflux-starter")
testImplementation("com.ninja-squad:springmockk:3.1.1")

Grpc

dependencies

//GRPC
implementation("com.linecorp.armeria:armeria-grpc-protocol")
implementation("com.linecorp.armeria:armeria-grpc")
implementation("com.google.protobuf:protobuf-java:${protobufVersion}")
implementation("com.google.protobuf:protobuf-kotlin:${protobufVersion}")
implementation("io.grpc:grpc-protobuf:${grpcVersion}")
implementation("io.grpc:grpc-stub:${grpcVersion}")
implementation("io.grpc:grpc-netty-shaded:${grpcVersion}")
api("io.grpc:grpc-kotlin-stub:${grpcKotlinVersion}")

others

buildscript {
	dependencies {
		classpath("com.google.protobuf:protobuf-gradle-plugin:0.9.3")
	}
}
...
protobuf {
	protoc {
		artifact = "com.google.protobuf:protoc:${protobufVersion}"
	}
	plugins {
		id("grpc") {
			artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
		}
		id("grpckt") {
			artifact = "io.grpc:protoc-gen-grpc-kotlin:${grpcKotlinVersion}:jdk8@jar"
		}
	}
	generateProtoTasks {
		all().forEach {
			it.plugins {
				id("grpc")
				id("grpckt")
			}
			it.builtins {
				id("kotlin")
			}
		}
	}
	sourceSets{
		main{
			proto {
				srcDir("../proto")
			}
			java {
				srcDir("build/generated/source/proto/main/java")
				srcDir("build/generated/source/proto/main/kotlin")
			}
		}
	}
}

Logging
Problem: In webflux, request body type is Flux, one it is read then it cannot be read again. -> read for logging then 400 bad request will happen.

implementation("net.logstash.logback:logstash-logback-encoder:6.6")
  • logback: based on log4j. By using slf4j, all logs can be intergrated into logback if libraries use other logging framework.
profile
염염

0개의 댓글