上一篇记录了redis的集群,这里记录下spring redis 集群配置
spring redis集群配置记录
xml代码
<!--连接池配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1000"/>
<property name="maxTotal" value="1000"/>
<property name="maxWaitMillis" value="1000"/>
</bean>
<!--集群配置-->
<bean id="clusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6381"/>
</bean>
</set>
</property>
</bean>
<!--连接工厂-->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="clusterConfiguration"/>
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
JedisConnectionFactory是一个工程类源码
Junit代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = “classpath:applicationContext.xml” )
public class TestJunit {
@Resource(name="connectionFactory")
private JedisConnectionFactory connectionFactory;
@Test
public void test(){
RedisConnection connection=connectionFactory.getClusterConnection();
connection.set("abc".getBytes(), "abc".getBytes());
System.out.println(new String(connection.get("abc".getBytes())));
}
}
这样就简单实现了spring redis的集群
添加扩展 配合spring cache
配置如下
<context:component-scan base-package="com.test.junit" />
<cache:annotation-driven />
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1000"/>
<property name="maxTotal" value="1000"/>
<property name="maxWaitMillis" value="1000"/>
</bean>
<bean id="clusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="127.0.0.1"/>
<constructor-arg name="port" value="6381"/>
</bean>
</set>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="clusterConfiguration"/>
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultSerializer" ref="jdkSerializer" />
<property name="keySerializer" ref="stringSerializer" />
<!-- <property name="valueSerializer" ref="stringSerializer" />-->
<!--是否开启事务-->
<!--<property name="enableTransactionSupport" value="true" />-->
</bean>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate" />
<!--缓存默认时间-->
<property name="defaultExpiration" value="1000" />
<!--是否开启前缀-->
<property name="usePrefix" value="true" />
<!--缓存时间-->
<property name="expires">
<map>
<entry key="messageCache" value="30"></entry>
</map>
</property>
</bean>
这样就实现了 spring 集群的 cache缓存