【发布时间】:2019-01-12 20:12:28
【问题描述】:
我目前正在构建一个 Spring Boot 应用程序,该应用程序需要与三个不同的数据库环境一起工作并以与环境无关的方式运行。
“dev”环境将使用本地 sqlite 数据库。
“uat”环境将使用 postgres 数据库。
“实时”环境将使用 sql 数据库。
在加载时,我的应用程序会检查环境参数是否存在:
如果没有设置或环境参数为 dev,那么它将创建一个本地 sqlite 数据库并在会话期间建立与它的连接。
如果设置为 uat,则将建立与 heroku postgres 数据库的连接。
如果设置为 live,则将建立到 mysql 数据库的连接。
现在我正在努力在 Java 上将其概念化。
这是我到目前为止编写的代码,我在其中获取环境参数。除此之外我不确定。
@SpringBootApplication
public class Launcher implements CommandLineRunner {
public static void main(String args[]) {
SpringApplication.run(Launcher.class, args);
}
@Override
public void run(String... args) throws Exception {
String currentEnvironment = System.getenv("CURRENT_ENV");
// if current env is null or dev, set up sqlite database (if it doesnt already exist and use this for the remainder of the session
// if uat connect to heroku postgres db
// if live then connect to mysql db
}
}
【问题讨论】:
标签: java mysql postgresql sqlite spring-boot