springboot + redis集群和单机切换(密码)自定义配置文件

项目中因为不是使用springRedis的配置,而是采用自定义的配置redis.pool,而且需要进行单机和集群的切换,中间出现了一些jar包冲突的问题,导致部分功能不能使用,还是记录一下,这个找了好久

1、配置文件如下,可能和现行的配置不同,使用model进行区分,当然代码有判断

redis:
  pool:
    maxTotal: 120 # 最大连接数
    maxIdle: 30 # 最大空闲实例的个数
    minIdle: 3 #最小空闲实例个数
    numTestsPerEvictionRun: 1024 # 每个逐出器运行期间要检查的最大对象数
    timeBetweenEvictionRunsMillis: 30000 # 释放扫描的扫描间隔
    minEvictableIdleTimeMillis: 1800000 # 连接的最小空闲时间
    softMinEvictableIdleTimeMillis: 10000 # 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
    maxWaitMillis: 1500 # 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
    testOnBorrow: true # 在获得链接的时候检查有效性,默认false
    testWhileIdle: true # 在空闲时检查有效性,默认false
    blockWhenExhausted: false # 连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
    timeOut: 10000 # jedis超时时间
  maxRedirects: 5 # RedisClusterConfiguration配置
  nodeCluters: 192.168.98.105:7000,192.168.98.105:7001,192.168.98.105:7002,192.168.98.105:7003,192.168.98.105:7004,192.168.98.105:7005
  hostName: localhost
  password: 43b6f9b82c71c13063a523c0dcbc6db8fc0ac8a0
  port: 6379
  model: 3 #redis的模式1 表示单机,2 表示sentinel, 3表示cluster

2、pom文件

原来使用的pom是1.4.7,但是这个版本好像有点低


	org.springframework.boot
	spring-boot-starter-redis
	1.4.7.RELEASE

这个版本在jedisConnectionFactory设置密码处好像不太兼容

因此需要处理jar的版本冲突问题(敲黑板啦`````这个才是重点),需要排除依赖


	org.springframework.boot
	spring-boot-starter-data-redis
	
		
			redis.clients
			jedis
		
	    
		    org.springframework.data
			spring-data-redis
		
	


	redis.clients
	jedis
    2.9.0


	org.springframework.data
	spring-data-redis
	1.8.10.RELEASE

3、一下为具体的config,因为有个工具类需要注入,所以会有CacheUtil,然后里面进行了redis的序列化,据说默认是jdk的序列化方式


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yss.ms.constant.RedisConstant;
import com.yss.ms.util.CacheUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;


@Configuration
public class RedisClusterConfig {

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

	@Value("${redis.hostName}")
	private String hostName;
	@Value("${redis.port}")
	private int port;
	@Value("${redis.password}")
	private String password;
	@Value("${redis.model}")
	private int model;


	@Value("${redis.maxRedirects}")
	private int maxRedirects;
	@Value("${redis.pool.blockWhenExhausted}")
	private boolean blockWhenExhausted;
	@Value("${redis.pool.maxTotal}")
	private int maxTotal;
	@Value("${redis.pool.maxIdle}")
	private int maxIdle;
	@Value("${redis.pool.minIdle}")
	private int minIdle;
	@Value("${redis.pool.numTestsPerEvictionRun}")
	private int numTestsPerEvictionRun;
	@Value("${redis.pool.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	@Value("${redis.pool.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	@Value("${redis.pool.softMinEvictableIdleTimeMillis}")
	private int softMinEvictableIdleTimeMillis;
	@Value("${redis.pool.maxWaitMillis}")
	private int maxWaitMillis;
	@Value("${redis.pool.timeOut}")
	private int timeOut;

	@Bean(name = "jedisPoolConfig")
    JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig pool = new JedisPoolConfig();
		pool.setMaxIdle(maxIdle);
		pool.setMinIdle(minIdle);
		pool.setBlockWhenExhausted(blockWhenExhausted);
		pool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
		pool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		pool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		pool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);

		pool.setMaxTotal(maxTotal);
		pool.setMaxWaitMillis(maxWaitMillis);
		return pool;
	}

	@Bean(name = "redisClusterConfiguration")
	public RedisClusterConfiguration getRedisClusterConfiguration() {
		RedisClusterConfiguration configuration = new RedisClusterConfiguration();
		Set nodes = new HashSet();

		String[] nodec=nodeCluters.split(",");
		for(int i=0;i nodes = new HashSet();
		String[] nodec=nodeCluters.split(",");
		for(int i=0;i getRedisTemplate(
			@Qualifier("jedisConnectionFactory") JedisConnectionFactory factory) {
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		// 使用Jackson2JsonRedisSerialize 替换默认序列化
		@SuppressWarnings({ "rawtypes", "unchecked" })
        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);

		// 设置value的序列化规则和 key的序列化规则
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

		redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
		redisTemplate.setEnableDefaultSerializer(true);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

	@Bean
	public CacheUtils cacheUtils(RedisTemplate redisTemplate){
		return new CacheUtils(redisTemplate);
	}


	/**
	 * 校验password是否为空,不为空设置密码
	 * @param jedisConnectionFactory
	 */
	private void checkPasswordIfNull(JedisConnectionFactory jedisConnectionFactory) {
		if (!StringUtils.isBlank(password)) {
			jedisConnectionFactory.setPassword(password);
		}
	}
}

 

 

 

你可能感兴趣的:(java,redis)