目的
使用SpringBoot 1.5.8.RELEASE版本整合Redis 4.0.11,实现Redis Pool操作Redis集群
引入依赖
<!--属性配置支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<!--redis依赖-->
<!--默认继承lettuce,切换成jedis需要排除依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--redis 客户端-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
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
- nodes 代表redis多个节点的ip与端口号,多个节点需要使用“,”隔开。
- max-redirects 最大的要重定向的次数(由于集群中数据存储在多个节点所以,在访问数据时需要通过节点进行转发)
- timeout 连接超时的时间
- max-attempts 最大的连接重试次数
编写RedisClusterProperty.JAVA
用于获取配置文件中的属性值,在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<String> nodes;
/**
* 获取连接的超时时间
*/
private Integer timeout;
/**
* 最大连接尝试次数
*/
private Integer maxAttempts;
}
编写JedisClusterConfig.JAVA
配置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<String> servers = redisClusterProperty.getNodes();
// Set<HostAndPort> 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;
// }
}
自定义RedisTemplate
SpringData提供的RedisTemplate只支持基本类型的操作,不能处理对象的存储。为了方便对象的操作,我们需要自定义一个RedisTemplate来操作对象。
编写RedisObjectSerializer.JAVA
处理对象的序列化
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<Object> {
private Converter<Object, byte[]> serilazier = new SerializingConverter();
private Converter<byte[], Object> deserilazier = new DeserializingConverter();
private static final byte[] EMPTY_ARRAY = new byte[0];
/**
* 将对象序列化为字节数字,序列化失败返回空数组
*
* @param o
* @return
* @throws SerializationException
*/
@Override
public byte[] serialize(Object o) throws SerializationException {
if (null == o) {
return EMPTY_ARRAY;
}
try {
return serilazier.convert(o);
} catch (Exception e) {
return EMPTY_ARRAY;
}
}
/**
* 将字节数组反列化成对象,序列化失败返回null
*
* @param bytes
* @return
* @throws SerializationException
*/
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if (isEmpty(bytes)) {
return null;
}
try {
return deserilazier.convert(bytes);
} catch (Exception e) {
return null;
}
}
/**
* 判断字符数字是否为空
*
* @param data
* @return
*/
private boolean isEmpty(byte[] data) {
return data == null || data.length == 0;
}
}
编写RedisTemplateConfig.JAVA
配置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<String, User> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new RedisObjectSerializer());
return redisTemplate;
}
}
Junit测试
@Autowired
RedisTemplate<String, User> 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地址。