SpringBoot integrates Redis Sentinel mode

Master slave build click Sentinel build click

configuration

yaml

# Redis master/slave sentry configuration
spring:
  redis:
    database: 0
    host: 127.0. 01.
    port: 6379
    password:
    pool:
      max-active: 8
      max-wait: - 1 Maximum connection pool blocking wait time (negative value indicates no limit)
      max-idle: 8
      min-idle: 0
    timeout: 0
    # sentry name of primary node
    sentinel:
      master: mymaster
      # comma-Separated list of host: Port Pairs Sentinel configuration list
      nodes: 127.0. 01.: 26380127.00 0.1:26381

Copy the code

Rely on


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7. RELEASE</version>
        </dependency>
Copy the code

The connection pool

@Configuration
@EnableAutoConfiguration
public class RedisCacheConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout = 0;

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.password}")
    private String password;

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

    @Value("${spring.redis.sentinel.master}") private String master; / * * * redis sentry configuration * / @ Bean public RedisSentinelConfigurationredisSentinelConfiguration(){
        RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
        String[] host = redisNodes.split(",");
        for(String redisHost : host){
            String[] item = redisHost.split(":");
            String ip = item[0];
            String port = item[1];
            configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
        }
        configuration.setMaster(master);
        returnconfiguration; } /** * Connect to redis factory class */ @bean public JedisConnectionFactoryjedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration());
        factory.setHostName(host);
        factory.setPort(port);
        factory.setTimeout(timeout);
        factory.setPassword(password);
        factory.setDatabase(database);
        returnfactory; } /** * RedisTemplate serializer */ @bean public RedisTemplate<Object, Object>redisTemplate{// The stringSerializer RedisTemplate<Object, Object> template = new RedisTemplate<>(); / / set the open transaction template. SetEnableTransactionSupport (true);
        //set key serializer
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        template.setConnectionFactory(jedisConnectionFactory());
        template.afterPropertiesSet();
        returntemplate; } /** * Set RedisCacheManager * to use cache annotations to manage the Redis cache ** @return
     */
    @Override
    @Bean
    public RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
        returnredisCacheManager; } /** * custom generated redis-key ** @return
     */
    @Override
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName()).append(".");
                sb.append(method.getName()).append(".");
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                System.out.println("keyGenerator=" + sb.toString());
                returnsb.toString(); }}; }}Copy the code