如何修复ReactiveMongo中的SSLHandshakeFailed以连接MongoDB地图集?

时间:2018-11-22 04:55:54

标签: scala reactivemongo

我该如何解决以下异常 从客户端接收请求时出错:SSLHandshakeFailed:服务器配置为仅允许SSL连接。

我的代码是

    object GetStarted {
  val mongoUri = "mongodb://reddy:<password>@cluster0-shard-00-00-4u9oj.mongodb.net:27017,cluster0-shard-00-01-4u9oj.mongodb.net:27017,cluster0-shard-00-02-4u9oj.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true"

  import ExecutionContext.Implicits.global // use any appropriate context

  val driver = MongoDriver()
  val parsedUri = MongoConnection.parseURI(mongoUri)
  val connection = parsedUri.map(driver.connection(_))

  val futureConnection = Future.fromTry(connection)

  def db1: Future[DefaultDB] = futureConnection.flatMap(_.database("agshift"))

  def personCollection: Future[BSONCollection] = db1.map(_.collection("agshift_version"))

  println(" futureConnection " + futureConnection)

  implicit def personWriter: BSONDocumentWriter[Person] = Macros.writer[Person]

  import scala.concurrent.duration._

  def createPerson(): Either[Future[Unit], String] = {
    try {
      Left(personCollection.flatMap(_.insert(BSONDocument("1c" -> "1x")).map(_ => {}))) // use personWriter)
    } catch {
      case e: Exception =>
        println(" createPerson ======== " + e.toString)
        Right(e.toString)
    }
  }

  val test2 = findCount().onComplete {
    case Failure(e) => println("    test2    Exception " + e.toString)
    case Success(writeResult) => {
      println(s"successfully inserted document: $writeResult")
    }
  }

  def findCount(): Future[Either[Int, String]] = {
    try {
      val resp = personCollection.flatMap(_.count(Some(BSONDocument("agsky" -> "1.0.112"))).map(i => i))
      println(" test :::: " + Await.result(resp, 40 seconds))
      Future(Left(Await.result(resp, 40 seconds))) // use personWriter))
    } catch {
      case e: Exception =>
        println(" findPerson ======== " + e.toString)
        Future(Right(e.toString))
    }
  }

  implicit def personReader: BSONDocumentReader[Person] = Macros.reader[Person]

  case class Person(firstName: String, lastName: String, age: Int)

}

我已使用以上代码连接MongoDB地图集,并得到了 SSLHandshakeFailed 。我该如何处理?

0 个答案:

没有答案