java - How to use OpenShift environment variables to configure a MySQL database -
so generated application jhipster.
locally, modify database info in application-dev.yml/application-prod.yml , works fine.
now want deploy application on openshift. tried putting environment variable names in application-xxx.yml returned null. modified databaseconfiguration.java
skip reading info .yml file , read them directly environment variables.
if(system.getenv("openshift_mysql_db_host") != null && system.getenv("openshift_mysql_db_port") != null){ config.adddatasourceproperty("url", "jdbc:mysql://"+system.getenv("openshift_mysql_db_host")+":"+system.getenv("openshift_mysql_db_port")+"/zetravelcloud?useunicode=true&characterencoding=utf8"); } if (system.getenv("openshift_mysql_db_username") != null) { config.adddatasourceproperty("user", system.getenv("openshift_mysql_db_username")); } else { config.adddatasourceproperty("user", ""); // hikaricp doesn't allow null user } system.out.println(system.getenv("openshift_mysql_db_password")); if (system.getenv("openshift_mysql_db_password") != null) { config.adddatasourceproperty("password", system.getenv("openshift_mysql_db_password")); } else { config.adddatasourceproperty("password", ""); // hikaricp doesn't allow null password }
instead of
config.adddatasourceproperty("url", datasourceproperties.geturl()); if (datasourceproperties.getusername() != null) { config.adddatasourceproperty("user", datasourceproperties.getusername()); } else { config.adddatasourceproperty("user", ""); // hikaricp doesn't allow null user } if (datasourceproperties.getpassword() != null) { config.adddatasourceproperty("password", datasourceproperties.getpassword()); } else { config.adddatasourceproperty("password", ""); // hikaricp doesn't allow null password }
now question is: how correctly use openshift environment variables in configuration class connect database?
bonus question: better way solve this, don't feel it's right put variable names in databaseconfiguration
class?
using ${openshift_mysql_db_host} syntax in application.yml way , should work.
Comments
Post a Comment