springmvc3.2集成redis集群

老项目需要集成redis集群

因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成。

 

jar包准备:

jedis-2.9.0.jar  -- 据说只有这个版本支持 密码...

commons-pool2-2.0.jar

spring-data-redis-1.8.1.RELEASE.jar

spring-data-commons-1.8.1.RELEASE.jar

 


    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxIdle" value="${redis.maxIdle}" />
        
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    bean>

    <bean id="jedis" class="redis.clients.jedis.JedisCluster">
        <constructor-arg>
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host1}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port1}">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host2}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port2}">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host3}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port3}">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host4}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port4}">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host5}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port5}">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${redis.host6}">constructor-arg>
                    <constructor-arg name="port" value="${redis.port6}">constructor-arg>
                bean>
            set>
        constructor-arg>
        <constructor-arg name="connectionTimeout" value="${redis.timeout}" />
        <constructor-arg name="soTimeout" value="${redis.timeout}" />
        <constructor-arg name="maxAttempts" value="${redis.maxAttempts}" />
        <constructor-arg name="password" value="${redis.password}" />
        <constructor-arg name="poolConfig">
            <ref bean="jedisPoolConfig" />
        constructor-arg>
    bean>
    
    

 

redis.maxIdle=200
redis.maxActive=1000
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=30
redis.maxAttempts=300
redis.password=xxxxxx

# 10.10.130.150:1501,10.10.130.150:1502,10.10.130.150:1503,10.10.130.150:1504,10.10.130.150:1505,10.10.130.150:1506
redis.host1=10.10.130.150
redis.port1=1501
redis.host2=10.10.130.150
redis.port2=1502
redis.host3=10.10.130.150
redis.port3=1503
redis.host4=10.10.130.150
redis.port4=1504
redis.host5=10.10.130.150
redis.port5=1505
redis.host6=10.10.130.150
redis.port6=1506
public class RedisUtil {
    private static JedisCluster jedis = (JedisCluster) SpringContextHolder.getBean("jedis");

    private RedisUtil() {
    }
    
    /**
     * 获取指定key的值,如果key不存在返回null,如果该Key存储的不是字符串,会抛出一个错误
     *
     * @param key
     * @return
     */
    public static String getstr(String key) {
        String value = null;
        value = jedis.get(key);
        return value;
    }

    /**
     * 设置key的值为value
     *
     * @param key
     * @param value
     * @return
     */
    public static String set(String key, String value) {
        return jedis.set(key, value);
    }
}

 

如果需要配置 redisTemplate的需要 spring版本 4.x ,否则会有各种错误。

 

转载于:https://www.cnblogs.com/lili-xia/p/11579056.html

你可能感兴趣的:(springmvc3.2集成redis集群)