【问题标题】:Error from logs after deploying ktor application to Heroku将 ktor 应用程序部署到 Heroku 后出现日志错误
【发布时间】:2021-01-06 04:07:18
【问题描述】:

我已经用 ktor 构建了一个测试应用程序,它在我的本地主机上运行良好,但是当我将它上传到 heroku 并对其进行处理时,它显示构建成功但是当我打开链接时,它显示应用程序错误。

  • 这是我的应用程序类:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@kotlin.jvm.JvmOverloads
fun Application.module(testing : Boolean = true) {
    val port = System.getenv("PORT")?.toInt() ?: 23567
    embeddedServer(Netty,port){

        install(ContentNegotiation){
            jackson {
                enable(SerializationFeature.INDENT_OUTPUT)
            }
        }
        routing {
            get("/randomgames"){

                var submodel = subModel("Active", Calendar.getInstance().time.toString())
                var game1  = userCredentials("Los Angelos","California","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png",submodel)
                var game2  = userCredentials("Texas","Arizona","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png",submodel)
                var game3  = userCredentials("New York","Virginia","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png","https://cdn.pixabay.com/photo/2017/08/05/11/16/logo-2582748_1280.png",submodel)
                var gamesList = arrayListOf<userCredentials>()
                gamesList.add(game1)
                gamesList.add(game2)
                gamesList.add(game3)

                var mainModel = MainModel(gamesList)

                call.respond(mainModel)
            }

        }
    }.start(wait = true)

}
  • 这是应用配置文件
ktor {
    deployment {
        environment = development
         port = ${PORT}
    }
    application {
        modules = [ ktor.api.com.ApplicationKt.module ]
    }
}

  • 这是我的 Procfile:
web: build/install/kotlinserver/bin/kotlinserver

*这是我的毕业典礼:

buildscript {
    repositories {
        jcenter()


    }
    
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
group 'ktor.api.com'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = "io.ktor.server.netty.EngineMain"

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

sourceSets {
    main.kotlin.srcDirs = main.java.srcDirs = ['src']
    test.kotlin.srcDirs = test.java.srcDirs = ['test']
    main.resources.srcDirs = ['resources']
    test.resources.srcDirs = ['testresources']
}

task stage {
    dependsOn installDist
    println "Running Stage"
}

repositories {
    mavenLocal()
    jcenter()
    maven { url 'https://kotlin.bintray.com/ktor' }
    maven { url 'https://kotlin.bintray.com/kotlin-js-wrappers' }
    maven { url "https://plugins.gradle.org/m2/" }
    maven {url 'https://jitpack.io'}
    maven {url 'https://github.com/psiegman/mvn-repo/raw/master/releases'}
    maven {url 'http://dl.bintray.com/kotlin/kotlinx.html'}
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "io.ktor:ktor-server-netty:$ktor_version"
    implementation "ch.qos.logback:logback-classic:$logback_version"
    implementation "io.ktor:ktor-server-core:$ktor_version"
    implementation "io.ktor:ktor-jackson:$ktor_version"
    implementation "io.ktor:ktor-gson:$ktor_version"
    implementation "io.ktor:ktor-auth:$ktor_version"
    implementation "io.ktor:ktor-html-builder:$ktor_version"
    implementation "org.jetbrains:kotlin-css-jvm:1.0.0-pre.31-kotlin-1.2.41"
    implementation "io.ktor:ktor-client-core:$ktor_version"
    implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
    implementation "io.ktor:ktor-client-apache:$ktor_version"
    testImplementation "io.ktor:ktor-server-tests:$ktor_version"
}

  • 这是连接到我的 heroku 帐户并获取日志后来自我的 cmd 的错误
// it show this error related to procfile 
bash: build/install/kotlinserver/bin/kotlinserver: No such file or directory

//and this is H10 error code 
2020-09-19T12:38:18.476773+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=aqueous-reaches-02639.herokuapp.com request_id=0dbb86c9-0bf1-4aac-acd6-0825c938b3ba fwd="41.200.180.139" dyno= connect= service= status=503 bytes= protocol=https
2020-09-19T12:38:19.256239+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=aqueous-reaches-02639.herokuapp.com request_id=d8bf7249-4f8a-4423-889a-81baca206ba4 fwd="41.200.180.139" dyno= connect= service= status=503 bytes= protocol=https

PS:我部署我的 ktor 应用程序的方式是简单地创建一个 git 并提交我的文件,然后在 heroku 网站中,我创建一个应用程序并连接到我的 github 并部署该应用程序,任何帮助将不胜感激谢谢。

【问题讨论】:

标签: kotlin ktor


【解决方案1】:

我刚刚解决了同样的问题

'kotlinserver' 名称错误,我确实通过在控制台中运行 gradlew 阶段并转到 Procfile 中指定的路径(构建/安装/...) 我认为正确的名称是项目(模块)名称。

还想提几点

  • 首先application.conf 文件中的${PORT} 将接收系统环境变量值,因此您不需要在应用程序模块函数中使用val port = System.getenv("PORT")?.toInt() ?: 23567。在application.conf 中添加 2 行,例如以下 port = 8080 port = ${?PORT} 会将端口设置为 8080,如果存在则设置为 SystemEnv PORT
  • 第二个只是想提醒一下,不要忘记为您的应用运行heroku ps:scale web=1(您也可以通过在web=1之前添加(-a [app-name])在此处指定应用

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2018-08-16
    • 2022-09-25
    • 2020-07-22
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多