How to Deploy Micronaut on Platform.sh with Redis
Contents:
To activate Redis and then have it accessed by the Micronaut application already in Platform.sh, it is necessary to modify two files.
Note:
This guide only covers the addition of a service configuration to an existing Micronaut 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.
1. Add the Redis service
In your .platform/services.yaml
file, include Persistent Redis with a valid supported version:
data:
type: redis-persistent:6.0
disk: 256
2. Add the Redis relationship
In your .platform.app.yaml
file, use the service name redisdata
to grant the application access to Elasticsearch via a relationship:
relationships:
redisdata: "data:redis"
3. Export connection credentials to the environment
Connection credentials for Redis, like any service, 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 Elasticsearch into it’s own environment variables that can be easily used by Micronaut. On Platform.sh, custom environment variables can be defined programmatically in a .environment
file using jq
to do just that:
export REDIS_HOST=$(echo $PLATFORM_RELATIONSHIPS|base64 --decode|jq -r ".redisdata[0].host")
export REDIS_PORT=$(echo $PLATFORM_RELATIONSHIPS|base64 --decode|jq -r ".redisdata[0].port")
export REDIS_URI=redis://${REDIS_HOST}:${REDIS_PORT}
export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"
Tip:
Environment variable names are following the conversion rules of the Micronaut Documentation.
4. Connect to Redis
Commit that code and push. The Redis instance is ready to be connected from within the Micronaut application.