scala 使用 uberjar 进行部署时覆盖 Typesafe 配置中的多个配置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24966621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overriding multiple config values in Typesafe config when using an uberjar to deploy
提问by Soumya Simanta
I've an Akka application which uses multiple configuration values (IP address, port numbers) defined in resource/application.conf. I'm using the sbt-assemblyplugin to create an uber jar and then deploying this jar.
我有一个 Akka 应用程序,它使用resource/application.conf. 我正在使用该sbt-assembly插件创建一个 uber jar,然后部署这个 jar。
Is there a way to override the whole application.conffile by using another file that is outside the uber jar ? (i.e., values in the new conf file are used)
有没有办法application.conf通过使用 uber jar 之外的另一个文件来覆盖整个文件?(即,使用新 conf 文件中的值)
回答by y?s??la
There are various ways to achieve that:
有多种方法可以实现:
You either set a classpath to include
application.conffrom external directory and appear on the classpath before other classpath entries like your jar. To do that you can use regularjava -classpath myconfdir:theapp.jarand specify main class explicitly.You can alternatively include another conf file into your file with
include "application"directive in your conf file.You can set environment variable in
application.confthat will point to a file to include. You set env in shell afterwards.You can override values programmatically:
config.withValue("hostname", ConfigValueFactory.fromAnyRef("localhost").ActorSystemtakes a Conf object or loads from default conf if not provided.The easiestby far is to just pick another file with
-Dconfig.resource=/dev.confjava command line argument.
您可以将类路径设置为
application.conf从外部目录包含并出现在类路径上,然后出现在其他类路径条目(如您的 jar)之前。为此,您可以使用常规java -classpath myconfdir:theapp.jar并明确指定主类。您也可以使用 conf 文件中的
include "application"指令将另一个 conf 文件包含到您的文件中。您可以在
application.conf其中设置指向要包含的文件的环境变量。之后你在 shell 中设置了 env。您可以以编程方式覆盖值:
config.withValue("hostname", ConfigValueFactory.fromAnyRef("localhost").ActorSystem如果未提供,则采用 Conf 对象或从默认 conf 加载。到目前为止,最简单的方法是使用
-Dconfig.resource=/dev.confjava 命令行参数选择另一个文件。
For more details refer to official docs here.
有关更多详细信息,请参阅此处的官方文档。
回答by Joseph Lust
We do it in prod like so:
我们在 prod 中这样做:
#deploy_prod.conf
include "application"
akka.remote.hostname = "prod.blah.com"
# Example of passing in S3 keys
s3.awsAccessKeyId="YOUR_KEY"
s3.awsSecretAccessKey="YOUR_SECRET_KEY"
The above file must end in .conf. It has all the production environment specific configs, and lives outsidethe jar, so you deploy an identical Akka artifact to all servers. It will override anything in application.conf.
上述文件必须以.conf. 它具有所有特定于生产环境的配置,并且位于jar之外,因此您可以将相同的 Akka 工件部署到所有服务器。它将覆盖application.conf.
Then in the startup script:
然后在启动脚本中:
java -Dconfig.file=/full/path/deploy_prod.conf -jar your.jar com.your.Main
回答by raisercostin
I was able to programmatically override default akka config with:
我能够以编程方式覆盖默认的 akka 配置:
val customConf =
ConfigFactory.parseString(s"""
akka {
persistence.snapshot-store.local{
dir = target/snapshot
}
persistence.journal.leveldb.dir = target/journal
}
""")
val config = customConf.withFallback(original).resolve()
logger.info(config.root().render())
val system = ActorSystem("iSystem", config)

