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())

现在这个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(,"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 = , 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 -> )
.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 = , 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()
.withMaxOpenRequests()
.withMaxRetries()
.withPipeliningLimit()
val pooledClient = new PooledClient("localhost",,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()) 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(,,)) 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",,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(,"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", ) 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 -> )
.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 = , 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 = , 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()) 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(,"Item#23", 46.0)))
.andThen { case _ => sys.terminate()} val settings = ConnectionPoolSettings(sys)
.withMaxConnections()
.withMaxOpenRequests()
.withMaxRetries()
.withPipeliningLimit()
val pooledClient = new PooledClient("localhost",,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()) 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(,,)) 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",,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(,"Item#23", 46.0)))
.andThen { case _ => sys.terminate()} }

Akka(37): Http:客户端操作模式的更多相关文章

  1. HDFS的Java客户端操作代码(HDFS的查看、创建)

    1.HDFS的put上传文件操作的java代码: package Hdfs; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  2. arm9的操作模式,寄存器,寻址方式

    工作模式 Arm有7种工作模式: 名称 简称 简介 User Usr 正常用户程序执行的模式(linux下用户程序就是在这一模式执行的.) FIQ Fiq 快速中断模式 IRQ Irq 普通中断模式 ...

  3. IdentityServer4 (1) 客户端授权模式(Client Credentials)

    写在前面 1.源码(.Net Core 2.2) git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git 2.相关章节 2.1. ...

  4. PYTHON工业互联网应用实战12—客户端操作

    本章节我们将实现与admin里类似的列操作"下达"功能,演示客户端是如何实现操作功能,同时,演示也会强调一点,何时合并你的功能代码,避免相同功能使用不同的代码段来实现,在企业开发中 ...

  5. ListView多选操作模式——上下文操作模式

    1.什么叫上下文操作模式 2.如何进入上下文操作模式 1.ListView自身带了单选.多选模式,可通过listview.setChoiceMode来设置: listview.setChoiceMod ...

  6. java web 获取客户端操作系统信息

    package com.java.basic.pattern; import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

  7. Hadoop系列007-HDFS客户端操作

    title: Hadoop系列007-HDFS客户端操作 date: 2018-12-6 15:52:55 updated: 2018-12-6 15:52:55 categories: Hadoop ...

  8. pytthon—day8 读写模式的结合、文件操作模式、with完成文本文件复制、游标操作

    一.读写模式的结合 w:写指没有新建文件,有文件就清空 w=open('1.txt','w',encoding='utf-8') w.write('000\n') 在写入数据时,需要及时处理内存空间, ...

  9. 使用Java客户端操作elasticsearch(二)

    承接上文,使用Java客户端操作elasticsearch,本文主要介绍 常见的配置 和Sniffer(集群探测) 的使用. 常见的配置 前面已介绍过,RestClientBuilder支持同时提供一 ...

随机推荐

  1. 学习笔记之08试用div做网页(滨院)-小作业

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 在不用Promise的情况下如何控制异步请求?

    如何更好的控制异步请求?相信大家一定首选Promise对象.确实,使用Promise控制异步请求确实非常方便,直接使用then()方法就可以实现当一个异步请求完成后再处理另一个请求或操作.同时,这样的 ...

  3. windows下LINUX模拟终端Cypwin以及Vim的配置使用

    Cypwin的安装 从官网下载相应版本后,直接安装. 官网地址:Cypwin 安装过程中可以选择相应的Packages,我们需要安装的Vim就需要在这一步中选择相应的包. Cypwin的使用 纯命令行 ...

  4. Xamarin.Forms 开发IOS、Android、UWP应用

    C#语言特点,简单.快速.高效.本次我们通过C#以及Xaml来做移动开发. 1.开发工具visual studio 2015或visual studio 2017.当然visual studio 20 ...

  5. uva 10118,记忆化搜索

    这个题debug了长达3个小时,acm我不能放弃,我又回来了的第一题! 一开始思路正确,写法不行,结果越改越乱 看了网上某神的代码,学习了一下 coding+debug:4小时左右,记忆化搜索+dp类 ...

  6. Ajax禁止重复提交

    var pendingRequests = []; var generatePendingRequestKey = function (obj) { return obj.data || {}; } ...

  7. js中的undefined 和null

    undefined是基本数据类型 表示未定义 缺少的意思 null是引用数据类型  是对象 表示空对象 undefined是从null派生出来的  所以undefined==null  true Ja ...

  8. 小技巧:Oracle:sqlplus 显示行列字符数

    遇到这种情况可以判断:行显示字符数不够,可以增加行显示字符数 01.可以当前会话HR@ACE >set line 400; 02.上面的方法其它会话不生效,懒不想每次设置怎么办? Oracle: ...

  9. LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  10. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...