基于akka-http搭建restfull框架

1.scala开发环境介绍

2.scala插件的demo模板介绍

3.akka-http提供demo研究

4.添加路由机制解析

 

package org.netsharp.rest

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer

import scala.io.StdIn

object WebServer {

  def main(args:Array[String]):Unit={

    implicit val system = ActorSystem("netsharp")
    implicit val materializer = ActorMaterializer()
    implicit val executionContext = system.dispatcher

    val userRoute = path("user"){
      get{
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say user to akka-http</h1>"))
      }
    }

    val orderRoute = path("order"){
      get{
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say order to akka-http</h1>"))
      }
    }

    val customerRoute = path("customer"){
      get{
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say customer to akka-http</h1>"))
      }
    }

    val routes =
      pathPrefix("v1") {
        userRoute ~ orderRoute ~ customerRoute
      } ~ path("")(getFromResource("public/index.html"))

    val bindingFuture = Http().bindAndHandle(routes,"localhost",80)

    println(s"Server online at http://localhost:80/\nPress RETURN to stop...")

    StdIn.readLine()

    bindingFuture.flatMap(_.unbind())
      .onComplete(_=>system.terminate())
  }
}

 4.通过变量解析路径

 

package org.netsharp.rest

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer

import scala.io.StdIn

object WebServer {

  def main(args:Array[String]):Unit={

    implicit val system = ActorSystem("netsharp")
    implicit val materializer = ActorMaterializer()
    implicit val executionContext = system.dispatcher

    val xroute=path(Segment/Segment/Segment){(v,domain,operation)=>
      get{
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"version:$v;domain:$domain;operation:$operation"))
      }
    }

    val bindingFuture = Http().bindAndHandle(xroute,"localhost",80)

    println(s"Server online at http://localhost:80/\nPress RETURN to stop...")

    StdIn.readLine()

    bindingFuture.flatMap(_.unbind())
      .onComplete(_=>system.terminate())
  }
}

a.sbt启动项目:run

b.浏览器中输入地址:http://localhost/v1/user/create

c.显示为:version:v1;domain:user;operation:create

 

5.路由机制的多级变量

package org.netsharp.rest

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer

import scala.io.StdIn

object WebServer {

  def main(args:Array[String]):Unit={

    implicit val system = ActorSystem("netsharp")
    implicit val materializer = ActorMaterializer()
    implicit val executionContext = system.dispatcher

    val xroute=path(Segments){xs=>
      get{
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, xs.toString()))
      }
    }

    val bindingFuture = Http().bindAndHandle(xroute,"localhost",80)

    println(s"Server online at http://localhost:80/\nPress RETURN to stop...")

    StdIn.readLine()

    bindingFuture.flatMap(_.unbind())
      .onComplete(_=>system.terminate())
  }
}

a.sbt启动项目:run

b.浏览器中输入地址:http://localhost/v1/user/create

c.显示为:List(v1, user, create)

6.akka-http与actor结合

 

转载于:https://www.cnblogs.com/Netsharp/p/9755057.html

Akka.NET 是 Akka 的 .NET 开源实现。用于构建强大的并发和分布式应用。Akka 是一个用 Scala 编写的库,用于简化编写容错的、高可伸缩性的 Java 和 Scala 的 Actor 模型应用。示例代码:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Akka; using Akka.Actor; namespace ConsoleApplication11 {     public class Greet     {         public Greet(string who)         {             Who = who;         }         public string Who { get;private set; }     }     public class GreetingActor : ReceiveActor     {         public GreetingActor()         {             Receive<Greet>(greet =>                 Console.WriteLine("Hello {0}", greet.Who));         }     }     class Program     {         static void Main(string[] args)         {             //create a new actor system (a container for your actors)             var system = ActorSystem.Create("MySystem");             //create your actor and get a reference to it.             //this will be an "ActorRef", which is not a              //reference to the actual actor instance             //but rather a client or proxy to it             var greeter = system.ActorOf<GreetingActor>("greeter");             //send a message to the actor             greeter.Tell(new Greet("World"));             //this prevents the app from exiting             //Before the async work is done             Console.ReadLine();         }     } } 标签:分布式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值