Kotless代表Kotlin無服務器框架。
它的重點在於通過直接從應用程序本身的代碼生成無服務器部署創建的例程。
因此,簡而言之,Kotless為您提供了一個魔術按鈕,將Web應用程序部署為AWS和Azure上的無服務器應用程序!
無肉種由兩個主要部分組成:
Kotless的關鍵特徵之一是它嵌入現有應用程序的能力。 Kotless使現有的Spring和KTOR應用程序非常容易地將其部署到AWS和Microsoft Azure無服務器平台上。
Kotless使用Gradle將現有的建築過程包裹起來,並將部署插入其中。
考慮從7.2版本開始使用Gradle的最新版本之一。
基本上,如果您已經使用了Gradle,則只需要做兩件事即可。
首先,設置無毛gradle插件。
您必須通過編輯settings.gradle.kts來告訴Gradle在哪裡可以找到插件:
pluginManagement {
resolutionStrategy {
this .eachPlugin {
if (requested.id.id == " io.kotless " ) {
useModule( " io.kotless:gradle: ${ this .requested.version} " )
}
}
}
repositories {
maven(url = uri( " https://packages.jetbrains.team/maven/p/ktls/maven " ))
gradlePluginPortal()
mavenCentral()
}
}並應用插件:
// Imports are necessary, for this example
import io.kotless.plugin.gradle.dsl.Webapp.Route53
import io.kotless.plugin.gradle.dsl.kotless
// Group may be used by Kotless DSL to reduce the number of introspected classes by package
// So, don't forget to set it
group = " org.example "
version = " 0.1.0 "
plugins {
// Version of Kotlin should be 1.3.72+
kotlin( " jvm " ) version " 1.5.31 " apply true
id( " io.kotless " ) version " 0.2.0 " apply true
}其次,在您的應用程序中添加庫特DSL(或KTOR或Spring Boot)作為庫:
repositories {
mavenCentral()
// Kotless repository
maven(url = uri( " https://packages.jetbrains.team/maven/p/ktls/maven " ))
}
dependencies {
implementation( " io.kotless " , " kotless-lang " , " 0.2.0 " )
implementation( " io.kotless " , " kotless-lang-aws " , " 0.2.0 " )
// if you want to deploy to Microsoft Azure, just replace -aws with -azure
// implementation("io.kotless", "ktor-lang-azure", "0.2.0")
// or for Ktor (Note, that `ktor-lang` depends on Ktor version 1.5.0)
// implementation("io.kotless", "ktor-lang", "0.2.0")
// implementation("io.kotless", "ktor-lang-aws", "0.2.0")
// implementation("io.kotless", "ktor-lang-azure", "0.2.0")
// or for Spring Boot (Note, that `spring-boot-lang` depends on Spring Boot version 2.3.0.RELEASE)
// implementation("io.kotless", "spring-boot-lang", "0.2.0")
// implementation("io.kotless", "spring-boot-lang-aws", "0.2.0")
// implementation("io.kotless", "spring-boot-lang-azure", "0.2.0")
}請注意,如果您使用KTOR或Spring Boot,則需要用特殊的無koteble *-lang依賴項替換項目依賴性中的現有。另外,之後,您需要將依賴庫的版本(如Spring Security)與*-lang中的版本捆綁在一起(請參閱本段)
這使您可以訪問代碼中的DSL接口,並在應用程序中設置Lambda調度程序。
根據用例,您可能需要在AWS或Microsoft Azure中部署應用程序。
請注意,如果您甚至沒有云帳戶,您仍然可以在本地使用Kotless運行和調試您的應用程序 - 只需使用local Gradle任務即可。
如果您沒有AWS帳戶,則可以按照Hadi Hariri的簡單說明來創建它。
如果您有一個AWS帳戶並想執行真實的部署 - 讓我們設置所有內容!很簡單:
kotless {
config {
aws {
storage {
bucket = " kotless.s3.example.com "
}
profile = " example "
region = " eu-west-1 "
}
}
webapp {
dns( " kotless " , " example.com " )
}
}在這裡,我們設置了無限本身的配置:
然後,我們設置了一個特定的應用程序:
這就是整個設置!
部署到Microsoft Azure也非常簡單明了:
kotless {
config {
azure {
storage {
storageAccount = " your-storage-account "
container = " container-which-kotless-would-use "
}
terraform {
backend {
resourceGroup = " your-resource-group "
}
}
}
}
webapp {
dns( " kotless " , " example.com " )
}
}
在這裡,我們設置了無限本身的配置:
然後,我們設置了一個特定的應用程序:
這就是整個設置!
現在,您可以使用Kotless DSL創建第一個無服務器應用程序:
@Get( " / " )
fun main () = " Hello world! "或與KTOR:
class Server : Kotless () {
override fun prepare ( app : Application ) {
app.routing {
get( " / " ) {
call.respondText { " Hello World! " }
}
}
}
}或帶有春季靴子:
@SpringBootApplication
open class Application : Kotless () {
override val bootKlass : KClass < * > = this :: class
}
@RestController
object Pages {
@GetMapping( " / " )
fun main () = " Hello World! "
}基於Kotless的應用程序可以以HTTP服務器本地啟動。此功能均由所有DSL支持。
此外,無限的本地開始可能會啟動AWS仿真(需要Docker)。只需使用覆蓋無限的本地啟動來實例化您的AWS服務客戶端:
val client = AmazonDynamoDBClientBuilder .standard().withKotlessLocal( AwsResource . DynamoDB ).build()並在Gradle中啟用它:
kotless {
// <...>
extensions {
local {
// enables AWS emulation (disabled by default)
useAWSEmulation = true
}
}
}在本地運行期間,將啟動LocalStack,所有客戶端將自動指向其端點。
本地開始功能不需要對雲提供商的任何訪問權限,因此您可以在沒有AWS帳戶的情況下檢查應用程序的行為。另外,它使您有可能從IDE進行本地調試。
Kotless能夠將現有的Spring Boot或KTOR應用程序部署到AWS無服務器平台。為此,您需要設置一個插件,並用適當的無核DSL替換現有的依賴關係。
對於KTOR ,您應該用implementation("io.ktor", "ktor-server-netty", "1.5.0") implementation("io.kotless", "ktor-lang", "0.1.6") 。請注意,此依賴關係將1.5.0版的KTOR捆綁在一起,因此您可能需要將其他KTOR庫(例如ktor-html-builder )升級到此版本。
對於Spring Boot,您應該替換使用的起動器(例如implementation("org.springframework.boot", "spring-boot-starter-web", "2.3.0.RELASE) )使用implementation("io.kotless", "spring-boot-lang", "0.1.6") 2.4.2版本。
完成後,您可能會按下deploy任務並使應用程序無服務器。請注意,您仍然可以通過local Gradle任務在本地運行應用程序。
雖然Kotless可以用作快速創建無服務器應用程序的框架,但它具有涵蓋不同應用領域的更多功能。
包括但不限於:
@Scheduled作業;Kotless正在積極開發中,因此我們目前正在努力以:
一個適當的例子,任何解釋都變得更好。
在存儲庫的examples文件夾中,您可以找到使用Kotless DSL構建的示例項目:
kotless/site - 關於用Kotless DSL(site.kotless.io)編寫的有關Kotless的網站。此示例演示了@StaticGet和@Get (靜態和動態路由)的用法以及鏈接API。kotless/shortener - 用Kotless DSL(Short.kotless.io)編寫的簡單URL縮短器。此示例演示@Get (動態路由), @Scheduled (計劃的lambdas),Permissions API(用於DynamoDB訪問)和TerraForm擴展。KTOR也存在類似的例子:
ktor/site - 關於用KTOR編寫的有關Kotless的網站(Ktor.site.kotless.io)。此示例演示了static {...}和routing {...}用法。ktor/shortener - 用ktor(ktor.short.kotless.io)編寫的簡單URL縮短器。此示例演示了routing { ... } (動態路由),權限API(用於DynamoDB訪問)和TerraForm擴展。對於春季靴子:
spring/site - 關於帶有Spring Boot(Spring.site.kotless.io)的無科特的網站。此示例演示了靜態和@RestController的用法。spring/shortener - 一個簡單的URL縮短器,編寫了帶有Spring Boot(Spring.short.kotless.io)的簡單網址。此示例演示了@RestController (動態路由),Permissions API(用於DynamoDB訪問)和TerraForm擴展的用法。 您可以查看Kotless上的客戶文檔所在的Wiki。
除此之外,無限無關的代碼本身已被廣泛記錄,您可以查看其界面以更好地了解Kotless。
您可能會提出問題並參加有關Kotlinlang Slack中#kotless渠道的討論。
特別感謝: