Websocket和Akka流对背压的作用

因此,在上一篇文章中,我展示了如何使用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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值