使用SpringBoot 1.5.8.RELEASE版本整合Redis 4.0.11,实现Redis Pool操作Redis集群
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-data-redis
io.lettuce
lettuce-core
redis.clients
jedis
SpringBoot默认使用lettcuce作为redis客户端,在这里我们使用jedis替换lettcuce所以需要排除lettcuce依赖。
Spring Data Redis 在1.7版本开始支持Redis的集群,如果非1.7版本,需要自己实现Redis集群的支持。使内置的Redis集群支持,需要Redis服务版本在3.0+
首先建立一个配置文件redis.properties来存放redis集群配置参数:
spring.redis.cluster.nodes=192.168.2.222:7000,192.168.2.222:7001,192.168.2.222:7002,192.168.2.222:7003,192.168.2.222:7004
spring.redis.cluster.max-redirects=2
spring.redis.cluster.timeout=5000
spring.redis.cluster.max-attempts=3
用于获取配置文件中的属性值,在Redis实力工厂提供配置参数:
package com.example.userservice.config.redis;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.List;
/**
* redis 集群配置属性
*/
@Component
@Validated
@Data
@ConfigurationProperties(value = "spring.redis.cluster")
@PropertySource(value = "classpath:jedis.properties")
public class RedisClusterProperty {
/**
* 集群节点的主机名
* spring.redis.cluster.nodes[0] = 127.0.0.1:7379
* spring.redis.cluster.nodes[1] = 127.0.0.1:7380
*/
private List nodes;
/**
* 获取连接的超时时间
*/
private Integer timeout;
/**
* 最大连接尝试次数
*/
private Integer maxAttempts;
}
配置Redis实例的工厂,将RedisClusterProperty中的参数注入进去:
package com.example.userservice.config.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;
/**
* redis集群必须redis 3.0以上版本的支持
* redis集群
*/
@Configuration
@Component
public class JedisClusterConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JedisClusterConfig.class);
@Autowired
private RedisClusterProperty redisClusterProperty;
/**
* Spring Data Redis 1.7 支持redis集群
* jedis集群配置
*
* @return
*/
@Bean
@Primary
public RedisConnectionFactory connectionFactory() {
RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(
new RedisClusterConfiguration(redisClusterProperty.getNodes()));
return redisConnectionFactory;
}
// @Bean
// public JedisCluster getJedisCluster() {
// List servers = redisClusterProperty.getNodes();
// Set nodes = new HashSet<>();
// Integer port = null;
// try {
// for (String server : servers) {
// String[] ipPortPair = server.split(":");
// port = Integer.parseInt(ipPortPair[1]);
// nodes.add(new HostAndPort(ipPortPair[0], port));
// }
// } catch (NumberFormatException e) {
// LOGGER.error("paser port :{} error", port);
// }
// JedisCluster jedisCluster = new JedisCluster(nodes, redisClusterProperty.getTimeout(), redisClusterProperty.getMaxAttempts());
// return jedisCluster;
// }
}
SpringData提供的RedisTemplate只支持基本类型的操作,不能处理对象的存储。为了方便对象的操作,我们需要自定义一个RedisTemplate来操作对象。
处理对象的序列化
package com.example.userservice.config.redis;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
* redis默认不支持对象的序列化,需要自定义序列化对象
* redisTemplate序列化支持类
*/
public class RedisObjectSerializer implements RedisSerializer
配置RedisTemplate,为其支持对象的操作
package com.example.userservice.config.redis;
import com.example.userservice.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* RedisTemplate配置类
* 使redis支持插入对象
*
*/
@Configuration
public class RedisTemplateConfig {
/**
* 自定义RedisTemplate bean对象
* @param factory
* @return
*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new RedisObjectSerializer());
return redisTemplate;
}
}
@Autowired
RedisTemplate redisTemplate;
@Test
public void testRedisCluster(){
User u = new User("001","xiaoming",10);
redisTemplate.opsForValue().set("xiaoming",u);
// System.out.println(redisTemplate.opsForValue().get("xiaoming"));
Assert.assertEquals(10,redisTemplate.opsForValue().get("xiaoming" +
"" +
"").getAge().intValue());
}
成功通过测试,结果如下:
实体类实现Serializable接口,启动redis集群的时候,不要使用127.0.0.1或者localhost,而要使用本地的IP地址。