Akka http客户端上的断路器可以';失败后不要退缩

Akka http客户端上的断路器可以';失败后不要退缩,akka,akka-http,circuit-breaker,Akka,Akka Http,Circuit Breaker,akka http客户端上断路器的一个示例看起来非常简单,但对我来说,它不起作用 object HttpWithCircuitBreaker extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher val breaker = new CircuitBreaker(

akka http客户端上断路器的一个示例看起来非常简单,但对我来说,它不起作用

object HttpWithCircuitBreaker extends App {
  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val ec = system.dispatcher
  val breaker =
    new CircuitBreaker(
      system.scheduler,
      maxFailures = 2,
      callTimeout = 3.seconds,
      resetTimeout = 25.seconds)
      .onOpen(println("circuit breaker opened"))
      .onClose(println("circuit breaker closed"))
      .onHalfOpen(println("circuit breaker half-open"))
  while (true) {
    val futureResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "https://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new"))
    breaker.withCircuitBreaker(futureResponse).map(resp => resp.status match {
      case Success(_) =>
        resp.entity.dataBytes.runWith(Sink.ignore)
        println("http success")
      case _ =>
        resp.entity.dataBytes.runWith(Sink.ignore)
        println(s"http error ${resp.status.intValue()}")
    }).recover {
      case e@_ =>
        println(s"exception ${e.getMessage}")
    }
    Thread.sleep(1000)
  }
}
它从完美的随机数开始。它在断开网络连接时打开,但在重新连接时从不关闭。从日志中可以看到,有人试图恢复,但由于超时而失败:

http success
http success
http success
exception Tcp command [Connect(www.random.org:443,None,List(),Some(10 seconds),true)] failed
exception Circuit Breaker Timed out.
circuit breaker opened
exception Tcp command [Connect(www.random.org:443,None,List(),Some(10 seconds),true)] failed
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker Timed out.
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
circuit breaker half-open             <--- new http call should start here
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker Timed out.  <--- but it fails with timeout
circuit breaker opened
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
exception Circuit Breaker is open; calls are failing fast
http成功
http成功
http成功
异常Tcp命令[Connect(www.random.org:443,None,List(),Some(10秒),true)]失败
异常断路器超时。
断路器断开
异常Tcp命令[Connect(www.random.org:443,None,List(),Some(10秒),true)]失败
异常断路器开路;电话故障很快
异常断路器超时。
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快
异常断路器开路;电话故障很快

断路器半开为使断路器正常工作,此
val

val futureResponse: Future[HttpResponse] = Http().singleRequest(...)
应该是
def

def futureResponse: Future[HttpResponse] = Http().singleRequest(...)
断路器需要包装异步调用-如果使用
val
,则异步调用将在断路器外部启动


另外:不要在代码中使用Thread.sleep,尝试使用-e.g.-Akka

谢谢。我终于明白了那是怎么回事。