Platform.sh User Documentation

How to Deploy Spring on Platform.sh with Redis

Try for 30 days
Flexible, version-controlled infrastructure provisioning and development-to-production workflows
Activate your trial

To activate Redis and then have it accessed by the Spring application already in Platform.sh, it is necessary to modify two files.

1. Add the Redis service Anchor to this heading

In your service configuration, include persistent Redis with a valid supported version:

.platform/services.yaml
data:
    type: redis-persistent:7.2
    disk: 256

2. Add the Redis relationship Anchor to this heading

In your app configuration, use the service name searchelastic to grant the application access to Elasticsearch via a relationship:

.platform.app.yaml
relationships:
    redisdata: "data:redis"

3. Export connection credentials to the environment Anchor to this heading

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 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_REDIS_HOST=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".redisdata[0].host")
export SPRING_REDIS_PORT=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".redisdata[0].port")
export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"

4. Connect to Redis Anchor to this heading

Commit that code and push. The Redis instance is ready to be connected from within the Spring application.

Use Spring Data for Redis Anchor to this heading

You can use Spring Data Redis to use Redis with your app. First, determine the MongoDB client using the Java configuration reader library.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
public class RedisConfig {


    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        Config config = new Config();
        RedisSpring redis = config.getCredential("redis", RedisSpring::new);
        return redis.get();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }

}

Is this page helpful?