Akka(37): Http:客户端操作模式

   Akka-http的客户端连接模式除Connection-Level和Host-Level之外还有一种非常便利的模式:Request-Level-Api。这种模式免除了连接Connection的概念,任何时候可以直接调用singleRequest来与服务端沟通。下面我们用几个例子来示范singleRequest的用法:

  (for {
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.GET,uri="http://localhost:8011/message"))
    message <- Unmarshal(response.entity).to[String]
  } yield message).andThen {
    case Success(msg) => println(s"Received message: $msg")
    case Failure(err) => println(s"Error: ${err.getMessage}")
  }.andThen {case _ => sys.terminate()}

这是一个GET操作:用Http().singleRequest直接把HttpRequest发送给服务端uri并获取返回的HttpResponse。我们看到,整组函数的返回类型都是Future[?],所以用for-comprehension来把所有实际运算包嵌在Future运算模式内(context)。下面这个例子是客户端上传数据示范:

 (for {
    entity <- Marshal("Wata hell you doing?").to[RequestEntity]
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/message",entity=entity))
    message <- Unmarshal(response.entity).to[String]
  } yield message).andThen {
    case Success(msg) => println(s"Received message: $msg")
    case Failure(err) => println(s"Error: ${err.getMessage}")
  }.andThen {case _ => sys.terminate()}

以上是个PUT操作。我们需要先构建数据载体HttpEntity。格式转换函数Marshal也返回Future[HttpEntity],所以也可以包含在for语句内。关注一下这个andThen,它可以连接一串多个monadic运算,在不影响上游运算结果的情况下实现一些副作用计算。值得注意的是上面这两个例子虽然表现形式很简洁,但我们无法对数据转换过程中的异常及response的状态码等进行监控。所以我们应该把整个过程拆分成两部分:先获取response,再具体处理response,包括核对状态,处理数据等:

  case class Item(id: Int, name: String, price: Double)

  def getItem(itemId: Int): Future[HttpResponse] = for {
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.GET,uri = s"http://localhost:8011/item/$itemId"))
  } yield response

  def extractEntity[T](futResp: Future[HttpResponse])(implicit um: Unmarshaller[ResponseEntity,T]) = {
    futResp.andThen {
      case Success(HttpResponse(StatusCodes.OK, _, entity, _)) =>
        Unmarshal(entity).to[T]
          .onComplete {
            case Success(t) => println(s"Got response entity: ${t}")
            case Failure(e) => println(s"Unmarshalling failed: ${e.getMessage}")
          }
      case Success(_) => println("Exception in response!")
      case Failure(err) => println(s"Response Failed: ${err.getMessage}")
    }
  }
  extractEntity[Item](getItem(13))

现在这个extractEntity[Item](getItem(13))可以实现全过程的监控管理了。用同样的模式实现PUT操作:

  def putItem(item: Item): Future[HttpResponse] =
   for {
    reqEntity <- Marshal(item).to[RequestEntity]
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/item",entity=reqEntity))
   } yield response

  extractEntity[Item](putItem(Item(23,"Item#23", 46.0)))
     .andThen { case _ => sys.terminate()}

当然,我们还是使用了前面几篇讨论里的Marshalling方式来进行数据格式的自动转换:

import de.heikoseeberger.akkahttpjson4s.Json4sSupport
import org.json4s.jackson
...
trait JsonCodec extends Json4sSupport {
  import org.json4s.DefaultFormats
  import org.json4s.ext.JodaTimeSerializers
  implicit val serilizer = jackson.Serialization
  implicit val formats = DefaultFormats ++ JodaTimeSerializers.all
}
object JsConverters extends JsonCodec
...
  import JsConverters._

  implicit val jsonStreamingSupport = EntityStreamingSupport.json()
    .withParallelMarshalling(parallelism = 8, unordered = false)

如果我们需要对数据交换过程进行更细致的管控,用Host-Level-Api会更加适合。下面我们就针对Host-Level-Api构建一个客户端的工具库:

class PooledClient(host: String, port: Int, poolSettings: ConnectionPoolSettings)
                  (implicit sys: ActorSystem, mat: ActorMaterializer) {

  import sys.dispatcher

  private val cnnPool: Flow[(HttpRequest, Int), (Try[HttpResponse], Int), Http.HostConnectionPool] =
    Http().cachedHostConnectionPool[Int](host = host, port = port, settings = poolSettings)
//单一request
  def requestSingleResponse(req: HttpRequest): Future[HttpResponse] = {
    Source.single(req -> 1)
      .via(cnnPool)
      .runWith(Sink.head).flatMap {
      case (Success(resp), _) => Future.successful(resp)
      case (Failure(fail), _) => Future.failed(fail)
    }
  }
//组串request
  def orderedResponses(reqs: Iterable[HttpRequest]): Future[Iterable[HttpResponse]] = {
    Source(reqs.zipWithIndex.toMap)
      .via(cnnPool)
      .runFold(SortedMap[Int, Future[HttpResponse]]()) {
        case (m, (Success(r), idx)) => m + (idx -> Future.successful(r))
        case (m, (Failure(f), idx)) => m + (idx -> Future.failed(f))
      }.flatMap { m => Future.sequence(m.values) }
  }
}

下面是一种比较安全的模式:使用了queue来暂存request从而解决因发送方与接收方速率不同所产生的问题:

class QueuedRequestsClient(host: String, port: Int, poolSettings: ConnectionPoolSettings)
                          (qsize: Int = 10, overflowStrategy: OverflowStrategy = OverflowStrategy.dropNew)
                  (implicit sys: ActorSystem, mat: ActorMaterializer) {
  import sys.dispatcher
  private val cnnPool: Flow[(HttpRequest,Promise[HttpResponse]),(Try[HttpResponse],Promise[HttpResponse]),Http.HostConnectionPool] =
    Http().cachedHostConnectionPool[Promise[HttpResponse]](host=host,port=port,settings=poolSettings)

  val queue =
    Source.queue[(HttpRequest, Promise[HttpResponse])](qsize, overflowStrategy)
      .via(cnnPool)
      .to(Sink.foreach({
        case ((Success(resp), p)) => p.success(resp)
        case ((Failure(e), p))    => p.failure(e)
      })).run()

  def queueRequest(request: HttpRequest): Future[HttpResponse] = {
      val responsePromise = Promise[HttpResponse]()
      queue.offer(request -> responsePromise).flatMap {
        case QueueOfferResult.Enqueued    => responsePromise.future
        case QueueOfferResult.Dropped     => Future.failed(new RuntimeException("Queue overflowed. Try again later."))
        case QueueOfferResult.Failure(ex) => Future.failed(ex)
        case QueueOfferResult.QueueClosed => Future.failed(new RuntimeException("Queue was closed (pool shut down) while running the request. Try again later."))
      }
  }
}

下面是这些工具函数的具体使用示范:

  val settings = ConnectionPoolSettings(sys)
    .withMaxConnections(8)
    .withMaxOpenRequests(8)
    .withMaxRetries(3)
    .withPipeliningLimit(4)
  val pooledClient = new PooledClient("localhost",8011,settings)

  def getItemByPool(itemId: Int): Future[HttpResponse] = for {
    response <- pooledClient.requestSingleResponse(HttpRequest(method=HttpMethods.GET,uri = s"http://localhost:8011/item/$itemId"))
  } yield response

  extractEntity[Item](getItemByPool(13))

  def getItemsByPool(itemIds: List[Int]): Future[Iterable[HttpResponse]] = {
    val reqs = itemIds.map { id =>
      HttpRequest(method = HttpMethods.GET, uri = s"http://localhost:8011/item/$id")
    }
    val rets = (for {
      responses <- pooledClient.orderedResponses(reqs)
    } yield responses)
    rets
  }
  val futResps = getItemsByPool(List(3,5,7))

  futResps.andThen {
    case Success(listOfResps) => {
      listOfResps.foreach { r =>
        r match {
          case HttpResponse(StatusCodes.OK, _, entity, _) =>
            Unmarshal(entity).to[Item]
              .onComplete {
                case Success(t) => println(s"Got response entity: ${t}")
                case Failure(e) => println(s"Unmarshalling failed: ${e.getMessage}")
              }
          case _ => println("Exception in response!")
        }
      }
    }
    case _ => println("Failed to get list of responses!")
  }

  val queuedClient = new QueuedRequestsClient("localhost",8011,settings)()


  def putItemByQueue(item: Item): Future[HttpResponse] =
    for {
      reqEntity <- Marshal(item).to[RequestEntity]
      response <- queuedClient.queueRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/item",entity=reqEntity))
    } yield response

  extractEntity[Item](putItemByQueue(Item(23,"Item#23", 46.0)))
    .andThen { case _ => sys.terminate()}

下面是本次讨论的示范源代码:

服务端代码:

import akka.actor._
import akka.stream._
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._

import de.heikoseeberger.akkahttpjson4s.Json4sSupport
import org.json4s.jackson
trait JsonCodec extends Json4sSupport {
  import org.json4s.DefaultFormats
  import org.json4s.ext.JodaTimeSerializers
  implicit val serilizer = jackson.Serialization
  implicit val formats = DefaultFormats ++ JodaTimeSerializers.all
}
object JsConverters extends JsonCodec


object TestServer extends App with JsonCodec {
  implicit val httpSys = ActorSystem("httpSystem")
  implicit val httpMat = ActorMaterializer()
  implicit val httpEC = httpSys.dispatcher

  import JsConverters._

  case class Item(id: Int, name: String, price: Double)
  val messages = path("message") {
    get {
      complete("hello, how are you?")
    } ~
    put {
      entity(as[String]) {msg =>
        complete(msg)
      }
    }
  }
  val items =
    (path("item" / IntNumber) & get) { id =>
       get {
         complete(Item(id, s"item#$id", id * 2.0))
       }
    } ~
      (path("item") & put) {
        entity(as[Item]) {item =>
          complete(item)
        }
     }

  val route = messages ~ items

  val (host, port) = ("localhost", 8011)

  val bindingFuture = Http().bindAndHandle(route,host,port)


  println(s"Server running at $host $port. Press any key to exit ...")

  scala.io.StdIn.readLine()

  bindingFuture.flatMap(_.unbind())
    .onComplete(_ => httpSys.terminate())

}

客户端源代码:

import akka.actor._
import akka.http.scaladsl.settings.ConnectionPoolSettings
import akka.stream._
import akka.stream.scaladsl._
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._

import scala.util._
import de.heikoseeberger.akkahttpjson4s.Json4sSupport
import org.json4s.jackson

import scala.concurrent._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.unmarshalling._
import akka.http.scaladsl.marshalling.Marshal

import scala.collection.SortedMap
import akka.http.scaladsl.common._

trait JsonCodec extends Json4sSupport {
  import org.json4s.DefaultFormats
  import org.json4s.ext.JodaTimeSerializers
  implicit val serilizer = jackson.Serialization
  implicit val formats = DefaultFormats ++ JodaTimeSerializers.all
}
object JsConverters extends JsonCodec

class PooledClient(host: String, port: Int, poolSettings: ConnectionPoolSettings)
                  (implicit sys: ActorSystem, mat: ActorMaterializer) {

  import sys.dispatcher

  private val cnnPool: Flow[(HttpRequest, Int), (Try[HttpResponse], Int), Http.HostConnectionPool] =
    Http().cachedHostConnectionPool[Int](host = host, port = port, settings = poolSettings)

  def requestSingleResponse(req: HttpRequest): Future[HttpResponse] = {
    Source.single(req -> 1)
      .via(cnnPool)
      .runWith(Sink.head).flatMap {
      case (Success(resp), _) => Future.successful(resp)
      case (Failure(fail), _) => Future.failed(fail)
    }
  }

  def orderedResponses(reqs: Iterable[HttpRequest]): Future[Iterable[HttpResponse]] = {
    Source(reqs.zipWithIndex.toMap)
      .via(cnnPool)
      .runFold(SortedMap[Int, Future[HttpResponse]]()) {
        case (m, (Success(r), idx)) => m + (idx -> Future.successful(r))
        case (m, (Failure(f), idx)) => m + (idx -> Future.failed(f))
      }.flatMap { m => Future.sequence(m.values) }
  }
}
class QueuedRequestsClient(host: String, port: Int, poolSettings: ConnectionPoolSettings)
                          (qsize: Int = 10, overflowStrategy: OverflowStrategy = OverflowStrategy.dropNew)
                  (implicit sys: ActorSystem, mat: ActorMaterializer) {
  import sys.dispatcher
  private val cnnPool: Flow[(HttpRequest,Promise[HttpResponse]),(Try[HttpResponse],Promise[HttpResponse]),Http.HostConnectionPool] =
    Http().cachedHostConnectionPool[Promise[HttpResponse]](host=host,port=port,settings=poolSettings)

  val queue =
    Source.queue[(HttpRequest, Promise[HttpResponse])](qsize, overflowStrategy)
      .via(cnnPool)
      .to(Sink.foreach({
        case ((Success(resp), p)) => p.success(resp)
        case ((Failure(e), p))    => p.failure(e)
      })).run()

  def queueRequest(request: HttpRequest): Future[HttpResponse] = {
      val responsePromise = Promise[HttpResponse]()
      queue.offer(request -> responsePromise).flatMap {
        case QueueOfferResult.Enqueued    => responsePromise.future
        case QueueOfferResult.Dropped     => Future.failed(new RuntimeException("Queue overflowed. Try again later."))
        case QueueOfferResult.Failure(ex) => Future.failed(ex)
        case QueueOfferResult.QueueClosed => Future.failed(new RuntimeException("Queue was closed (pool shut down) while running the request. Try again later."))
      }
  }
}
object ClientRequesting extends App {
  import JsConverters._

  implicit val sys = ActorSystem("sysClient")
  implicit val mat = ActorMaterializer()
  implicit val ec = sys.dispatcher

  implicit val jsonStreamingSupport = EntityStreamingSupport.json()
    .withParallelMarshalling(parallelism = 8, unordered = false)

  case class Item(id: Int, name: String, price: Double)

  def extractEntity[T](futResp: Future[HttpResponse])(implicit um: Unmarshaller[ResponseEntity,T]) = {
    futResp.andThen {
      case Success(HttpResponse(StatusCodes.OK, _, entity, _)) =>
        Unmarshal(entity).to[T]
          .onComplete {
            case Success(t) => println(s"Got response entity: ${t}")
            case Failure(e) => println(s"Unmarshalling failed: ${e.getMessage}")
          }
      case Success(_) => println("Exception in response!")
      case Failure(err) => println(s"Response Failed: ${err.getMessage}")
    }
  }

  
  (for {
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.GET,uri="http://localhost:8011/message"))
    message <- Unmarshal(response.entity).to[String]
  } yield message).andThen {
    case Success(msg) => println(s"Received message: $msg")
    case Failure(err) => println(s"Error: ${err.getMessage}")
  }  //.andThen {case _ => sys.terminate()}


  (for {
    entity <- Marshal("Wata hell you doing?").to[RequestEntity]
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/message",entity=entity))
    message <- Unmarshal(response.entity).to[String]
  } yield message).andThen {
    case Success(msg) => println(s"Received message: $msg")
    case Failure(err) => println(s"Error: ${err.getMessage}")
  } //.andThen {case _ => sys.terminate()}


  def getItem(itemId: Int): Future[HttpResponse] = for {
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.GET,uri = s"http://localhost:8011/item/$itemId"))
  } yield response

  extractEntity[Item](getItem(13))

  def putItem(item: Item): Future[HttpResponse] =
   for {
    reqEntity <- Marshal(item).to[RequestEntity]
    response <- Http().singleRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/item",entity=reqEntity))
   } yield response

  extractEntity[Item](putItem(Item(23,"Item#23", 46.0)))
     .andThen { case _ => sys.terminate()}
  
  val settings = ConnectionPoolSettings(sys)
    .withMaxConnections(8)
    .withMaxOpenRequests(8)
    .withMaxRetries(3)
    .withPipeliningLimit(4)
  val pooledClient = new PooledClient("localhost",8011,settings)

  def getItemByPool(itemId: Int): Future[HttpResponse] = for {
    response <- pooledClient.requestSingleResponse(HttpRequest(method=HttpMethods.GET,uri = s"http://localhost:8011/item/$itemId"))
  } yield response

  extractEntity[Item](getItemByPool(13))

  def getItemsByPool(itemIds: List[Int]): Future[Iterable[HttpResponse]] = {
    val reqs = itemIds.map { id =>
      HttpRequest(method = HttpMethods.GET, uri = s"http://localhost:8011/item/$id")
    }
    val rets = (for {
      responses <- pooledClient.orderedResponses(reqs)
    } yield responses)
    rets
  }
  val futResps = getItemsByPool(List(3,5,7))

  futResps.andThen {
    case Success(listOfResps) => {
      listOfResps.foreach { r =>
        r match {
          case HttpResponse(StatusCodes.OK, _, entity, _) =>
            Unmarshal(entity).to[Item]
              .onComplete {
                case Success(t) => println(s"Got response entity: ${t}")
                case Failure(e) => println(s"Unmarshalling failed: ${e.getMessage}")
              }
          case _ => println("Exception in response!")
        }
      }
    }
    case _ => println("Failed to get list of responses!")
  }

  val queuedClient = new QueuedRequestsClient("localhost",8011,settings)()
  
  def putItemByQueue(item: Item): Future[HttpResponse] =
    for {
      reqEntity <- Marshal(item).to[RequestEntity]
      response <- queuedClient.queueRequest(HttpRequest(method=HttpMethods.PUT,uri="http://localhost:8011/item",entity=reqEntity))
    } yield response

  extractEntity[Item](putItemByQueue(Item(23,"Item#23", 46.0)))
    .andThen { case _ => sys.terminate()}
  

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏个人分享

Spring技术揭幕----DispatcherServlet

Spring MVC是一个MVC模式的实现。在Spring MVC的使用中,需要在web.xml中配置DispatcherServlet,也就是说其核心是一个S...

864
来自专栏张善友的专栏

在asp.net页面上得到Castle容器的实例

在项目中使用Castle IOC容器,Asp.net程序中如何得到Castle容器内。 可以如下实现: 1、Gloabal实现接口IContainerAcces...

18410
来自专栏余林丰

Spring AOP中级——应用场景

  在《Spring AOP初级——入门及简单应用》中对AOP作了简要的介绍,以及一些专业术语的解释,同时写了一个简单的Spring AOPdemo。本文将继续...

18010
来自专栏技术墨客

Spring核心——注解自动装载 原

之前的文章介绍了Spring的IoC容器配置管理方面的详细内容,需要了解的可以从IoC容器的设计模式开始阅读。在介绍基于注解配置的配置之前我们再重复一下在之前提...

886
来自专栏令仔很忙

SpringIOC--初始化源码解析

在上一篇 Spring从入门到精通(一)—-IoC(控制反转) 中,详细的介绍了IOC的基本原理,本篇博客就不再赘述;这次主要是从源码的角度来给大家分享Spr...

581
来自专栏码匠的流水账

聊聊spring的async注解

这里讨论一下getAsyncExecutor这里定义null的情况。 spring-context-4.3.9.RELEASE-sources.jar!/org...

572
来自专栏静默虚空的博客

Spring 面试问题 TOP 50

Spring 配置文件是 XML 文件。该文件主要包含类信息。它描述了这些类是如何配置以及相互引入的。但是,XML 配置文件冗长且更加干净。如果没有正确规划和编...

632
来自专栏Python、Flask、Django

Flask 项目系列 -- 基于Flask打造招聘网站(2017-12-07更新)

1073
来自专栏用户2442861的专栏

25个经典的Spring面试问答

本人收集了一些在大家在面试时被经常问及的关于Spring的主要问题,这些问题有可能在你下次面试时就会被问到。对于本文中未提及的Spring其他模块,我会单独分...

502
来自专栏Spring相关

Springboot+ajax传输json数组以及单条数据的方法

592

扫码关注云+社区