Ganesh Prajapati
Ganesh Prajapati

Reputation: 21

Enable single server and cluster mode both for ElastiCache

I am trying to conifgure redisson.yaml something like below

redis:
  mode: ${REDIS_MODE:single}  # Default to single mode if REDIS_MODE is not set

# Configuration for single Redis server
singleServerConfig:
  enabled: ${REDIS_MODE:single} == 'single'
  address: ${REDIS_URL:-redis://localhost:6379}
  username: ${REDIS_USERNAME:-null}
  password: ${REDIS_PASSWORD:-null}
  database: ${REDIS_DATABASE:-0}

# Configuration for Redis cluster
clusterServersConfig:
  enabled: ${REDIS_MODE:single} == 'cluster'
  nodeAddresses:
    - ${REDIS_URL:-redis://localhost:6379}
    # Add more nodes if available
  password: ${REDIS_PASSWORD:-null}

But it is giving error Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('#' (code 35)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (String)"# Common Redis configuration (if any) redis: mode: ${REDIS_MODE:single}

SO how we can enable conditional check in redisson.yaml file to load the redis config based on environment variable?

Upvotes: 0

Views: 32

Answers (2)

Ganesh Prajapati
Ganesh Prajapati

Reputation: 21

We had to add two separte redis-config yaml files, and handle it based on env variable programatically in RedisConfiguration

redisson-single.yaml redisson-cluster.yaml

then load it based on env variable

@Value("classpath:redis-config/redisson-${REDIS_MODE}.yaml"

Upvotes: 0

Med Agou
Med Agou

Reputation: 1281

Yaml doesn't support conditionals so I think the trick is to use two env varaiables as boolean switches let's say : REDIS_SINGLE_ENABLED and REDIS_CLUSTER_ENABLED

clusterServersConfig:
  enabled: ${REDIS_CLUSTER_ENABLED}

Also remove the comment in front of mode: ${REDIS_MODE:single} I think you error is from the '#'.

   Unexpected character ('#' (code 35)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (String)"# Common Redis configuration (if any) **redis: mode: ${REDIS_MODE:single}

Upvotes: 1

Related Questions