[英]How to use Environment Variables and application.conf files in Scala?
I am unsure of how to get environment variables from my dev.env and prod.env into my application.conf (or reference them from the conf file).我不确定如何从我的 dev.env 和 prod.env 获取环境变量到我的 application.conf(或从 conf 文件中引用它们)。
I want to deploy my app to heroku and connect it to heroku's database.我想将我的应用程序部署到 heroku 并将其连接到 heroku 的数据库。 In the future, I'll want to deploy to AWS in docker container.将来,我想在 docker 容器中部署到 AWS。
Can some please explain how environment variables work in Scala and explain how to refernce the environement variables from application.conf.请解释一下环境变量在 Scala 中是如何工作的,并解释如何从 application.conf 中引用环境变量。
I already have a configLoader.我已经有一个 configLoader。
Below is a picture of my file structure and I have also copied code below:下面是我的文件结构的图片,我还复制了下面的代码:
application.conf file below: application.conf 文件如下:
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = DEBUG
}
server-config {
host = "0.0.0.0"
port = 8080
base-url = ${?BASE_URL}
}
db-config {
url = {?JDBC_DATABASE_URL}
username = "su"
password = "password"
pool-size = 10
driver="org.h2.Driver"
}
dev.env below: dev.env 下面:
ENV=dev
BASE_URL=http://localhost:8080
JDBC_DATABASE_URL=jdbc:postgresql://localhost:5400/bookswapdb
prd.env below: prd.env 下面:
ENV=${_ENVIRONMENT}
BASE_URL=https://git.heroku.com/appone2021.git
JDBC_DATABASE_URL=${URL_WITH_CREDS_REDACTED}
Assuming that you're using ConfigFactory.load
(or having Akka load the configuration for you), the environment variables will "just work", assuming that the JVM process is actually running with the environment variables set.假设您正在使用ConfigFactory.load
(或让 Akka 为您加载配置),环境变量将“正常工作”,假设 JVM 进程实际运行时设置了环境变量。
To get the value of eg, db-config.url
, you should just be able to do要获得例如db-config.url
的值,您应该能够做到
val config = ConfigFactory.load() // or if in an actor, context.system.config
val dbUrl = config.getString("db-config.url")
On Heroku, you would just use config vars .在 Heroku 上,您只需使用 config vars 。 For AWS deployment, the particulars of how you inject environment variables into the container will depend on how you're running the container.对于 AWS 部署,如何将环境变量注入容器的细节将取决于您运行容器的方式。 Note that setting the environment variables may be difficult or impossible in IntelliJ (since if IntelliJ runs the app inside its own JVM, it will only have the environment variables set when IntelliJ started up).请注意,在 IntelliJ 中设置环境变量可能很困难或不可能(因为如果 IntelliJ 在其自己的 JVM 中运行应用程序,它只会在 IntelliJ 启动时设置环境变量)。
db={
default.driver = "org.postgresql.Driver"
default.url="vendor://username:password@host:port/db"
db.url=${DATABASE_URL}
db.profile="jdbc.PostgresProfile"
}
Play will automatically convert this into a JDBC URL for you if you are using one of the built in database connection pools.如果您使用的是内置数据库连接池之一,Play 会自动将其转换为 JDBC URL。 But other database libraries and frameworks, such as Slick or Hibernate, may not support this format natively.但是其他数据库库和框架,例如 Slick 或 Hibernate,可能本身不支持这种格式。 If that's the case, you may try using the dynamic JDBC_DATABASE_URL in place of DATABASE_URL in the configuration like this:如果是这种情况,您可以尝试在配置中使用动态 JDBC_DATABASE_URL 代替 DATABASE_URL,如下所示:
db.default.url=${?JDBC_DATABASE_URL}
to get value from application.conf file:从 application.conf 文件中获取值:
import com.typesafe.config.Config
val str1=config.getString("your_string")
reference https://www.playframework.com/documentation/2.8.x/ProductionHeroku参考https://www.playframework.com/documentation/2.8.x/ProductionHeroku
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.