• spark下测试akka的分布式通讯功能


    采用的spark版本为1.1.0

    scala版本为2.10.4

    • 编写scala类文件myactors.scala:

      package bluejoe
      
      import akka.actor._
      import com.typesafe.config.ConfigFactory
      import akka.remote.RemoteScope
      
      class LocalActor extends akka.actor.Actor {
      
        //Get a reference to the remote actor
        val remoteActor = context.actorFor("akka://RemoteNodeApp@10.0.224.170:2552/user/remoteActor")
        def receive: Receive = {
          case message: String =>
           println("<<<<<<<<<<<<<"+message)  
      }
      }
      
      class RemoteActor extends akka.actor.Actor {
        def receive: Receive = {
          case message: String =>
            // Get reference to the message sender and reply back
            println(">>>>>>>>>>>>"+message)
        }
      }


    • 编译生成class文件:

      scalac -classpath /usr/local/spark/lib/spark-assembly-1.1.0-hadoop2.3.0.jar myactors.scala

    • 启动spark-shell,注意指定classpath:

      spark-shell --driver-class-path /root


    • 准备2个配置文件,remote.conf内容如下:

      RemoteSys {
      	akka {
      		actor {
      	    		provider = "akka.remote.RemoteActorRefProvider"
      	  	}
      	   remote {
      	    transport = "akka.remote.netty.NettyRemoteTransport"
      	    netty {
      	      hostname = "10.0.224.170"
      	      port = 2552
      	    }
      	  }
      	}
      }


    • client.conf的内容如下:

      LocalSys {
              akka {
                      actor {
                      provider = "akka.remote.RemoteActorRefProvider"
                      }
              }
      }


    • 在spark-shell中输入server端代码:

      import bluejoe._
      import akka.actor._
      import com.typesafe.config.ConfigFactory
      
      
      val system = ActorSystem("RemoteNodeApp", ConfigFactory.parseFile(new java.io.File("/root/remote.conf")).getConfig("RemoteSys"))
      val remoteActor = system.actorOf(Props[RemoteActor], name = "remoteActor")
      


    • 启动另外一个spark-shell,输入client端代码:

      import bluejoe._
      import akka.actor._
      import com.typesafe.config.ConfigFactory
      
      // load the configuration
      val config = ConfigFactory.parseFile(new java.io.File("/root/client.conf")).getConfig("LocalSys")
      val system = ActorSystem("LocalNodeApp", config)
      val clientActor = system.actorOf(Props[LocalActor])
      clientActor ! "Hello"
      Thread.sleep(4000)
      system.shutdown()


  • 相关阅读:
    es6异步编程 Promise 讲解 --------各个优点缺点总结
    js重新讲解继承,es5的一些继承,es6继承的改变 ----------由浅入深
    node.js里的buffer常见操作,copy,concat等实例讲解
    node.js 写流 createWriteStream----由浅入深
    node.js 读取文件--createReadStream
    Java的位运算符—— 与(&)、非(~)、或(|)、异或(^)
    XML的特殊字符处理
    mysql语句收藏
    MYSQL学习
    利用HTML5 LocalStorage实现跨页面通信channel
  • 原文地址:https://www.cnblogs.com/bluejoe/p/5115840.html
Copyright © 2020-2023  润新知