Redis集群xml配置和工具类
redis单机改集群
由于公司项目在用为redis单机,在存储和查询性能方面需要调优,所以改为redis集群,自己也研究了一下,配置redisCluster可以通过bean注入也可以写一个工具类,本质都是通过spring容器实例化redisCluster来提供redis数据的存取。下面介绍一下xml配置bean和工具类配置。
bean文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="minIdle" value="${min-idle}" />
<property name="maxIdle" value="${max-idle}" />
<property name="maxTotal" value="${max-active}" />
</bean>
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host1}" />
<constructor-arg name="port" value="${port1}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host2}" />
<constructor-arg name="port" value="${port2}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host3}" />
<constructor-arg name="port" value="${port3}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host4}" />
<constructor-arg name="port" value="${port4}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host5}" />
<constructor-arg name="port" value="${port5}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${host6}" />
<constructor-arg name="port" value="${port6}" />
</bean>
</set>
</property>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="jedisPoolConfig"></property>
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration" />
<property name="password" value="${password}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="KeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="ValueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
<property name="HashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="HashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
</bean>
</beans>
使用的简单的三主三从,通过配置文件注入。
RedisCluster工具类
@Log4j2
@Configuration
@EnableAutoConfiguration
@Component
public class CacheConfig implements Serializable {
private static final long serialVersionUID = -2539468533583469346L;
@Value("${password}")
private String password;
@Value("${timeout}")
private int timeout;
@Value("${namespace}")
private String namespace;
@Value("${maxtotal}")
private int maxtotal;
@Value("${maxidle}")
private int maxidle;
@Value("${minidle}")
private int minidle;
@Value("${maxwaitmillis}")
private int maxwaitmillis;
@Value("${redisClusterNotes}")
private String redisClusterNotes;
@Value("${soTimeout}")
private int soTimeout;
@Value("${maxAttempts}")
private int maxAttempts;
@Value("${maxRedirects}")
private int maxRedirects;
@Bean(name = "jedisPoolConfig")
public JedisPoolConfig getJedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxtotal);
jedisPoolConfig.setMaxIdle(maxidle);
jedisPoolConfig.setMinIdle(minidle);
jedisPoolConfig.setMaxWaitMillis(maxwaitmillis);
jedisPoolConfig.setTestOnBorrow(false);
jedisPoolConfig.setTestOnReturn(false);
jedisPoolConfig.setTestWhileIdle(false);
return jedisPoolConfig;
}
@Bean(name = "redisClusterConfiguration")
public RedisClusterConfiguration getRedisClusterConfiguration(){
if (StringUtils.isEmpty(redisClusterNotes)){
log.error("redis集群节点为空");
throw new RuntimeException();
}
String[] hostAndPorts = redisClusterNotes.split(","