Scala中的环境特定配置 [英] specific config by environment in Scala

查看:33
本文介绍了Scala中的环境特定配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在 Scala 中设置项目的好方法,该项目根据环境使用不同的配置.

What is a good way to set up a project in Scala which uses different configuration depending on environments.

我需要专门为开发测试生产环境设置不同的数据库(类似于在Rails中所做的)

I need to specifically have different databases for development, test and production environment (similar to what is done in Rails)

推荐答案

我正在使用的另一个策略是使用 包括.我通常将我的 DEV 设置存储在 default application.conf文件,然后我为其他环境创建一个新的 conf 文件并包含默认文件.

Another strategy I'm using consists of using includes. I usually store my DEV settings in the default application.conf file then I create a new conf file for other environments and include the default one.

假设我的 DEV conf application.conf 如下所示:

Let's say my DEV conf application.conf looks like this:

myapp {
    server-address = "localhost"
    server-port = 9000

    some-other-setting = "cool !"
}

那么对于 PROD,我可以有另一个名为 prod.conf 的文件:

Then for the PROD, I could have another file called prod.conf:

include "application"

# override default (DEV) settings
myapp {
    server-address = ${PROD_SERVER_HOSTNAME}
    server-port = ${PROD_SERVER_PORT}
}

请注意,我覆盖了在 PROD 环境中更改的设置(some-other-setting 因此与 DEV 中的相同).

Note that I override only the settings that change in the PROD environment (some-other-setting is thus the same as in DEV).

配置引导代码不测试任何东西

The config bootstrap code doesn't test anything

...
val conf = ConfigFactory.load()
...

要从 DEV 切换到 PROD conf,只需传递一个带有要加载的配置文件名称的系统属性:

To switch from the DEV to the PROD conf, simply pass a system property with the name of the config file to load:

java -Dconfig.resource=prod.conf ...

在 DEV 中,不需要传递它,因为 application.conf 将由 默认.

In DEV, no need to pass it since application.conf will be loaded by default.

所以我们在这里使用 Typesafe Config 的默认加载机制来实现这一点.

So here we're using Typesafe Config's default loading mechanism to achieve this.

我创建了一个简单的 项目 来演示这种技术.随意克隆和试验.

I've created a simple project to demonstrate this technique. Feel free to clone and experiment.

这篇关于Scala中的环境特定配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆