我想使用 spray.io 创建一个API,我按照https://danielasfregola.com/2015/02/23/how-to-build-a-rest-api-with-spray/的每条指令执行
之后,这是我的代码
package API.Configurations
import java.io.File
import com.typesafe.config.ConfigFactory
import akka.io.IO
import spray.can.Http
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
import akka.actor.{ActorSystem, Props}
object MainServices
extends App
{
val configFile = new File("/home/ubuntu/conf/application.conf")
val getPortMYSQL = ConfigFactory.parseFile(configFile).getInt("port")
val getAddressMYSQL = ConfigFactory.parseFile(configFile).getString("address")
val getUsernameMYSQL = ConfigFactory.parseFile(configFile).getString("username")
val getPasswordMYSQL = ConfigFactory.parseFile(configFile).getString("password")
val getPortAPI = ConfigFactory.parseFile(configFile).getInt("port_api")
val getAddressAPI = ConfigFactory.parseFile(configFile).getString("address_api")
val getPortES = ConfigFactory.parseFile(configFile).getInt("port_es")
val getAddressES = ConfigFactory.parseFile(configFile).getString("address_es")
val getclusterNameES = ConfigFactory.parseFile(configFile).getString("clusterNameES")
implicit val system = ActorSystem("BRISK_API")
val service = system.actorOf(Props[ServicesControllers], "CONFIGURATION_BRISK_API")
import scala.concurrent.duration._
implicit val askTimeout = new Timeout(5.seconds)
// implicit val timeout = Timeout(5.seconds)
IO(Http) ? Http.Bind(service, interface = getAddressAPI, port = getPortAPI)
println(s"Detil Services MYSQL : Port = ${getPortMYSQL}, Address = ${getAddressMYSQL}, Username = ${getUsernameMYSQL}, Password = ${getPasswordMYSQL}")
println(s"Detil Services API : Port = ${getPortAPI}, Address = ${getAddressAPI}")
println(s"Detil Services ES : Port = ${getPortES}, Address = ${getAddressES}, ClusterName = ${getclusterNameES}")
}
当我尝试使用脚本时: sbt run ,即时收到错误。编译说:
[error] (run-main-0) java.lang.NoClassDefFoundError: akka/util/Timeout
java.lang.NoClassDefFoundError: akka/util/Timeout
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
Caused by: java.lang.ClassNotFoundException: akka.util.Timeout
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 0 s, completed Oct 14, 2016 5:33:23 PM
总是在 akka / util / timeout ,任何想法如何处理? 感谢。
编辑1: 我从那些链接中被跟踪,但结果仍然与之前相同。 Play 2.5 with Akka - could not find implicit value for parameter timeout: akka.util.Timeout
编辑2: 我的 build.sbt
name := "SBTSR2016"
version := "1.0"
mainClass in Compile := Some("API.Configurations.MainServices")
scalaVersion := "2.10.5"
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case x => MergeStrategy.first
}
resolvers += "spray repo" at "http://repo.spray.io"
resolvers += "spray nightlies repo" at "http://nightlies.spray.io"
libraryDependencies += "io.spray" % "spray-json_2.10" % "1.3.2" % "provided"
libraryDependencies += "io.spray" % "spray-can_2.10" % "1.3.2" % "provided"
libraryDependencies += "io.spray" % "spray-client_2.10" % "1.3.2" % "provided"
libraryDependencies += "io.spray" % "spray-testkit_2.10" % "1.3.2" % "provided"
//libraryDependencies += "io.spray" % "spray-routing_2.10" % "1.3.2" % "provided"
libraryDependencies += "io.spray" % "spray-http_2.10" % "1.3.2" % "provided"
libraryDependencies += "io.spray" % "spray-httpx_2.10" % "1.3.2" % "provided"
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.12" % "provided"
libraryDependencies += "org.elasticsearch" % "elasticsearch" % "2.3.1" % "provided"
libraryDependencies += "com.sksamuel.elastic4s" % "elastic4s-streams_2.10" % "2.3.1" % "provided"
//libraryDependencies += "org.elasticsearch" % "elasticsearch" % "1.3.2"
libraryDependencies += "org.elasticsearch" % "elasticsearch-mapper-attachments" % "2.3.1" % "provided"
libraryDependencies += "com.typesafe" % "config" % "1.2.1" % "provided"
libraryDependencies += "io.spray" % "spray-routing-shapeless2_2.10" % "1.3.2"
//libraryDependencies += "com.typesafe.akka" % "akka-actor_2.10" % "2.3.4" % "provided"
libraryDependencies += "com.typesafe.akka" % "akka-actor_2.10" % "2.3.1" % "provided"
答案 0 :(得分:0)
在 build.sbt ,添加一些代码,在此下方:
mergeStrategy in assembly := {
case m if m.toLowerCase.endsWith("manifest.mf") => MergeStrategy.discard
case m if m.toLowerCase.matches("meta-inf.*\\.sf$") => MergeStrategy.discard
case "reference.conf" => MergeStrategy.concat
case _ => MergeStrategy.first
}
它对我有用。
参考链接:https://blog.knoldus.com/2015/07/20/spark-with-spray-starter-kit/comment-page-1/