我正在测试 finagle 并克服了当前的情况:
服务器:
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http
import com.twitter.util.{Await, Future}
class Server(name: String, port: Int) {
val service = new Service[http.Request, http.Response] {
def apply(req: http.Request): Future[http.Response] = {
println(s"Request on server $name")
Future.value(
http.Response(req.version, http.Status.Ok)
)
}
}
val server = Http.serve(s":$port", service)
}
object Server1 extends App {
val server = new Server("1", 9001).server
Await.ready(server)
}
object Server2 extends App {
val server = new Server("2", 9002).server
Await.ready(server)
}
客户:
import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http
import com.twitter.util.{Await, Future}
object Client extends App {
val client: Service[http.Request, http.Response] =
Http.client
.methodBuilder("localhost:9001,localhost:9002")
.newService("client")
while(true) {
val request = http.Request(http.Method.Get, "/")
request.host = "www.scala-lang.org"
val response: Future[http.Response] = client(request)
println(Await.result(response))
Thread.sleep(50)
}
}
Finagle 版本:
libraryDependencies ++= Seq(
"com.twitter" %% "finagle-core" % "20.4.1",
"com.twitter" %% "finagle-http" % "20.4.1",
"com.twitter" %% "finagle-serversets" % "20.4.1"
)
- 我启动两台服务器,然后启动客户端
- 客户端向服务器发送请求
- 我杀死了其中一台服务器
- 客户立即死亡
我找不到让客户端在这种情况下不死的方法,并在它再次启动时尝试重新连接到服务器