akka http:服务器绑定未按预期运行
akka http: Server Binding not behaving as expected
我有以下代码:
val serverSource = Http().bind("localhost", 8080)
val connectionSink = Sink.foreach[IncomingConnection] {
connection => println(s"Accepted incoming connection from ${connection.remoteAddress}")
}
val serverBindingFuture = serverSource.to(connectionSink).run()
serverBindingFuture.onComplete {
case Success(binding) => {
println("Server binding successful")
binding.terminate(10 seconds)
}
case Failure(ex) => println(s"Server binding failed $ex")
}
如代码片段所示,我将在 10 秒后终止服务器绑定。所以我希望如果我在此期限到期之前发送请求,我应该会打印消息 'Accepted incoming connection from...'。
但我发现我总是收到 'This site can’t be reached',并且消息从未打印出来。
As the snippet tells, I am terminating the server binding after 10 seconds. So I expect that if I send the request before this period expires, I should get message 'Accepted incoming connection from...' printed.
您误解了 terminate
的语义。 duration 参数是一个 hard 关闭的最后期限,并不表示服务器将在此之前继续接受请求。
这里对您的代码进行了调整以证明这一点。
- 服务器一启动就打印
Server binding successful
并等待10秒再调用terminate
- 在调用
terminate
之前打印 Starting shutdown
并在 terminate
完成后立即打印 Finished shutdown
你应该看到:
curl 'http://localhost:8080/api
触发 println
直到 Starting shutdown
出现
Starting shutdown
和 Finished shutdown
之间的延迟可能很短(比 10 秒短得多)
val serverSource = Http().bind("localhost", 8080)
val connectionSink = Sink.foreach[IncomingConnection] {
connection => println(s"Accepted incoming connection from ${connection.remoteAddress}")
}
val serverBindingFuture = serverSource.to(connectionSink).run()
serverBindingFuture.onComplete {
case Success(binding) => {
println("Server binding successful")
system.scheduler.scheduleOnce(10 seconds) {
println("Starting shutdown")
binding.terminate(10 seconds).onComplete { termOutcome =>
println(s"Finished shutdown $termOutcome")
}
}
}
case Failure(ex) => println(s"Server binding failed $ex")
}
我有以下代码:
val serverSource = Http().bind("localhost", 8080)
val connectionSink = Sink.foreach[IncomingConnection] {
connection => println(s"Accepted incoming connection from ${connection.remoteAddress}")
}
val serverBindingFuture = serverSource.to(connectionSink).run()
serverBindingFuture.onComplete {
case Success(binding) => {
println("Server binding successful")
binding.terminate(10 seconds)
}
case Failure(ex) => println(s"Server binding failed $ex")
}
如代码片段所示,我将在 10 秒后终止服务器绑定。所以我希望如果我在此期限到期之前发送请求,我应该会打印消息 'Accepted incoming connection from...'。
但我发现我总是收到 'This site can’t be reached',并且消息从未打印出来。
As the snippet tells, I am terminating the server binding after 10 seconds. So I expect that if I send the request before this period expires, I should get message 'Accepted incoming connection from...' printed.
您误解了 terminate
的语义。 duration 参数是一个 hard 关闭的最后期限,并不表示服务器将在此之前继续接受请求。
这里对您的代码进行了调整以证明这一点。
- 服务器一启动就打印
Server binding successful
并等待10秒再调用terminate
- 在调用
terminate
之前打印Starting shutdown
并在terminate
完成后立即打印Finished shutdown
你应该看到:
curl 'http://localhost:8080/api
触发println
直到Starting shutdown
出现Starting shutdown
和Finished shutdown
之间的延迟可能很短(比 10 秒短得多)
val serverSource = Http().bind("localhost", 8080)
val connectionSink = Sink.foreach[IncomingConnection] {
connection => println(s"Accepted incoming connection from ${connection.remoteAddress}")
}
val serverBindingFuture = serverSource.to(connectionSink).run()
serverBindingFuture.onComplete {
case Success(binding) => {
println("Server binding successful")
system.scheduler.scheduleOnce(10 seconds) {
println("Starting shutdown")
binding.terminate(10 seconds).onComplete { termOutcome =>
println(s"Finished shutdown $termOutcome")
}
}
}
case Failure(ex) => println(s"Server binding failed $ex")
}