SpringBoot実戦の16統合redis哨兵


前言
Springboot統合redisに興味のある方は、このブログSpringBoot実戦の13統合redisを参考にしてください
spring統合redisに興味のある参考ここspring実戦3 spring統合単例redis
Spring統合哨兵参考ここspring実戦4 spring統合redis哨兵
本編ではspringboot統合哨兵モードについて説明します
スタート
建設プロジェクト
新しいspringboot-redis-sentinelプロジェクト
maven依存の追加
 <dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
dependency>
<dependency>
    <groupId>com.hsy.javagroupId>
    <artifactId>java-utilartifactId>
dependency>

プロファイル
新しいredis.properties redisプロファイル
#Matser ip  
redis.host=172.16.191.102
#   
redis.port=6379
#     
redis.password=123456
#      
redis.database=0
#                2000
redis.timeout=10000  

#     
redis.pool.maxIdle=300  
#            。  0     ,   jedis 2.4   redis.maxTotal
#redis.pool.maxActive=600
#    pool      jedis  ,       redis.maxActive,   jedis 2.4      
redis.pool.maxTotal=1000  
#          。            。  -1     。
redis.pool.maxWaitMillis=1000  
#            1800000  (30  )
redis.pool.minEvictableIdleTimeMillis=300000  
#           ,  3
redis.pool.numTestsPerEvictionRun=1024  
#         (  )      ,        ,   -1
redis.pool.timeBetweenEvictionRunsMillis=30000  
#               ,      ,                
redis.pool.testOnBorrow=true  
#         ,   false
redis.pool.testWhileIdle=true  

#redis    
# spring.redis.spool.cluster.nodes=192.168.177.128:7001,192.168.177.128:7002,192.168.177.128:7003,192.168.177.128:7004,192.168.177.128:7005,192.168.177.128:7006
# spring.redis.pool.cluster.max-redirects=3

#    
redis.sentinel.master.name=mymaster
redis.sentinel.host1=172.16.191.102
redis.sentinel.port1=26379
redis.sentinel.host2=172.16.191.102
redis.sentinel.port2=26380
redis.sentinel.host3=172.16.191.102
redis.sentinel.port3=26381

新しいアプリケーションyml
spring:
  redis:
    sentinel:
      master: mymaster
      nodes: 172.16.191.102:26379,172.16.191.102:26380,172.16.191.102:26381
server:
  port: 10003

redis構成およびシーケンス化方式
@Configuration
@PropertySource("classpath:config/redis.properties")
public class RedisConfig2 {
    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.database}")
    private Integer database;
    @Value("${redis.timeout}")
    private Integer timeout;

    @Value("${redis.pool.maxIdle}")
    private Integer maxIdle;

    @Value("${redis.pool.maxTotal}")
    private Integer maxTotal;

    @Value("${redis.pool.maxWaitMillis}")
    private Integer maxWaitMillis;

    @Value("${redis.pool.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;

    @Value("${redis.pool.numTestsPerEvictionRun}")
    private Integer numTestsPerEvictionRun;

    @Value("${redis.pool.timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis;

    @Value("${redis.pool.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${redis.pool.testWhileIdle}")
    private boolean testWhileIdle;

    /**
     * JedisPoolConfig    
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //      
        jedisPoolConfig.setMaxIdle(maxIdle);
        //             
        jedisPoolConfig.setMaxTotal(maxTotal);
        //           
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        //               1800000  (30  )
        jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        //                         : 1/abs(n),   3
        jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        //          (  )      ,        ,   -1
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        //                ,      ,                
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);
        //          ,   false
        jedisPoolConfig.setTestWhileIdle(testWhileIdle);
        return jedisPoolConfig;
    }
    /**
     *      
     * @Title: JedisConnectionFactory
     * @param @param jedisPoolConfig
     * @param @return
     * @return JedisConnectionFactory
     * @autor lpl
     * @date 2018 2 24 
     * @throws
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
        jedisConnectionFactory.setUsePool(true);
        //   
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        //IP  
        jedisConnectionFactory.setHostName(host);
        //   
        jedisConnectionFactory.setPort(port);
        //  Redis     
        jedisConnectionFactory.setPassword(password);
        //         
        jedisConnectionFactory.setDatabase(database);
        //            
        jedisConnectionFactory.setTimeout(timeout);
        return jedisConnectionFactory;
    }

    /**
     *     RedisTemplate   
     *
     * @return
     */
    @Bean
    public RedisTemplate functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
        return redisTemplate;
    }
    /**
     *        redis       ,     
     *
     * @param redisTemplate
     * @param factory
     */
    private void initDomainRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) {
        //     Serializer,           String,   User    ,       User can't cast to String!
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        // GenericJackson2JsonRedisSerializer     
        //redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // Jackson2JsonRedisSerializer     
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //set value serializer
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        //     
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
    }
}

添削して調べる.
crudファイルを作成します.redisをカプセル化したので、ここでは簡単な継承だけでいいです.

@Repository(value = "redisRepository")
public class RedisRepository extends AbstractSpringRedisCacheEnhance {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public StringRedisTemplate getStringRedisTemplate() {
        return null;
    }

    @Override
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
}

これでspringboot統合redis哨兵モードが完了しました.
まとめ
Springboot集積redis哨兵、私ももっと全面的な資料を見つけていません.上記の統合方式も私が模索しました.
いずれにしても効果はすでにあり、その理論については後続の研究がこのブログを更新する必要がある.
ソースコード
springboot-redis-sentinel