I have written a simple spring boot application(version springboot 2.0) which uses mysql(version 5.7).
application.properties snippet
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = testuser
spring.datasource.password = testpassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
When I run it locally, it works fine. If I want to run this spring boot application in docker then I can change
spring.datasource.url = jdbc:mysql://mysql-container:3306/test?useSSL=false
mysql-container is run using mysql:5.7 image from dockerhub.
However I want to change value of host from some placeholder properties file. so that this looks something like:
spring.datasource.url = jdbc:mysql://${MYSQL_HOST}:3306/test?useSSL=false
note: I am not sure about placeholder format. Is it ${MYSQL_HOST} or @MYSQL_HOST@ ?
you can name this placeholder file as placeholder.properties or placeholder.conf or .env or anything. The content of that file should be something like:
MYSQL_HOST=localhost
or
MYSQL_HOST=some ip address
I can create .env or .env.test or .env.prod and I can refer that env file based on where I want to run application.
UPDATE -
I have two questions:
Where should I keep placeholder.properties? Is it under /config/ or under some specific directory?
how to invoke placeholder inside application.properties ?
can someone suggest?
If you project is Maven you can use maven filter:
<build>
<filters>
<filter>src/main/filters/myfilter.properties</filter>
</filters>
</build>
This generates the /target/classes/application.properties
it has been filtered to contain the property values (with replaced placeholders)
Thanks to answers by @Raheela Aslam and @paulsm4 and some more research found the issue.
What I was trying to achieve:
How I fixed it:
I created configmap for mysql_user, mysql_password, mysql_host with respective values.
kubectl create configmap mysql-config \
--from-literal=mysql_user=testuser \
--from-literal=mysql_password=testuserpass \
--from-literal=mysql_user=$(minikube ip)
and used these inside application.properties something like below
spring.datasource.url = jdbc:mysql://${MYSQL_HOST}:3306/test?useSSL=false
spring.datasource.username = ${MYSQL_USER}
spring.datasource.password = ${MYSQL_PASSWORD}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
and then used configmap values in deployment.yaml for kubernetes. Then did start service for deployment.
SUGGESTION: If you have a relatively small #/properties, why not just have a different application.properties file for each different environment?
You'd specify the environment at runtime with -Dspring.profiles.active=myenv
.
PS:
To answer your specific question: the syntax is ${MYSQL_HOST}