centos 7
官网下载jdk 1.8和scala 2.11.8,rpm直接安装。
sbt 0.13.11 tgz的直接解压。ln建立软连接。
新建目录helloakka/project
helloakka下新建bulid.sbt,内容如下:
name := "helloakka"
version := "1.0.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.7",
"com.typesafe.akka" %% "akka-agent" % "2.4.7",
"com.typesafe.akka" %% "akka-camel" % "2.4.7",
"com.typesafe.akka" %% "akka-cluster" % "2.4.7",
"com.typesafe.akka" %% "akka-cluster-metrics" % "2.4.7",
"com.typesafe.akka" %% "akka-cluster-sharding" % "2.4.7",
"com.typesafe.akka" %% "akka-cluster-tools" % "2.4.7",
"com.typesafe.akka" %% "akka-contrib" % "2.4.7",
"com.typesafe.akka" %% "akka-http-core" % "2.4.7",
"com.typesafe.akka" %% "akka-http-testkit" % "2.4.7",
"com.typesafe.akka" %% "akka-multi-node-testkit" % "2.4.7",
"com.typesafe.akka" %% "akka-osgi" % "2.4.7",
"com.typesafe.akka" %% "akka-persistence" % "2.4.7",
"com.typesafe.akka" %% "akka-persistence-tck" % "2.4.7",
"com.typesafe.akka" %% "akka-remote" % "2.4.7",
"com.typesafe.akka" %% "akka-slf4j" % "2.4.7",
"com.typesafe.akka" %% "akka-stream" % "2.4.7",
"com.typesafe.akka" %% "akka-stream-testkit" % "2.4.7",
"com.typesafe.akka" %% "akka-testkit" % "2.4.7",
"com.typesafe.akka" %% "akka-distributed-data-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-typed-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-http-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-http-jackson-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-http-spray-json-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-http-xml-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-persistence-query-experimental" % "2.4.7",
"com.typesafe.akka" %% "akka-typed-experimental" % "2.4.7"
)
新建start.scala,内容如下:
import akka.stream.scaladsl._
import akka.util.ByteString
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpEntity, ContentTypes}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.util.Random
import scala.io.StdIn
import akka.stream.ActorMaterializer
import akka.actor.ActorSystem
object start {
def main(args: Array[String]) {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
def respone(html:String) = complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,html))
val route =
{get {
pathSingleSlash {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,"<html><body>Hello world!</body></html>"))
} ~
path("ping") {
respone("""<div style="max-height:100px;maxHeightIE:100px;">ping</div>""")
} ~
path("crash") {
sys.error("BOOM!")
}
} ~
post {
pathSingleSlash {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,"<html><body>Hello world!</body></html>"))
}
}
}
// `route` will be implicitly converted to `Flow` using `RouteResult.route2HandlerFlow`
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
project目录下echo sbt.version=0.13.11 >> build.properties
helloakka目录下运行sbt,然后输入run回车。