因此,在上一篇文章中,我展示了如何使用akka-streams创建一个websocket服务器 。 在本后续文章中,我们将进一步了解反压与websockets一起工作的方式(以及可能在akka之上的任何基于TCP的协议)。 为了向您展示这一点,我们将使用与可视化反压文章中相同的设置。 在那里,我们使用了以下工具:
- akka-mon :一些监视工具,可用于参与者发送有关指标的事件。
- Statsd :提供akka-mon可向其发送指标的UDP API。 Statsd收集这些指标并将其发送到石墨和influxdb等系统。
- Influxdb :我们使用它来收集各种指标。
- Grafana :Grafana可以可视化存储在influxDB中的数据。 我们将使用它来创建显示反压效果的折线图。
在本文中,我们不会过多地研究细节,有关更多信息,请参阅上一篇文章(todo:链接)。
我们离开的地方
让我们快速看一下上一篇文章中创建的websocket服务器(的一部分):
val binding = Http().bindAndHandleSync({
case WSRequest(req@HttpRequest(GET, Uri.Path("/simple"), _, _, _)) => handleWith(req, Flows.reverseFlow)
case WSRequest(req@HttpRequest(GET, Uri.Path("/echo"), _, _, _)) => handleWith(req, Flows.echoFlow)
case WSRequest(req@HttpRequest(GET, Uri.Path("/graph"), _, _, _)) => handleWith(req, Flows.graphFlow)
case WSRequest(req@HttpRequest(GET, Uri.Path("/graphWithSource"), _, _, _)) => handleWith(req, Flows.graphFlowWithExtraSource)
case WSRequest(req@HttpRequest(GET, Uri.Path("/stats"), _, _, _)) => handleWith(req, Flows.graphFlowWithStats(router, req.getUri().parameter("id")))
case _: HttpRequest => HttpResponse(400, entity = "Invalid websocket request")
}, interface = "localhost", port = 9001)
为了测试背压,我们使用/ stats路由,在这种情况下我们将其更改。 此路由通过以下路由提供一组统计信息:
def graphFlowWithStats(router: ActorRef, id: Option[String]): Flow[Message, Message, Unit] = {
Flow() { implicit b =>
import FlowGraph.Implicits._
id match {
case Some(i) => println(s"Connection received for stats from id: $i")
case _ => println(s"Connection received for stats no id")
}
// create an actor source
val source = Source.actorPublisher[String](Props(classOf[VMStatsPublisher],router, id))
// Graph elements we'll use
val merge = b.add(Merge[String](2))
val filter = b.add(Flow[String].filter(_ => false))
// c