redis cluster java client jedisCluster spring集成方法

1、使用 jedis 的原生 JedisCluster

spring 的 applicationContext.xml 配置 redis 的连接、连接池、jedisCluster Bean

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
</bean>

<!-- redis config start -->
<!-- redis pool config -->
<bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- jedisCluster config -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis.host1}"/>
<constructor-arg type="int" value="${redis.port1}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg type="String" value="${redis.host2}"/>
<constructor-arg type="int" value="${redis.port2}"/>
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" ref="genericObjectPoolConfig" />
</bean>
<!-- redis config end -->

redis.xml 的配置:

#redis config
redis.maxActive=1000
redis.maxIdle=10
redis.maxWaitMillis=30000
redis.testOnBorrow=true

#redis host and port config
redis.host1=192.168.1.2
redis.port1=6379
redis.host2=192.168.1.2
redis.port2=6380

jedisCluster 的使用:

@Autowired
private JedisCluster jedisClust;

 

2、自定义 spring 工厂类生产 jedisCluster

JedisClusterFactory.java

package com.www.core.utils;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String address;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> JedisCluster jedisCluster;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer timeout;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer maxRedirections;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> GenericObjectPoolConfig genericObjectPoolConfig;

</span><span style="color: rgba(0, 0, 255, 1)">private</span> Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$"<span style="color: rgba(0, 0, 0, 1)">);

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> JedisCluster getObject() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedisCluster;
}

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> Class&lt;? <span style="color: rgba(0, 0, 255, 1)">extends</span> JedisCluster&gt;<span style="color: rgba(0, 0, 0, 1)"> getObjectType() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.jedisCluster != <span style="color: rgba(0, 0, 255, 1)">null</span> ? <span style="color: rgba(0, 0, 255, 1)">this</span>.jedisCluster.getClass() : JedisCluster.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
}

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isSingleton() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}



</span><span style="color: rgba(0, 0, 255, 1)">private</span> Set&lt;HostAndPort&gt; parseHostAndPort() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        String[] addressArr</span>=address.trim().split(","<span style="color: rgba(0, 0, 0, 1)">);
        Set</span>&lt;HostAndPort&gt; haps = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;HostAndPort&gt;<span style="color: rgba(0, 0, 0, 1)">();
        </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(String addressStr:addressArr){
            String[] ipAndPort </span>= addressStr.trim().split(":"<span style="color: rgba(0, 0, 0, 1)">);
            HostAndPort hap </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1<span style="color: rgba(0, 0, 0, 1)">].trim()));
            haps.add(hap);
        }
        
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> haps;
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IllegalArgumentException ex) {
        </span><span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> ex;
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex) {
        </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception("解析 jedis 配置文件失败"<span style="color: rgba(0, 0, 0, 1)">, ex);
    }
}

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> afterPropertiesSet() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
    Set</span>&lt;HostAndPort&gt; haps = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.parseHostAndPort();
    
    jedisCluster </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
    
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setTimeout(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> timeout) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.timeout =<span style="color: rgba(0, 0, 0, 1)"> timeout;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setMaxRedirections(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> maxRedirections) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.maxRedirections =<span style="color: rgba(0, 0, 0, 1)"> maxRedirections;
}



</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * @Param String address to set
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAddress(String address) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.address =<span style="color: rgba(0, 0, 0, 1)"> address;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.genericObjectPoolConfig =<span style="color: rgba(0, 0, 0, 1)"> genericObjectPoolConfig;
}

}

 

spring 的 applicationContext.xml 配置 redis 的连接池和工厂 bean

    <!-- redis 连接配置 start-->
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="genericObjectPoolConfig"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="org.apache.commons.pool2.impl.GenericObjectPoolConfig"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="maxIdle"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.maxIdle}"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="maxTotal"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.maxTotal}"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="minIdle"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.minIdle}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> redis连接配置 end</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="jedisCluster"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="com.www.core.utils.JedisClusterFactory"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="address"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.adress}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="timeout"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.timeout}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="maxRedirections"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.maxRedirections}"</span>  <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="genericObjectPoolConfig"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="genericObjectPoolConfig"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>

 

redis.xml 配置

#redis config
redis.maxTotal=200
redis.maxIdle=50
redis.minIdle=10

#redis host and port config
redis.adress=192.168.1.2:6379,192.168.1.2:6380,192.168.1.2:6381
redis.timeout=300000
redis.maxRedirections=6

 

jedisCluster 的使用

@Autowired
private JedisCluster jedisCluster;