springboot2.0 配置redis哨兵

litblank發表於2019-02-26

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<String,Object>)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
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions> <exclusion> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </exclusion> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>    


複製程式碼

相關文章