springboot2.0 配置redis哨兵

1、遇到的报错

  • ClassNotFoundException: redis.clients.jedis.GeoCoordinate
  • java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig
  • Factory method 'getConnectionFactory' threw exception; nested exception is j
  • ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

基本都是jar版本问题和配置重名问题

正确配置项

package com.exch.platform.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: chenyadong
 * @Date: 2019/2/25 14:25
 * @Version 1.0
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis.sentinel")
public class RedisConfig {

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

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

    @Value("${redis.sentinel.port}")
    private Integer  sentinelPort;

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

    /**
     * 哨兵配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.sentinel")
    public RedisSentinelConfiguration getRedisSentinelConfig(){
        RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
        sentinelConfiguration.setMaster(master);
        sentinelConfiguration.sentinel(sentinelHost,sentinelPort);
        return sentinelConfiguration;
    }

    //连接redis的工厂类
    @Autowired
    private JedisPoolConfig jedisPoolConfig;
    @Autowired
    private JedisConnectionFactory jesidConnFact;

    @Bean
    @ConfigurationProperties(prefix="redis.pool")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix="redis")
    public JedisConnectionFactory getConnectionFactory(){
        JedisConnectionFactory jedisFactory = new JedisConnectionFactory(getRedisSentinelConfig(),getRedisConfig());
        return jedisFactory;
    }


    @Bean
    public RedisTemplate getRedisTemplate(){
        RedisTemplate redisTemplate = new StringRedisTemplate(getConnectionFactory());
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        // 值采用json序列化
        redisTemplate.setValueSerializer(jacksonSeial);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jacksonSeial);
        redisTemplate.afterPropertiesSet();

        //((RedisTemplate)redisTemplate).opsForValue().set("cyd0226","阿斯蒂芬请问asd123");
        return redisTemplate;
    }

}

复制代码
.yml

redis:
  pool:
    max-active: 200
    max-wait: -1
    max-idle: 8
    min-idle: 0
  timeout: 0
  database: 1
  password: 123456789
  sentinel:
    master: testre
    nodes: 10.18.9.21
    port: 26380
复制代码
pom

            org.springframework.boot
            spring-boot-starter-data-redis
              redis.clients jedis   io.lettuce lettuce-core  
        

        
            org.apache.commons
            commons-pool2
            2.5.0
        

        
            redis.clients
            jedis
            2.9.0
            


复制代码

你可能感兴趣的:(springboot2.0 配置redis哨兵)