如何为 Spark Java 提供动态数据库配置凭据?

How to provide dynamic database configuration credentials for Spark Java?

我正在使用 spark java 构建一个简单的 REST API。该项目使用 Postgres 数据库进行存储,gradle 作为构建系统。 fat Jar 部署在 AWS.

我有三个环境 localstagingproduction。对于每个 fat jar 创建,我想提供如下用于本地构建的特定配置:

    dbHost = "localhost"
    dbUsername = "myusername"
    dbPassword = "mypassword"
    database = "mydb"
    dbPort = "5432"

如何根据 fatJar 构建变体动态提供这些值?

我按照这个示例 github 项目 Kotlin-dev-proxy.

使用了 .properties 文件

服务器读取名为 app_environment 的环境变量并使用适当的资源文件。

示例代码:

DbConfig.kt

data class DbConfig(
        val dbHost: String,
        val dbUsername: String,
        val dbPassword: String,
        val dbName: String,
        val dbPort: Int)

ServerSettings.kt

class ServerSettings(settings: String) {
    val resources = Properties()

    init {
        val fileUrl: URL = resources.javaClass.getResource("/$settings.properties")
                ?: throw FileNotFoundException("$settings.properties file not found")
        fileUrl.openStream().use { resources.load(it) }
    }

    fun printSettings() = resources.stringPropertyNames().forEach {
        println("Property: $it has value: '${resources[it]}'")
    }

    fun getString(key: String): String = resources[key]!! as String

    fun getInt(key: String): Int = (resources[key]!! as String).toInt()
}

DbConfig.kt

fun provideDbConfig(): DbConfig {
        val settings = ServerSettings(System.getenv("app_environment"))
        settings.printSettings()

        return DbConfig(
                dbHost = settings.getString("dbHost"),
                dbUsername = settings.getString("dbUsername"),
                dbPassword = settings.getString("dbPassword"),
                dbName = settings.getString("dbName"),
                dbPort = settings.getInt("dbPort")
        )
    }

resources/develop.properties

# Local Db values
dbHost=localhost
dbUsername=postgresusername
dbPassword=postgrespassword
dbName=mydbname
dbPort=5432

在 Amazon EBS 中部署时,转到软件配置并为环境添加适当的环境变量(staging/production)。