【问题标题】:How to configure play application with Akka to run in a cluster如何使用 Akka 配置 play 应用程序以在集群中运行
【发布时间】:2019-11-11 04:46:41
【问题描述】:

我有一个想要在集群中运行的 PlayFramework (play-scala) 应用程序。所以我可能有几个这个应用程序镜像的 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
  }
}

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

【问题讨论】:

    标签: playframework akka akka-cluster


    【解决方案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)分配给application.conf 中的akka.discovery.method,可能类似于以下内容:

    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 分配给application.conf 中的kubernetes-api,如下所示:

    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}
        }
      }
    }
    

    【讨论】:

    • 谢谢。您能否提供与 AWS Beanstalk 一起使用的建议配置?
    • @Shafique Jamal,很遗憾我手头没有任何相关的 Beanstalk 配置示例可供分享。您可能需要考虑发布一个具有特定要求的单独问题。顺便说一句,有一篇不错的(虽然有点过时)博客文章 comparing Beanstalk, ECS and Kubernetes 可能很有趣。
    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多