Akka(36): Http:Client-side-Api,Client-Connections
Akka-http的客户端Api应该是以HttpRequest操作为主轴的网上消息交换模式编程工具。我们知道:Akka-http是搭建在Akka-stream之上的。所以,Akka-http在客户端构建与服务器的连接通道也可以用Akka-stream的Flow来表示。这个Flow可以通过调用Http.outgoingConnection来获取:
/**
* Creates a [[akka.stream.scaladsl.Flow]] representing a prospective HTTP client connection to the given endpoint.
* Every materialization of the produced flow will attempt to establish a new outgoing connection.
*
* To configure additional settings for requests made using this method,
* use the `akka.http.client` config section or pass in a [[akka.http.scaladsl.settings.ClientConnectionSettings]] explicitly.
*/
def outgoingConnection(host: String, port: Int = ,
localAddress: Option[InetSocketAddress] = None,
settings: ClientConnectionSettings = ClientConnectionSettings(system),
log: LoggingAdapter = system.log): Flow[HttpRequest, HttpResponse, Future[OutgoingConnection]] =
_outgoingConnection(host, port, settings.withLocalAddressOverride(localAddress), ConnectionContext.noEncryption(), ClientTransport.TCP, log)
我们看到:这个函数实现了对Server端地址host+port的设定,返回的结果类型是Flow[HttpRequest,HttpResponse,Future[OutgoingConnection]]。这个Flow代表将输入的HttpRequest转换成输出的HttpResponse。这个转换过程包括了与Server之间的Request,Response消息交换。下面我们试着用这个Flow来向Server端发送request,并获取response:
val connFlow: Flow[HttpRequest,HttpResponse,Future[Http.OutgoingConnection]] =
Http().outgoingConnection("akka.io") def sendHttpRequest(req: HttpRequest) = {
Source.single(req)
.via(connFlow)
.runWith(Sink.head)
} sendHttpRequest(HttpRequest(uri="/"))
.andThen{
case Success(resp) => println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
.andThen {case _ => sys.terminate()}
上面的这种模式就是所谓Connection-Level-Client-Side-Api。这种模式可以让用户有更大程度的自由度控制connection的构建、使用及在connection上发送request的方式。一般来讲,当返回response的entity被完全消耗后系统会自动close connection,这套api还提供了一些手动方法可以在有需要的情况下手动进行connection close,如下:
//close connection by cancelling response entity
resp.entity.dataBytes.runWith(Sink.cancelled)
//close connection by receiving response with close header
Http().bindAndHandleSync(
{ req ⇒ HttpResponse(headers = headers.Connection("close") :: Nil) },
"akka.io",
)(mat)
Akka-http客户端api还有一种实用的Host-Level-Client-Side-Api模式。这套api能自动针对每个端点维护一个连接池(connection-pool),用户只需对连接池进行配置。系统按照连接池配置自动维护池内线程的生、死、动、停。akka-http.host-connection-pool配置中max-connections,max-open-requests,pipelining-limit等控制着connection、在途request的数量,需要特别注意。针对某个端点的连接池是通过Http().cachedHostConnectionPool(endPoint)获取的。同样,获取的也是一个client-flow实例。因为系统自动维护着线程池,所以client-flow实例可以任意引用,无论调用次数与调用时间间隔。cachedHostConnectionPool()函数定义如下:
/**
* Same as [[#cachedHostConnectionPool]] but for encrypted (HTTPS) connections.
*
* If an explicit [[ConnectionContext]] is given then it rather than the configured default [[ConnectionContext]] will be used
* for encryption on the connections.
*
* To configure additional settings for the pool (and requests made using it),
* use the `akka.http.host-connection-pool` config section or pass in a [[ConnectionPoolSettings]] explicitly.
*/
def cachedHostConnectionPoolHttps[T](host: String, port: Int = ,
connectionContext: HttpsConnectionContext = defaultClientHttpsContext,
settings: ConnectionPoolSettings = defaultConnectionPoolSettings,
log: LoggingAdapter = system.log)(implicit fm: Materializer): Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool] = {
val cps = ConnectionPoolSetup(settings, connectionContext, log)
val setup = HostConnectionPoolSetup(host, port, cps)
cachedHostConnectionPool(setup)
}
函数返回结果类型:Flow[(HttpRequest,T),(Try[HttpResponse],T),HostConnectionPool]。因为线程池内的线程是异步构建request和接收response的,而返回response的顺序未必按照发送request的顺序,所以需要一个tuple2的T类型标示request与返回的response进行匹配。线程池会根据idle-timeout自动终止,也可以手动通过HostConnectionPool.shutDown()实现:
/**
* Represents a connection pool to a specific target host and pool configuration.
*/
final case class HostConnectionPool private[http] (setup: HostConnectionPoolSetup)(
private[http] val gateway: PoolGateway) { // enable test access /**
* Asynchronously triggers the shutdown of the host connection pool.
*
* The produced [[scala.concurrent.Future]] is fulfilled when the shutdown has been completed.
*/
def shutdown()(implicit ec: ExecutionContextExecutor): Future[Done] = gateway.shutdown() private[http] def toJava = new akka.http.javadsl.HostConnectionPool {
override def setup = HostConnectionPool.this.setup
override def shutdown(executor: ExecutionContextExecutor): CompletionStage[Done] = HostConnectionPool.this.shutdown()(executor).toJava
}
}
也可以通过Http().shutdownAllConnectionPools()一次性终止ActorSystem内所有线程池:
/**
* Triggers an orderly shutdown of all host connections pools currently maintained by the [[akka.actor.ActorSystem]].
* The returned future is completed when all pools that were live at the time of this method call
* have completed their shutdown process.
*
* If existing pool client flows are re-used or new ones materialized concurrently with or after this
* method call the respective connection pools will be restarted and not contribute to the returned future.
*/
def shutdownAllConnectionPools(): Future[Unit] = {
val shutdownCompletedPromise = Promise[Done]()
poolMasterActorRef ! ShutdownAll(shutdownCompletedPromise)
shutdownCompletedPromise.future.map(_ ⇒ ())(system.dispatcher)
}
我们用cachedHostConnectionPool获取一个client-flow实例:
Flow[(HttpRequest,T),(Try[HttpResponse],T),HostConnectionPool]后就可以进行输入HttpRequest到HttpResponse的转换处理。如下面的例子:
val pooledFlow: Flow[(HttpRequest,Int),(Try[HttpResponse],Int),Http.HostConnectionPool] =
Http().cachedHostConnectionPool[Int](host="akka.io",port=) def sendPoolRequest(req: HttpRequest, marker: Int) = {
Source.single(req -> marker)
.via(pooledFlow)
.runWith(Sink.head)
} sendPoolRequest(HttpRequest(uri="/"), )
.andThen{
case Success((tryResp, mk)) =>
tryResp match {
case Success(resp) => println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
.andThen {case _ => sys.terminate()}
在以上这个例子里实际同样会遇到Connection-Level-Api所遇的的问题,这是因为获取的线程池内的线程还是有限的,只能缓解因为request速率超出response速率所造成的request积压。目前最有效的方法还是通过使用一个queue来暂存request后再逐个处理:
val QueueSize =
// This idea came initially from this blog post:
// http://kazuhiro.github.io/scala/akka/akka-http/akka-streams/2016/01/31/connection-pooling-with-akka-http-and-source-queue.html
val poolClientFlow = Http().cachedHostConnectionPool[Promise[HttpResponse]]("akka.io")
val queue =
Source.queue[(HttpRequest, Promise[HttpResponse])](QueueSize, OverflowStrategy.dropNew)
.via(poolClientFlow)
.toMat(Sink.foreach({
case ((Success(resp), p)) => p.success(resp)
case ((Failure(e), p)) => p.failure(e)
}))(Keep.left)
.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 responseFuture: Future[HttpResponse] = queueRequest(HttpRequest(uri = "/"))
responseFuture.andThen {
case Success(resp) => println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}.andThen {case _ => sys.terminate()}
下面是本次Akka-http-client-side-connection讨论的示范源代码:
import akka.actor._
import akka.http.javadsl.{HostConnectionPool, OutgoingConnection}
import akka.stream._
import akka.stream.scaladsl._
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._ import scala.concurrent._
import scala.util._ object ClientApiDemo extends App {
implicit val sys = ActorSystem("ClientSys")
implicit val mat = ActorMaterializer()
implicit val ec = sys.dispatcher
/*
val connFlow: Flow[HttpRequest,HttpResponse,Future[Http.OutgoingConnection]] =
Http().outgoingConnection("www.sina.com") def sendHttpRequest(req: HttpRequest) = {
Source.single(req)
.via(connFlow)
.runWith(Sink.head)
} sendHttpRequest(HttpRequest(uri="/"))
.andThen{
case Success(resp) =>
//close connection by cancelling response entity
resp.entity.dataBytes.runWith(Sink.cancelled)
println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
// .andThen {case _ => sys.terminate()} //close connection by receiving response with close header
Http().bindAndHandleSync(
{ req ⇒ HttpResponse(headers = headers.Connection("close") :: Nil) },
"akka.io",
80)(mat) val pooledFlow: Flow[(HttpRequest,Int),(Try[HttpResponse],Int),Http.HostConnectionPool] =
Http().cachedHostConnectionPool[Int](host="akka.io",port=80) def sendPoolRequest(req: HttpRequest, marker: Int) = {
Source.single(req -> marker)
.via(pooledFlow)
.runWith(Sink.head)
} sendPoolRequest(HttpRequest(uri="/"), 1)
.andThen{
case Success((tryResp, mk)) =>
tryResp match {
case Success(resp) => println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
case Failure(err) => println(s"request failed: ${err.getMessage}")
}
.andThen {case _ => sys.terminate()}
*/ val QueueSize =
// This idea came initially from this blog post:
// http://kazuhiro.github.io/scala/akka/akka-http/akka-streams/2016/01/31/connection-pooling-with-akka-http-and-source-queue.html
val poolClientFlow = Http().cachedHostConnectionPool[Promise[HttpResponse]]("akka.io")
val queue =
Source.queue[(HttpRequest, Promise[HttpResponse])](QueueSize, OverflowStrategy.dropNew)
.via(poolClientFlow)
.toMat(Sink.foreach({
case ((Success(resp), p)) => p.success(resp)
case ((Failure(e), p)) => p.failure(e)
}))(Keep.left)
.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 responseFuture: Future[HttpResponse] = queueRequest(HttpRequest(uri = "/"))
responseFuture.andThen {
case Success(resp) => println(s"got response: ${resp.status.intValue()}")
case Failure(err) => println(s"request failed: ${err.getMessage}")
}.andThen {case _ => sys.terminate()} }
Akka(36): Http:Client-side-Api,Client-Connections的更多相关文章
- Scalaz(36)- Free :实践-Free In Action - 实用体验
在上面几期讨论中我们连续介绍了Free Monad.因为FP是纯函数编程,也既是纯函数的组合集成,要求把纯代码和副作用代码可以分离开来.Free Monad的程序描述(AST)和程序实现(Interp ...
- Identity Server 4 原理和实战(完结)_建立Identity Server 4项目,Client Credentials 授权实例
创建项目 dotnet new -i IdentityServer4.Templates 多出来的这些模板 adminUI用来测试,想要用再生产环境,需要交钱 结合core的 Identity来使用 ...
- node模拟http服务器session机制-我们到底能走多远系列(36)
我们到底能走多远系列(36) 扯淡: 年关将至,总是会在一些时间节点上才感觉时光飞逝,在平时浑浑噩噩的岁月里都浪费掉了太多的宝贵.请珍惜! 主题: 我们在编写http请求处理和响应的代码的时 ...
- Windows Phone开发(36):动画之DoubleAnimation
原文:Windows Phone开发(36):动画之DoubleAnimation 从本节开始,我们将围绕一个有趣的话题展开讨论--动画. 看到动画一词,你一定想到Flash,毕竟WP应用的一个很重要 ...
- Akka(2):Actor生命周期管理 - 监控和监视
在开始讨论Akka中对Actor的生命周期管理前,我们先探讨一下所谓的Actor编程模式.对比起我们习惯的行令式(imperative)编程模式,Actor编程模式更接近现实中的应用场景和功能测试模式 ...
- Akka(6): become/unbecome:运算行为切换
通过一段时间的学习了解,加深了一些对Akka的认识,特别是对于Akka在实际编程中的用途方面.我的想法,或者我希望利用Akka来达到的目的是这样的:作为传统方式编程的老兵,我们已经习惯了直线流程方式一 ...
- Akka(8): 分布式运算:Remoting-远程查找式
Akka是一种消息驱动运算模式,它实现跨JVM程序运算的方式是通过能跨JVM的消息系统来调动分布在不同JVM上ActorSystem中的Actor进行运算,前题是Akka的地址系统可以支持跨JVM定位 ...
- Akka(33): Http:Marshalling,to Json
Akka-http是一项系统集成工具.这主要依赖系统之间的数据交换功能.因为程序内数据表达形式与网上传输的数据格式是不相同的,所以需要对程序高级结构化的数据进行转换(marshalling or se ...
- Akka(0):聊聊对Akka的初步了解和想法
前一段时间一直沉浸在函数式编程模式里,主要目的之一是掌握一套安全可靠的并发程序编程方法(concurrent programming),最终通过开源项目FunDA实现了单机多核CPU上程序的并行运算. ...
随机推荐
- Sets 比赛时想错方向了。。。。 (大数不能处理负数啊)
Sets Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...
- netsh用法
netsh(Network Shell) 是一个windows系统本身提供的功能强大的网络配置命令行工具. 导出配置脚本:netsh -c interface ip dump > c:\inte ...
- 【转】TCP/IP报文格式
1.IP报文格式 IP协议是TCP/IP协议族中最为核心的协议.它提供不可靠.无连接的服务,也即依赖其他层的协议进行差错控制.在局域网环境,IP协议往往被封装在以太网帧(见本章1.3节)中传送.而所有 ...
- HDU2282 Chocolate KM算法
第一次做这样的题,其中有几个细节是反复思考反复调试,最后一A的,ORZ,又加深了对KM算法的理解.能不参考网上的题解,而是平静下来思考,参透,最后敢于尝试.....真的很重要,以后遇到才会有更深的印象 ...
- C# Async/await 异步多线程编程
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- ovs2.7 在系统重启后,再次使用时提示数据库无法连接的问题。
问题现象如下,ovs开始安装后,对ovs的操作是正常的,但是,现在系统重启后,OVS的操作第一条命令就失败,如下: 问题解决方法: 参考 http://blog.csdn.net/xyq54/art ...
- 第五章 MySQL事务,视图,索引,备份和恢复
第五章 MySQL事务,视图,索引,备份和恢复 一.事务 1.什么是事务 事务是一种机制,一个操作序列,它包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求.要么都执行 ...
- Mvc 流程调用分析
链接地址 https://www.processon.com/view/link/59e71fbbe4b09000f03ce78e 总结: 1. 在Global.ascx 中我们使用RouteColl ...
- 【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器
[网络爬虫入门04]彻底掌握BeautifulSoup的CSS选择器 广东职业技术学院 欧浩源 2017-10-21 1.引言 目前,除了官方文档之外,市面上及网络详细介绍BeautifulSoup ...
- 解决Nginx+Tomcat时ContextPath不同的问题
1 问题描述 项目前端模板使用Thymeleaf,在对各种URL进行格式化输出时,都使用@{uri}代码.它会自动读取项目部署的虚拟路径,添加到URI的前端输出. 真实测试和生产环境中, ...