如何在Finagle RPC系统中使用Filter with Service?

How to use Filter with Service in Finagle RPC system?

我正在学习 twitter 的 RPC 异步系统,它很重要使用过滤器和服务处理任务 logical.I 已经阅读了 https://twitter.github.io/finagle/guide/ServicesAndFilters.html 的论文并尝试编写一个简单的代码来实现我知道的。但是有一些问题:

import com.twitter.util.Future

trait Service[Req, Rep] extends (Req => Future[Rep])

abstract class Filter[-ReqIn, +RepOut, +ReqOut, -RepIn]
  extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut])

trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

class DoubleFilter(times: Int) extends SimpleFilter[(Int,Int), Int] {
  def apply(request: (Int,Int), service: Service[(Int,Int), Int]): Future[Int] = {
    service((request._1 * times, request._2 * times))
  }
}

class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
  def apply(request: (Int, Int), service: Service[(Int, Int),Int]): Future[Int] = {
    service((request._1 + count, request._2 + count))
  }
}

object Test extends App {
  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)

  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int,Int), Int] {
    def apply(req: (Int,Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }

  val serviceWithDoble = doubleFilter andThen addELemsService

  val doubleWithAddFilter = doubleFilter andThen addFilter
}

有两个编译错误的结果:value andThen 不是 DoubleFilter 的成员!
我想念什么明白吗?我直认为医生忽略了一些东西,但我误导了。

我认为您导入错误trait/class。我尝试使用正确的导入编译以下代码并且编译没有错误。

import com.twitter.finagle.{Service, Filter}
import com.twitter.util.Future


object Test extends App {

  trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

  class DoubleFilter(times: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 * times, request._2 * times))
    }
  }

  class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 + count, request._2 + count))
    }
  }

  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)
  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int, Int), Int] {
    def apply(req: (Int, Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }
  val serviceWithDoble = doubleFilter andThen addELemsService
  val doubleWithAddFilter = doubleFilter andThen addFilter

}