redis之哨兵 springboot配置

转载自https://blog.csdn.net/m0_37367413/article/details/82018125

 

 

springboot整合redis哨兵方式配置

1.springboot工程引入redis

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-data-redis</artifactId>
  4.  
    </dependency>

2.application.properties填写配置信息

  1.  
    ####redis的配置信息###
  2.  
    spring.redis.sentinel.master=mymaster
  3.  
    spring.redis.sentinel.nodes=host1:port1,host2:port2,...
  4.  
    spring.redis.password=password......
  5.  
    #采用哪个数据库
  6.  
    spring.redis.database=0
  7.  
    # 连接池最大连接数,默认8个,(使用负值表示没有限制)
  8.  
    spring.redis.pool.max-active=8
  9.  
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
  10.  
    spring.redis.pool.max-wait=-1
  11.  
    # 连接池中的最大空闲连接
  12.  
    spring.redis.pool.max-idle=8
  13.  
    # 连接池中的最小空闲连接
  14.  
    spring.redis.pool.min-idle=0
  15.  
    # 连接超时时间(毫秒)
  16.  
    spring.redis.timeout=0

3.注入使用

  1.  
    @Autowired
  2.  
    private RedisTemplate redisTemplate;

4.自定义redis序列化

  1.  
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
  2.  
    import com.fasterxml.jackson.annotation.PropertyAccessor;
  3.  
    import com.fasterxml.jackson.databind.ObjectMapper;
  4.  
    import org.springframework.context.annotation.Bean;
  5.  
    import org.springframework.context.annotation.Configuration;
  6.  
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  7.  
    import org.springframework.data.redis.core.RedisTemplate;
  8.  
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9.  
     
  10.  
    /**
  11.  
    * 〈redis模板的相应配置自定义序列化〉
  12.  
    * @create 2018/8/24
  13.  
    * @since 1.0.0
  14.  
    */
  15.  
    @Configuration
  16.  
    public class RedisConfig {
  17.  
     
  18.  
    @Bean
  19.  
    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory redisConnectionFactory){
  20.  
     
  21.  
    RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
  22.  
    redisTemplate.setConnectionFactory(redisConnectionFactory);
  23.  
     
  24.  
    //自定义序列化方式
  25.  
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  26.  
    ObjectMapper objectMapper = new ObjectMapper();
  27.  
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  28.  
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  29.  
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  30.  
     
  31.  
    redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
  32.  
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  33.  
    redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
  34.  
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  35.  
    redisTemplate.afterPropertiesSet();
  36.  
     
  37.  
     
  38.  
    return redisTemplate;
  39.  
    }
  40.  
     
  41.  
    }

5.采用自己的模板

  1.  
    @Autowired
  2.  
    private RedisTemplate<String,Object> redisTemplate;

搞定。。。。。。

posted @ 2019-02-20 17:34  heroinss  阅读(2727)  评论(0编辑  收藏  举报