akka http的hello world

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回车。

 

转载于:https://my.oschina.net/u/2342911/blog/701010

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值