Akka从源代码修改/创建配置文件

Sar*_*ris 2 java akka typesafe-config

是否可以从源代码修改或创建配置文件.我正在创建一些带有远程处理的客户端/服务器架构.我想要实现的是能够启动客户端应用程序,例如:主机/端口,当没有配置文件时,创建一个完成命令行args.

akka {
  actor {
    provider = remote
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1" <--- here 
      port = 2553 <--- here
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

配置并不是很复杂.我想从源端口改变端口(最终主机,现在它无论如何都是localhost用于测试),以便自动化它,所以我可以通过将它们传递给main函数来运行多个客户端.

Jef*_*ung 6

是的,您可以在代码中修改或创建配置.以下摘录来自Akka 文档:

以编程方式修改配置的示例:

// make a Config with just your special setting
Config myConfig = ConfigFactory.parseString("something=somethingElse");

// load the normal config stack (system props, then application.conf, then reference.conf)
Config regularConfig = ConfigFactory.load();

// override regular stack with myConfig
Config combined = myConfig.withFallback(regularConfig);

// put the result in between the overrides (system props) and defaults again
Config complete = ConfigFactory.load(combined);

// create ActorSystem
ActorSystem system = ActorSystem.create("myname", complete);
Run Code Online (Sandbox Code Playgroud)

以编程方式创建配置的示例(这是在Scala中,但您可以将其调整为Java):

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory

val customConf = ConfigFactory.parseString("""
  akka.actor.deployment {
    /my-service {
      router = round-robin-pool
      nr-of-instances = 3
    }
  }
""")

// ConfigFactory.load sandwiches customConfig between default reference
// config and default overrides, and then resolves it.
val system = ActorSystem("MySystem", ConfigFactory.load(customConf))
Run Code Online (Sandbox Code Playgroud)