源码下载 下载 redis-cluster分支
和单机版不同的地方
1 .新增配置文件redisClust.properties
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=192.168.1.62:8001,192.168.1.62:8002,192.168.1.62:8003,192.168.1.62:8004,192.168.1.62:8005,192.168.1.62:8006
2.新增 RedisClusterConfig类替换调RedisConfig类
package com.roger.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.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisClusterConfig extends CachingConfigurerSupport {
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.maxWaitMillis}")
private int maxWaitMillis;
@Value("${redis.blockWhenExhausted}")
private boolean blockWhenExhausted;
@Value("${redis.testOnBorrow}")
private boolean testOnBorrow;
@Autowired
private JedisPoolConfig jedisPoolConfig;
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
Properties prop = new Properties();
InputStream in = this.getClass().getResourceAsStream("/redisCluster.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
PropertiesPropertySource propertySource = new PropertiesPropertySource("clusterNodes", prop);
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(propertySource);
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder
jedisPoolConfigBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
jedisPoolConfigBuilder.poolConfig(jedisPoolConfig);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfigBuilder.build());
return jedisConnectionFactory;
}
@Bean
public RedisTemplate redisTemplate() {
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);
RedisSerializer stringRedisSerializer = new StringRedisSerializer();
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
类比单机和Cluster不同地方
@Bean Properties prop = new Properties(); JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolConfigBuilder.poolConfig(jedisPoolConfig); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfigBuilder.build()); |