java - 如何在akka项目java中使用typesafe conf

标签 java akka akka-cluster

我有一个用 Java 编写的应用程序,使用 AKKA 框架。我想在本地模式下运行该应用程序。配置文件application.conf:

    akka{
 actor {
   provider = "akka.cluster.ClusterActorRefProvider"
 }
 remote {
  transport = "akka.remote.netty.NettyRemoteTransport"

   log-remote-lifecycle-events = off
   netty.tcp {

      hostname = "127.0.0.1"
      port =2552
      maximum-frame-size = 1048576000b
      send-buffer-size = 1048576000b
      receive-buffer-size = 1048576000b

   }
 }

 cluster {
  failure-detector {
      threshold = 12
      acceptable-heartbeat-pause = 240s
      heartbeat-interval = 200s
      heartbeat-request {
        expected-response-after = 100s
      }
    }
    seed-nodes = [
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2552",
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2553",
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2554",
    ]
   auto-down-unreachable-after = 10s
 }
}

我想配置1个Master和3个worker。所以MasterMain类读取conf文件:

public class MasterMain {

  public static void main(String[] args) throws IOException {

        final int nbWorkers = 3 ;
        final int nbPartitions = 3 ;
        final String graphFile = "graph/facebook.txt" ;
        final int algo = 1 ;
        final int method = 1;               
        int port = 2552;   
        final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
        //  withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
            withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
                //withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
            withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
                withFallback(ConfigFactory.load("kcore"));

        System.out.println(configLocal.toString());

        final ActorSystem system = ActorSystem.create("MasterMain", configLocal);

        system.log().info("System will start when at least"+nbWorkers+" workers node join the cluster.");
        Cluster.get(system).registerOnMemberUp(new Runnable() {
          @Override
          public void run() {
                      system.actorOf(Props.create(Master.class, nbWorkers, nbPartitions,graphFile,algo,method  ),"master");
          }
        });

我很困惑如何正确创建 Config configLocal = ConfigFactory .....

最佳答案

创建configLocal对象时,您可以使用ConfigFactory.load()从文件加载配置。

final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
    //  withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
        withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
            //withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
        withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
            withFallback(ConfigFactory.load());

关于java - 如何在akka项目java中使用typesafe conf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51174257/

相关文章:

scala - Future[Option[Future[Option[Boolean]]] 简化 future 和期权?

AkkaHTTP : How to access captured values from the path when using mutlpart upload ( entity(as[ Multipart. 表单数据]) )

scala - 升级到1.2版本后,将Marshaller的 future 未纳入隐含范围

scala - 在集群节点上运行的 Akka Streams

具有持久邮箱的 Akka 无状态参与者

Java 集合类型不匹配 : cannot convert from TreeSet to SortedSet

java - 如何使用 QueryDSL 构建 GenericDao?

java - Scala/Lift 问题 rss feed fetch

java - 为什么我们可以将数组分配给 Java 中 Object 类型的引用?

Akka集群配置