首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kmongo Ktor服务器的配置问题(Kotlin server + Mongo)

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

Stack Overflow用户
提问于 2019-03-12 11:43:52
回答 1查看 1.2K关注 0票数 0

我在用Ktor和KMongo。当我在本地主机中启动服务器并制作一个简单的'get‘请求时,我遇到了一个问题。对于服务器上数据库的配置,引用并不是非常明确和完整的-- https://litote.org/kmongo/。有人知道如何解决这个问题吗?我认为这是一个简单的配置问题,但我不知道如何解决它。我不使用Spring.

这是连接到KmongoDB的代码:

代码语言:javascript
复制
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 = KMongo.createClient().coroutine

这是堆栈错误:

代码语言:javascript
复制
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-ClusterId{value='5c86bde3cad88d4f128ae82c', description='null'}-localhost:27017] DEBUG org.mongodb.driver.connection - Closing connection connectionId{localValue:1}
2019-03-11 20:58:27.668 [cluster-ClusterId{value='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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-13 18:43:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55120675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档