2017-05-24 74 views
1

是否可以從源代碼修改或創建配置文件。我正在使用遠程創建一些客戶端/服務器體系結構。我想要實現的是啓動客戶端應用程序的能力,例如:主機/端口以及當沒有配置文件創建一個滿足命令行參數時。Akka從源代碼修改/創建配置文件

akka { 
    actor { 
    provider = remote 
    } 
    remote { 
    enabled-transports = ["akka.remote.netty.tcp"] 
    netty.tcp { 
     hostname = "127.0.0.1" <--- here 
     port = 2553 <--- here 
    } 
    } 
} 

配置不是很複雜。我想從源端改變端口(最終主機,現在它是本地主機無論如何測試)自動化一點,所以我可以運行多個客戶端只需將它們傳遞給主函數。

回答

3

是的,您可以在代碼中修改或創建配置。以下摘錄是從阿卡documentation

編程修改配置的一個例子:

// 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); 

編程方式創建一個配置(這是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))