Kmongo (Kotlin Server + Mongo) 的 Ktor 服务器配置问题

Posted

技术标签:

【中文标题】Kmongo (Kotlin Server + Mongo) 的 Ktor 服务器配置问题【英文标题】:Problem with the configuration of the Ktor server for Kmongo (Kotlin Server + Mongo) 【发布时间】:2019-08-02 21:04:10 【问题描述】:

我将 Ktor 与 KMongo 一起使用。当我在 localhost 中启动服务器并发出一个简单的 'get' 请求 时,我遇到了问题。参考资料不是很清楚和完整 - https://litote.org/kmongo/ - 关于服务器上数据库的配置。 有谁知道如何解决这个问题?我认为这是一个简单的配置问题,但我不知道如何解决它。 我没有使用 Spring。

这是连接到 KmongoDB 的代码:

package com.progettoetere.Routes

import io.ktor.application.
import io.ktor.response.respondText
import io.ktor.routing.*
import kotlinx.coroutines.runBlocking

import org.litote.kmongo.eq
import org.litote.kmongo.reactivestreams.*
import org.litote.kmongo.coroutine.*

fun Route.survey()

    //ramo per i survey
    route("/survey")

        //ritorno tutti i questionari
        get("/")



                        val client = KMongo.createClient().coroutine //client whit Kotli coroutines
                        val database = client.getDatabase("test") //get a test database
                        val colSurvey = database.getCollection<Survey>() //get the collection of surveys

                        //start coroutine
                        runBlocking 

                            //get one survey for DEBUG with id == 1023
                            val dato: Survey? = colSurvey.findOne(Survey::id_survey eq 1023)

                            //check the survey
                            if (dato != null) 
                                call.respondText  "The survey is present" 
                             else 
                                call.respondText  "The survey is not present" 
                            
                        


创建客户端实例时出现错误:

val client = KMongo.createClient().coroutine

这是堆栈错误:

2019-03-11 20:58:22.334 [main] INFO  Application - Responding at http://0.0.0.0:8080
2019-03-11 20:58:27.633 [nettyCallPool-4-1] INFO  org.mongodb.driver.cluster - Cluster created with settings hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500
2019-03-11 20:58:27.655 [nettyCallPool-4-1] DEBUG org.mongodb.driver.cluster - Updating cluster description to  type=UNKNOWN, servers=[address=localhost:27017, type=UNKNOWN, state=CONNECTING]
2019-03-11 20:58:27.666 [cluster-ClusterIdvalue='5c86bde3cad88d4f128ae82c', description='null'-localhost:27017] DEBUG org.mongodb.driver.connection - Closing connection connectionIdlocalValue:1
2019-03-11 20:58:27.668 [cluster-ClusterIdvalue='5c86bde3cad88d4f128ae82c', description='null'-localhost:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:117)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:128)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connessione rifiutata
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198)
    at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
    at sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:293)

【问题讨论】:

【参考方案1】:

问题解决了。我确信 Mongo 服务器是用 Ktor 服务器启动的,实际上我必须手动启动 Mongo 服务器。一旦从终端启动 Mongo 服务器,Ktor 服务器就能够在浏览器获取或 curl 请求期间与 mongo 进行通信。

【讨论】:

以上是关于Kmongo (Kotlin Server + Mongo) 的 Ktor 服务器配置问题的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB 数据库不是在 Kotlin(Ktor) 中创建的

异常打开套接字 kmongo 和 ktor

如何修复 io.ktor.server.engine.CommandLineKt 在 gradle/kotlin/netty 项目中抛出的“既未指定端口也未指定 sslPort”?

如何使用 Kotlin 在所有活动中播放背景音乐?

Kotlin:如何将一个函数作为参数传递给另一个函数?

Android AIDL:从java更改代码到kotlin之后项目崩溃