Redis Sentinel - Spring Data Redis



`Redis Sentinel` is a high-availability solution for Redis, a popular in-memory key-value store. It is used to ensure that Redis instances are highly available by monitoring the Redis master and its replicas and promoting a new master if the current master goes down. `RedisSentinelConfiguration` is a part of the Spring Data Redis project, which provides integration with Redis in Spring applications.

Here's a sample Java code snippet that demonstrates how to configure `RedisSentinelConfiguration` and use it in a Spring application. You can also define `RedisSentinelConfiguration` using a `PropertySource` for externalized configuration. In this example, we'll show both approaches.

1. Using Java Configuration


   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.data.redis.connection.RedisConnectionFactory;
   import org.springframework.data.redis.connection.RedisSentinelConfiguration;
   import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
   import redis.clients.jedis.JedisPoolConfig;

   @Configuration
   public class RedisConfig {

       @Bean
       public RedisSentinelConfiguration redisSentinelConfiguration() {
           RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                   .master("mymaster") // Name of the Redis master server
                   .sentinel("localhost", 26379) // Sentinel host and port
                   .sentinel("localhost", 26380);

           return sentinelConfig;
       }

       @Bean
       public RedisConnectionFactory redisConnectionFactory() {
           JedisPoolConfig poolConfig = new JedisPoolConfig();
           poolConfig.setMaxTotal(10); // Max number of connections

           return new JedisConnectionFactory(redisSentinelConfiguration(), poolConfig);
       }
   }

   In this code:
   - We create a `RedisSentinelConfiguration` bean, specifying the master name and sentinel hosts/ports.
   - We create a `RedisConnectionFactory` bean using Jedis as the Redis client and configure it with the sentinel configuration.



2. Using `PropertySource` for Externalized Configuration

   You can also externalize the configuration using properties files. Here's how you can configure `RedisSentinelConfiguration` in `application.properties`:

   spring.redis.sentinel.master=mymaster
   spring.redis.sentinel.nodes=localhost:26379,localhost:26380

   Then, modify the Java configuration as follows:

   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.data.redis.connection.RedisConnectionFactory;
   import org.springframework.data.redis.connection.RedisSentinelConfiguration;
   import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
   import redis.clients.jedis.JedisPoolConfig;

   @Configuration
   public class RedisConfig {

       @Value("${spring.redis.sentinel.master}")
       private String sentinelMaster;

       @Value("${spring.redis.sentinel.nodes}")
       private String sentinelNodes;

       @Bean
       public RedisSentinelConfiguration redisSentinelConfiguration() {
           RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
                   .master(sentinelMaster)
                   .sentinel(sentinelNodes);

           return sentinelConfig;
       }

       @Bean
       public RedisConnectionFactory redisConnectionFactory() {
           JedisPoolConfig poolConfig = new JedisPoolConfig();
           poolConfig.setMaxTotal(10);

           return new JedisConnectionFactory(redisSentinelConfiguration(), poolConfig);
       }
   }
   In this code:

   - We use `@Value` annotations to inject the properties from `application.properties` into our Java configuration.
   - The properties `spring.redis.sentinel.master` and `spring.redis.sentinel.nodes` correspond to the master name and sentinel nodes defined in the properties file.

Remember to include the necessary Spring Data Redis and Jedis dependencies in your project's build file (`pom.xml` or `build.gradle`).

Documentation:
- [Spring Data Redis Documentation](https://docs.spring.io/spring-data/redis/docs/current/reference/html/#reference)
- [Spring Framework Configuration Properties](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config)


Post a Comment

Previous Post Next Post