spring redis集群配置

上一篇记录了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是一个工程类源码

connectionFacotry

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缓存

结果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值