maven项目实现master和worker通信

 pom

<properties>
  <scala.version>2.10.6</scala.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>${scala.version}</version>
  </dependency>


  <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
  <dependency>
    <groupId>com.typesafe.akka</groupId>
    <artifactId>akka-actor_2.10</artifactId>
    <version>2.2.2</version>
  </dependency>

    <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-remote -->
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-remote_2.10</artifactId>
        <version>2.2.2</version>
    </dependency>

</dependencies>
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.0</version>
              <configuration>
                  <source>6</source>
                  <target>6</target>
                  <encoding>UTF-8</encoding>
              </configuration>
              <executions>
                  <execution>
                      <phase>compile</phase>
                      <goals>
                          <goal>compile</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>

          <plugin>
              <groupId>net.alchim31.maven</groupId>
              <artifactId>scala-maven-plugin</artifactId>
              <version>3.2.1</version>
              <executions>
                  <execution>
                      <id>scala-compile-first</id>
                      <phase>process-resources</phase>
                      <goals>
                          <goal>add-source</goal>
                          <goal>compile</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.5.5</version>
              <configuration>
                  <archive>
                      <manifest>
                          <mainClass>scalaAkka.Worker</mainClass>
                      </manifest>
                  </archive>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
              </configuration>
              <executions>
                  <execution>
                      <id>make-assembly</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
  </build>

master

package scalaAkka

import akka.actor.{Actor, ActorSystem, Props}
import com.typesafe.config.ConfigFactory

class Master extends Actor{



  override def receive: Receive ={
    case "connect" =>{

      println("connected")
      sender ! "reply"
    }
  }
}

object Master{
  def main(args: Array[String]): Unit = {


    println("aaaaa")
    val host = "10.1.30.143"
    val port = 8009
    val configStr =
      s"""
         |akka.actor.provider = "akka.remote.RemoteActorRefProvider"
         |akka.remote.netty.tcp.hostname = "$host"
         |akka.remote.netty.tcp.port = "$port"
       """.stripMargin
    val config = ConfigFactory.parseString(configStr)
    val system = ActorSystem("Master",config )
    val master = system.actorOf(Props(new Master),"master")
    system.awaitTermination()



  }
}

worker

package scalaAkka

import akka.actor.{Actor, ActorSelection, ActorSystem, Props}
import com.typesafe.config.ConfigFactory

class Worker(masterHost:String,masterPort:Int) extends Actor{

  var master:ActorSelection = _

  override def preStart(): Unit = {
    master =context.actorSelection(s"akka.tcp://Master@$masterHost:$masterPort/user/master")
    println("connect send ")
    master ! "connect"
  }

  override def receive: Receive = {

    case "reply" => println("reply from master")
  }
}

object  Worker{
  def main(args: Array[String]): Unit = {
    val host = "10.1.30.143"
    val port = 61616
    val configStr =
      s"""
         |akka.actor.provider = "akka.remote.RemoteActorRefProvider"
         |akka.remote.netty.tcp.hostname = "$host"
         |akka.remote.netty.tcp.port = "$port"
       """.stripMargin
    val config = ConfigFactory.parseString(configStr)
    val system = ActorSystem("worker",config)
    val worker = system.actorOf(Props(new Worker("10.1.30.143",8009)),"worker")
    system.awaitTermination()
  }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值