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?
2
Answers
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"
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
Also remove the comment in front of
mode: ${REDIS_MODE:single}
I think you error is from the ‘#’.