加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在scala spray框架中,如何创建具有不同配置的多个http客户端(例

发布时间:2020-12-16 08:43:50 所属栏目:安全 来源:网络整理
导读:我有两个喷雾http客户端,如下所示: val pipelineFoo: HttpRequest = Future[Foo] = ( sendReceive ~ unmarshal[Message.Foo]) val pipelineBar: HttpRequest = Future[Bar] = ( sendReceive ~ unmarshal[Message.Bar]) def execFoo(h: String,p: Int): Futu
我有两个喷雾http客户端,如下所示:

val pipelineFoo: HttpRequest => Future[Foo] = (
    sendReceive
    ~> unmarshal[Message.Foo])

  val pipelineBar: HttpRequest => Future[Bar] = (
    sendReceive
    ~> unmarshal[Message.Bar])

  def execFoo(h: String,p: Int): Future[Foo] = {
    val uri = Uri.from(scheme = "http",host = h,port = p,path = "/foo")
    pipelineFoo(Get(uri))
  }

  def execBar(h: String,p: Int): Future[Bar] = {
    val uri = Uri.from(scheme = "http",path = "/bar")
    pipelineBar(Get(uri))
  }

我希望foo请求在超时超时重试几次,并且条形码请求不会重试并且有一个短暂的超时(比如1秒).我怎样才能在喷涂中实现这一点(对不起,如果这是文档中的某个地方,但我一直无法找到它 – 我只找到了一些关于全局设置这些配置参数的文档).

解决方法

这不应该太困难. sendReceive实际上可以采用更多参数.例如,以下是其中一个备选方案的签名:

def sendReceive(transport: ActorRef)(implicit ec: ExecutionContext,futureTimeout: Timeout): SendReceive

我自己使用这个类似的场景,当我点击外部服务而不是点击我们的内部服务时,我必须有更多的重试次数和更长的超时时间.

这是我使用的管道示例:

lazy val pipeline: HttpRequest => Future[HttpResponse] = (
addCredentials(BasicHttpCredentials(clientConnection.credentials._1,clientConnection.credentials._2))
  ~> addHeader(`User-Agent`(ProductVersion("<YOUR NAME HERE>","<YOUR VERSION HERE>","http://github.com/<WHEREVER YOUR PROJECT IS>"),ProductVersion("spray-client","1.3.1","http://spray.io")))
  ~> logRequest(log)
  ~> sendReceive(clientConnection.connection)(clientConnection.context,clientConnection.timeout)
  ~> decode(Deflate)
  ~> decode(Gzip)
)

clientConnection没什么特别的.它只是我创建的一个案例类,可以通过代码手动填写,也可以在application.conf中填写一些配置

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读