scala 如何在 Play 2.0 中为每个环境设置不同的数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10391987/
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
How to set up different databases per environment in Play 2.0?
提问by Bill
I'd like my Play app to use different databases for test, local and production (production is Heroku) environments.
我希望我的 Play 应用程序对测试、本地和生产(生产是 Heroku)环境使用不同的数据库。
In application.confI have:
在application.conf我有:
db.default.driver=org.postgresql.Driver
%dev.db.default.url="jdbc:postgresql://localhost/foobar"
%test.db.default.url="jdbc:postgresql://localhost/foobar-test"
%prod.db.default.url=${DATABASE_URL}
This doesn't seem to work. When I run play testor play run,
all DB access fails with:
这似乎不起作用。当我运行play test或 时play run,所有数据库访问都失败了:
Configuration error [Missing configuration [db.default.url]] (Configuration.scala:258)
I have a few questions about this:
我对此有几个问题:
In general, I'm a little confused about how databases are configured in Play: it looks like there's plain
db,db.[DBNAME]anddb. [DBNAME].urland different tutorials make different choices among those. Certain expressions that seem like they should work (e.g.db.default.url = "jdbc:..."fail with an error that a string was provided where an object was expected).I've seen other people suggest that I create separate
prod.conf,dev.confandtest.conffiles that each includeapplication.confand then contain DB-specific configuration. But in that case, how do I specify what database to use when I runtestfrom the Play console?Is the
%envsyntax supposed to work in Play 2?What's the correct way to specify an environment for
play testto use?
在一般情况下,我感到困惑的数据库是如何在游戏配置一点:它看起来像有平原
db,db.[DBNAME]并db. [DBNAME].url与不同的教程让那些之间不同的选择。某些看起来应该可以工作的表达式(例如db.default.url = "jdbc:...",在预期对象的位置提供字符串的错误导致失败)。我见过其他人建议我创建单独的
prod.conf,dev.conf和test.conf文件,每个文件都包含application.conf然后包含特定于数据库的配置。但在这种情况下,当我test从 Play 控制台运行时,如何指定要使用的数据库?是
%env语法应该工作,播放2?指定
play test要使用的环境的正确方法是什么?
采纳答案by James Ward
In Play 2 there aren't different config environments. Instead you just set or override the config parameters in the conf/application.conffile. One way to do it is on the playcommand line, like:
在 Play 2 中没有不同的配置环境。相反,您只需设置或覆盖conf/application.conf文件中的配置参数。一种方法是在play命令行上,例如:
play -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL ~run
You can also tell Play to use a different config file:
您还可以告诉 Play 使用不同的配置文件:
play -Dconfig.file=conf/prod.conf ~run
For an example Procfile for Heroku, see:
https://github.com/jamesward/play2bars/blob/scala-anorm/Procfile
有关 Heroku 的示例 Procfile,请参阅:https:
//github.com/jamesward/play2bars/blob/scala-anorm/Procfile
More details in the Play Docs:
http://www.playframework.org/documentation/2.0/Configuration
Play 文档中的更多详细信息:http:
//www.playframework.org/documentation/2.0/Configuration
回答by Marcus Schiesser
At least in Play 2.1.1 it is possibly to override configuration values with environment variables, if they are set. (For details see: http://www.playframework.com/documentation/2.1.1/ProductionConfiguration)
至少在 Play 2.1.1 中,如果设置了环境变量,则可能会覆盖配置值。(详情见:http: //www.playframework.com/documentation/2.1.1/ProductionConfiguration)
So you can set the following in your conf/application.conf:
所以你可以在你的中设置以下内容conf/application.conf:
db.default.url="jdbc:mysql://localhost:3306/my-db-name"
db.default.url=${?DATABASE_URL_DB}
per default it will use the JDBC-URL defined unless the environment variable DATABASE_URL_DBdefines a value for it.
So you just set your development database in the configuration and for production or stages you define the environment variable.
默认情况下,它将使用定义的 JDBC-URL,除非环境变量DATABASE_URL_DB为其定义了值。因此,您只需在配置中设置开发数据库,并为生产或阶段定义环境变量。
But beware, this substitution does NOT WORK if you put your variable reference inside quoted strings:
但请注意,如果您将变量引用放在带引号的字符串中,则此替换不起作用:
db.default.url="jdbc:${?DATABASE_URL_DB}"
Instead, just unquote the section to be substituted, for example.
相反,只需取消引用要替换的部分,例如。
database_host = "localhost"
database_host = ${?ENV_DATABASE_HOST}
db.default.url="jdbc:mysql://"${?database_host}":3306/my-db-name"
In this example, localhost will be used by default if the environment variable ENV_DATABASE_HOSTis not set. (For details see: https://www.playframework.com/documentation/2.5.x/ConfigFile#substitutions)
在此示例中,如果ENV_DATABASE_HOST未设置环境变量,则默认使用 localhost 。(详情见:https: //www.playframework.com/documentation/2.5.x/ConfigFile#substitutions)
回答by KajMagnus
You can actually still use the Play 1.0 config value naming method, in Play 2, if you, when you load config values, check if Play.isTest, and then prefix the properties you load with 'test.'. Here's a snipped:
您实际上仍然可以使用 Play 1.0 配置值命名方法,在 Play 2 中,如果您在加载配置值时检查 if Play.isTest,然后在加载的属性前加上“test.”前缀。这是一个剪辑:
def configPrefix = if (play.api.Play.isTest) "test." else ""
def configStr(path: String) =
Play.configuration.getString(configPrefix + path) getOrElse
die(s"Config value missing: $configPrefix$path")
new RelDb(
server = configStr("pgsql.server"),
port = configStr("pgsql.port"),
database = configStr("pgsql.database"),
user = ...,
password = ...)
And the related config snippet:
以及相关的配置片段:
pgsql.server="192.168.0.123"
pgsql.port="5432"
pgsql.database="prod"
...
test.pgsql.server="192.168.0.123"
test.pgsql.port="5432"
test.pgsql.database="test"
...
Now you don't need to remember setting any system properties when you run your e2e test suite, and you won't accidentally connect to the prod database.
现在,您无需记住在运行 e2e 测试套件时设置任何系统属性,也不会意外连接到 prod 数据库。
I suppose that you can optionally place the test.values in a separate file, which you would then include at the end of the main config file I think.
我想您可以选择将这些test.值放在一个单独的文件中,然后您可以将其包含在我认为的主配置文件的末尾。
回答by paullabis
There is another approach which is to override Global / GlobalSettings method onLoadConfigand from there you can setup application configuration with generic config and specific environment configuration like below...
还有另一种方法是覆盖 Global / GlobalSettings 方法onLoadConfig ,然后您可以使用通用配置和特定环境配置来设置应用程序配置,如下所示...
conf/application.conf --> configurations common for all environment
conf/dev/application.conf --> configurations for development environment
conf/test/application.conf --> configurations for testing environment
conf/prod/application.conf --> configurations for production environment
You can check http://bit.ly/1AiZvX5for my sample implementation.
您可以查看http://bit.ly/1AiZvX5以获取我的示例实现。
Hope this helps.
希望这可以帮助。
回答by ObjectiveTruth
Off-topic but if you follow 12-factor-app then having separate configurations named after environments is bad:
题外话,但如果你遵循 12-factor-app 那么以环境命名的单独配置是不好的:
Another aspect of config management is grouping. Sometimes apps batch config into named groups (often called “environments”) named after specific deploys, such as the development, test, and production environments in Rails. This method does not scale cleanly: as more deploys of the app are created, new environment names are necessary, such as staging or qa. As the project grows further, developers may add their own special environments like joes-staging, resulting in a combinatorial explosion of config which makes managing deploys of the app very brittle
Another aspect of config management is grouping. Sometimes apps batch config into named groups (often called “environments”) named after specific deploys, such as the development, test, and production environments in Rails. This method does not scale cleanly: as more deploys of the app are created, new environment names are necessary, such as staging or qa. As the project grows further, developers may add their own special environments like joes-staging, resulting in a combinatorial explosion of config which makes managing deploys of the app very brittle
source: http://12factor.net/config

