我正在使用 Akka 开发一个小型微服务。包含在 HttpServer 中,如下所示:
object HttpServer extends App {
val config = ConfigFactory.load()
implicit val system = ActorSystem(config.getString("server.actor-system"))
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
implicit val logger = Logging(system, getClass)
implicit val employeeRouterActor = system.actorOf(Props[EmployeeRouterActor], name = "employeeRouterActor")
val employeeRoutes = new EmployeeRoutes()
val routes = employeeRoutes.route // ~ otherRoutes.route
val host = config.getString("server.host")
val port = config.getInt("server.port")
val bindingFuture = Http().bindAndHandle(routes, host, port)
logger.info(s"Server online at http://$host:$port/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
logger.info(s"Server stopped :(")
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
更新到最新的 Akka 版本后,从 _2.12:2.4.17 到 _2.12:2.5.2 我总是在服务器启动时遇到以下错误:
objc[27272]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/bin/java (0x10983c4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10a8914e0). One of the two will be used. Which one is undefined.
22:29:35.457 [my-system-akka.actor.default-dispatcher-2] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
Exception in thread "main" java.lang.NoClassDefFoundError: akka/stream/Fusing$
at akka.http.scaladsl.HttpExt.fuseServerFlow(Http.scala:87)
at akka.http.scaladsl.HttpExt.bindAndHandle(Http.scala:181)
at server.HttpServer$.delayedEndpoint$server$HttpServer$1(HttpServer.scala:36)
at server.HttpServer$delayedInit$body.apply(HttpServer.scala:17)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at server.HttpServer$.main(HttpServer.scala:17)
at server.HttpServer.main(HttpServer.scala)
Caused by: java.lang.ClassNotFoundException: akka.stream.Fusing$
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
服务器在 2.4.17 中工作正常。更新到 2.5.2 后有人遇到此错误吗?
解决方案是将 Akka Http 也更新到最新版本,在本例中为 10.0.7。
就我而言,我将 akka-stream 移至 2.5.23,将 akka-http 移至 10.1.9 以支持 scala 2.12