无法使用Scala驱动程序

时间:2018-04-15 17:17:45

标签: mongodb scala

使用MongoDB Scala驱动程序从我的Scala应用程序连接到Mongo DB Atlas实例时遇到问题

当我尝试创建客户端(从SBT控制台执行此操作)时,我收到以下错误

scala> val url = "mongodb+srv://dbuser:dbpass@host.mongodb.net/test"
url: String = mongodb+srv://dbuser:dbpass@host.mongodb.net/test
scala> val mongoClient: MongoClient = MongoClient(url)
Apr 15, 2018 8:02:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[dbname-shard-00-00-randomchars.mongodb.net:27017, dbname-shard-00-01-randomchars.mongodb.net:27017, dbname-shard-00-02-randomchars.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='dbname-shard-randomchars
Apr 15, 2018 8:02:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server dbname-shard-00-00-randomchars.mongodb.net:27017 to client view of cluster
Apr 15, 2018 8:02:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server dbname-shard-00-01-randomchars.mongodb.net:27017 to client view of cluster
Apr 15, 2018 8:02:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server dbname-shard-00-02-randomchars.mongodb.net:27017 to client view of cluster
mongoClient: org.mongodb.scala.MongoClient = MongoClient(com.mongodb.async.client.MongoClientImpl@50416ab3)

scala> Apr 15, 2018 8:02:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server host.mongodb.net:27017
com.mongodb.MongoException: java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponent(ZLio/netty/buffer/ByteBuf;)Lio/netty/buffer/CompositeByteBuf;
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:136)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponent(ZLio/netty/buffer/ByteBuf;)Lio/netty/buffer/CompositeByteBuf;
    at com.mongodb.connection.netty.NettyStream.writeAsync(NettyStream.java:182)
    at com.mongodb.connection.netty.NettyStream.write(NettyStream.java:167)
    at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:403)
    at com.mongodb.connection.InternalStreamConnection.sendCommandMessage(InternalStreamConnection.java:270)
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:253)
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    ... 2 more

过了一会儿,我也在控制台上反复得到这个输出:

java.nio.channels.ClosedChannelException
java.nio.channels.ClosedChannelException

这是我第一次这样做,我不确定我错过了什么。

我已经使用MongoDB CLI和& amp ;;测试了端点+凭证。 Mongo DB Compass(GUI),它们都运行良好。

其他信息:

OS Version: macOS High Sierra 10.13.4

MongoDB Atlas version: Version 3.4.14
Mongo DB Scala Driver Version: 2.2
Scala Version: 2.12.5

这是我的build.sbt文件(来自scala-seed项目的简单):

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization := "com.example",
      scalaVersion := "2.12.5",
      version      := "0.1.0-SNAPSHOT"
    )),
    name := "Hello",
    libraryDependencies ++= Seq(
      scalaTest % Test,
      "org.mongodb.scala" %% "mongo-scala-driver" % "2.2.1",
      "io.netty" % "netty-all" % "4.0.4.Final"
    )
  )

我还暂时允许从MongoDB Atlas上的任何IP访问

1 个答案:

答案 0 :(得分:1)

缺少在classPath上安装的netty版本:CompositeByteBuf.addComponent(ByteBuf)这就是NoSuchMethodError的原因。

pom中Mongo Scala驱动程序所需的版本是netty 4.1.17.Final - 所以请确保它位于classPath上并且可以由Scala驱动程序使用。