score:6

Setting up the whole thing for the first time is a pain but if you do it once you will have a good skeleton that you will user on regular basis.

I've written in comment below the question user clustering not remoting.

Here is how I do it: I set up an sbt root project with three sub-projects.

  • common
  • frontend
  • backend

In common you put everything that is common to both projects e.g. the messages that they share, actor classes that are created in frontend and deployed to backend.

Put a reference.conf to common project, here is mine:

akka {
loglevel = INFO
actor {
    provider = "akka.cluster.ClusterActorRefProvider"
    debug {
        lifecycle = on
    }
}

 cluster {
      seed-nodes = [
        "akka.tcp://application@127.0.0.1:2558",
        "akka.tcp://application@127.0.0.1:2559"
      ]
  }

}

Now in the frontend:

akka {


    remote {
        log-remote-lifecycle-events = off
        netty.tcp {
            hostname = "127.0.0.1"
            port = 2558
        }
    }

    cluster {
      auto-down = on
      roles = [frontend]
    }
}

and the backend

akka {

    remote {
        log-remote-lifecycle-events = off
        netty.tcp {
            hostname = "127.0.0.1"
            port = 0
        }
    }

    cluster {
      auto-down = on
      roles = [backend]
    }
}

This will work like this: You start the fronted part first which will control the cluster. Then you can start any number of backends you want that will join automatically (look at the port, it's 0 so it will be chosen randomly).

Now you need to add the whole logic to the frontend main: Create the actor system with name application:

val system = ActorSystem("application")

Do the same at the backend main.

Now write your code in fronted so it will create your workers with a router, here's my example code:

context.actorOf(ServiceRuntimeActor.props(serviceName)
        .withRouter(
          ClusterRouterConfig(ConsistentHashingRouter(),
            ClusterRouterSettings(
              totalInstances = 10, maxInstancesPerNode = 3,
            allowLocalRoutees = false, useRole = Some("backend"))
          )
        ),
        name = shortServiceName)

just change your ServiceRuntimeActor to name of your worker. It will deploy workers to all backends that you've started and limit this to max 3 per node and max 10 in total.

Hope this will help.


Related Query

More Query from same tag