How to Deploy Spring on Platform.sh with MongoDB
To activate MongoDB and then have it accessed by the Spring application already in Platform.sh, it is necessary to modify two files.
This guide only covers the addition of a MongoDB service configuration to an existing Spring project already configured to deploy on Platform.sh. Please see the deployment guide for more detailed instructions for setting up app containers and initial projects.
.platform/services.yaml file, include MongoDB with a valid supported version:
dbmongo: type: mongodb:3.6 disk: 512
.platform.app.yaml file, use the service name
dbmongo to grant the application access to MongoDB via a relationship:
relationships: mongodatabase: "dbmongo:mongodb"
Connection credentials for services are exposed to the application container through the
PLATFORM_RELATIONSHIPS environment variable from the deploy hook onward. Since this variable is a base64 encoded JSON object of all of your project’s services, you’ll likely want a clean way to extract the information specific to the databse into it’s own environment variables that can be easily used by Spring. On Platform.sh, custom environment variables can be defined programmatically in a
.environment file using
jq to do just that:
export SPRING_DATA_MONGODB_USERNAME=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".dbmongo.username"` export SPRING_DATA_MONGODB_PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".dbmongo.password"` export SPRING_DATA_MONGODB_HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".dbmongo.host"` export SPRING_DATA_MONGODB_DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".dbmongo.path"` export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"
Commit that code and push. The application is ready and connected to a MongoDB instance.