akka http的hello world

centos 7html

官網下載jdk 1.8和scala 2.11.8,rpm直接安裝。node

sbt 0.13.11 tgz的直接解壓。ln創建軟鏈接。json

新建目錄helloakka/projectcentos

helloakka下新建bulid.sbt,內容以下:dom

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",post

"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"
)ui

新建start.scala,內容以下:spa

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.ActorSystemscala

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
  }
}server

project目錄下echo sbt.version=0.13.11 >> build.properties

helloakka目錄下運行sbt,而後輸入run回車。

相關文章
相關標籤/搜索