HelloworldServer.scala
object HelloworldServer {
def main(args: Array[String]): Unit = {
val conf = new SparkConf()
val securityManager = new SecurityManager(conf)
val rpcEnv: RpcEnv = RpcEnv.create("hello-service", "localhost", 52345, conf, securityManager)
val helloEndpoint: RpcEndpoint = new HelloEndpoint(rpcEnv)
rpcEnv.setupEndpoint("hello-service", helloEndpoint)
rpcEnv.awaitTermination()
}
}
class HelloEndpoint(override val rpcEnv: RpcEnv) extends RpcEndpoint {
override def onStart(): Unit = {
println("start hello endpoint")
}
override def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {
case SayHi(msg) => {
//println(s"receive $msg")
context.reply(s"Hi, $msg")
}
case SayBye(msg) => {
//println(s"receive $msg")
context.reply(s"Bye, $msg")
}
}
override def onStop(): Unit = {
println("stop hello endpoint")
}
}
HelloworldClient.scala
object HelloworldClient {
def main(args: Array[String]): Unit = {
syncCall()
}
def syncCall() = {
// 初始化RpcEnv環境
val conf = new SparkConf
// 這裏的rpc環境主機需要指定本機,端口號可以任意指定
val rpcEnv = RpcEnv.create("hello-client", "localhost", 52346, conf, new SecurityManager(conf))
// 根據Server端IP + Port獲取後端服務的引用,得到的是RpcEndpointRef類型對象
val endpointRef = rpcEnv.setupEndpointRef(RpcAddress("localhost", 52345), "hello-service")
// 1、客戶端異步請求
// 客戶端通過RpcEndpointRef#ask方法異步訪問服務端,服務端通過RpcEndpoint#receiveAndReply方法獲取到該請求後處理
val future = endpointRef.ask[String](SayBye("I am zhangsan"))
// 客戶端請求成功/失敗時的處理方法
future.onComplete {
case scala.util.Success(value) => println(s"Got the result = $value")
case scala.util.Failure(e) => println(s"Got error: $e")
}
// 客戶端等待超時時間
Await.result(future, Duration("5s"))
// 2、客戶端同步請求
val resp = endpointRef.askSync[String](SayHi("hehe"))
print(resp)
}
}