如何使用Akka配置Play应用程序以在集群中运行

时间:2019-06-29 19:14:06

标签: playframework akka akka-cluster

我有一个要在群集中运行的PlayFramework(播放标量)应用程序。因此,我可能正在运行此应用程序映像的多个docker容器。我不知道这些IP地址的提前时间,因为云服务提供商可以动态启动和停止它们,所以我无法指定种子节点。另外,对于每个应用程序实例,所有application.conf文件都应该相同吗?

我如何配置play应用程序以使该应用程序的每个实例都能发现并加入Akka集群?

我看过: https://www.playframework.com/documentation/2.7.x/ScalaAkka#Akka-Cluster Akka cluster setup with play framework https://github.com/lregnier/play-akka-cluster-aws

由于无法指定种子节点,是否必须使用Akka Cluster Bootstrap

足以在application.conf文件中包含以下内容(取自Cluster Usage

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

  cluster {
    seed-nodes = [
      "akka.tcp://ClusterSystem@127.0.0.1:2551",
      "akka.tcp://ClusterSystem@127.0.0.1:2552"]

    # auto downing is NOT safe for production deployments.
    # you may want to use it during development, read more about it in the docs.
    #
    # auto-down-unreachable-after = 10s
  }
}

但是没有种子节点吗?如果是这样,节点如何发现并加入集群?

1 个答案:

答案 0 :(得分:1)

鉴于此要求,最好的选择可能是使用Akka Cluster Bootstrap。要运行基于容器的服务,service discovery使用AWS ECSKubernetes可能更接近您的需求。

Akka Cluster Bootstrap通过自动集群引导机制解决了您没有预先分配的种子节点的需求。每个节点都通过其公开的HTTP端点进行相互探测,如果不存在seed nodes(即不存在现有群集),则地址“最低”的节点将自己成为seed node,提示其他节点加入新形成的集群。有关更多详细信息,请参见此Akka doc re: cluster bootstrap

如Akka文档中所述,Akka Cluster Bootstrap取决于模块Akka DiscoveryAkka Management

libraryDependencies ++= Seq(
  "com.lightbend.akka.management" %% "akka-management-cluster-bootstrap" % "1.0.1",
  "com.typesafe.akka" %% "akka-discovery" % "2.5.21"
)

对于service discovery using ECS,将aws-api-ecsaws-api-ecs-async(用于非阻塞IO)分配给akka.discovery.method中的application.conf,可能类似于以下内容:< / p>

akka {
  cluster {
    seed-nodes = []
    seed-nodes = ${?SEED_NODES}
  }
  # ...
  management {
    cluster.bootstrap {
      contact-point-discovery {
        required-contact-point-nr = 2
        required-contact-point-nr = ${?REQUIRED_CONTACT_POINTS}
      }
    }
    # ...
  }
  discovery {
    method = aws-api-ecs-async
    aws-api-ecs-async {
      cluster = "my-ecs-cluster"
    }
  }
}

对于service discovery using Kubernetes,应该在akka.discovery.method中为kubernetes-api分配application.conf,如下所示:

akka {
  cluster {
    seed-nodes = []
    seed-nodes = ${?SEED_NODES}
  }
  # ...
  management {
    cluster.bootstrap {
      contact-point-discovery {
        required-contact-point-nr = 2
        required-contact-point-nr = ${?REQUIRED_CONTACT_POINTS}
      }
    }
    # ...
  }
  discovery {
    method = kubernetes-api
    kubernetes-api {
      pod-namespace = "default"
      pod-namespace = ${?K8S_NAMESPACE}
      pod-label-selector = "app=akka-cluster"
      pod-label-selector = ${?K8S_SELECTOR}
      pod-port-name = "cluster-mgmt-port"
      pod-port-name = ${?K8S_MANAGEMENT_PORT}
    }
  }
}