This app does not contain any datasource beans or properties required to connect to an external MySQL database. As a result, CF will auto-reconfigure the Spring Boot application by injecting a datasource bean created by the java_buildpack, on cf push.
Some Spring Boot apps in CF bind to a database but do not have any datasource configurations in application.properties or in code. CF is able to detect that the datasource is missing and will inject a datasource automatically based on the VCAP_SERVICES credentials.
Similar to the application-properties demo, there are a couple of easy ways to migrate this app. The first way is to simply copy the VCAP_SERVICES var from CF, save it to an OCP secret, load it as an environment variable, and reference it in application.properties.
However, since an application like this is already not using VCAP_SERVICES, we may as well pick and choose only the fields we need from VCAP_SERVICES, save those fields in an OCP secret, and reference them in application.properties like this:
spring.datasource.url=jdbc:${uri}
This is the solution demonstrated in this demo.
See the CF Setup doc.
First, in order to deploy the CF app into OpenShift, you need to create a container image. This can be done easily by using OpenShift's native build capabilities.
See the Building the Application Image doc.
Run the following commands to deploy the CF application to OCP:
- Get the CF application GUID
export GUID=$(cf app sample-app --guid -q)
- Save the VCAP_SERVICES json to a variable. The tool jq comes in handy here.
export VCAP_SERVICES_JSON=$(cf curl /v2/apps/$GUID/env -q | jq -r .system_env_json.VCAP_SERVICES)
- Save the datasource uri to a variable:
export URI=$(echo $VCAP_SERVICES_JSON | jq -r '."compose-for-mysql"[0].credentials.uri')
- Save the URI as an OCP secret
oc create secret generic sample-app-uri --from-literal=URI=${URI} - Deploy the app
Notice in .openshift/deploy.yml the ConfigMap that is being created, which references the URI environment variable (added by envFrom in the deployment).
oc apply -f .openshift/deploy.yml
- Ensure the app is connected to the database
This should return a JSON of customer names.
curl $(oc get route sample-app -o jsonpath='{.spec.host}')/findall
Here are instructions for cleanup up your CF and OCP environments after running this demo:
- Delete the sample-app from OCP
oc delete -f .openshift/deploy.yml
- Delete the sample-app from CF
cf delete java-test
- The application-properties demo uses the same database provisioned from this demo. If you want to follow that demo next, you can save some time by leaving the database running. Otherwise, you can delete the database with the following command:
cf delete-service testdb