Akka2使用探索2(Con?guration)
akka2使用Typesafe Config庫,可以使用ConfigFactory.load()加載配置文件,默認加載classpath下的application.conf, application.json and application.properties文件。ActorSystem將會把這些配置和reference.conf合并(merge)起來。
?
如果要寫akka應用,將配置寫在classpath根目錄下的application.conf文件中。
如果要寫基于akka的lib包,將配置寫在jar包內的根目錄下的reference.conf文件中.
問題:如果一個項目依賴多個基于akka的jar包,這些jar包中都有reference.conf,并且配置有沖突,它是怎么解決的呢?
?
可以合并config.
Returns a new value computed by merging this value with another, with keys in this value "winning" over the other one. Only ConfigObject and Config instances do anything in this method (they need to merge the fallback keys into themselves). All other values just return the original value, since they automatically override any fallback.
The semantics of merging are described in the spec for HOCON.
Note that objects do not merge "across" non-objects; if you write object.withFallback(nonObject).withFallback(otherObject), then otherObject will simply be ignored. This is an intentional part of how merging works. Both non-objects, and any object which has fallen back to a non-object, block subsequent fallbacks.
a.withFallback(b)? //a和b合并,如果有相同的key,以a為準
?
Clone the config with only the given path (and its children) retained; all sibling paths are removed.
a.withOnlyPath(String path)? //只取a里的path下的配置
?
Clone the config with the given path removed.
a.withoutPath(String path) //只取a里出path外的配置
?
ConfigFactory還有其他的API,用其他的方式(字符串、文件、Map、Properties、url等)加載配置文件,可以查看相應的api。
?
配置內容即可以是層級關系,也可以用”.”號分隔寫成一行:
akka {
??? host = "0.0.0.0"
??? port = 9999
}
akka.host = "0.0.0.0"
akka.port = 9999
?
配置文件還可以在java啟動參數中加載:
-Dconfig.resource=/dev.conf
?
也可以用include關鍵字引入其他的配置。比如可以把一些通用配置寫在一個common.conf文件中,在自己的配置中只寫個性配置,然后include “common”:
calculator {
? include "common"
? akka {
??? remote.netty.port = 2552
? }
}
轉載于:https://blog.51cto.com/xiefeifeihu/950593
總結
以上是生活随笔為你收集整理的Akka2使用探索2(Con?guration)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocos2d-x 旅程開始--(实现单
- 下一篇: 【HDU1325】Is It A Tre