The client uses the latest Jedis 2.7
1.
Maven dependencies:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
2.
add Spring configuration
<bean name= "Genericobjectpoolconfig" class= " Org.apache.commons.pool2.impl.GenericObjectPoolConfig " ><property name=" Maxwaitmillis " Value= "-1" /><property name= "Maxtotal" value= " /><property name=" Minidle " value=" 8 " /><property name=" Maxidle " value=" /></bean> <bean id= "Jediscluster" class= "xxx. Jedisclusterfactory "><property name=" Addressconfig "><value>classpath: Connect-redis.properties</value></property><property name= "AddressKeyPrefix" value= " Address /> <!-- key prefix in the properties file --><property Name= "Timeout" value= "300000" /><property name= "Maxredirections" value= "6" / ><property name= "Genericobjectpoolconfig" ref= "Genericobjectpoolconfig" /></bean
3.
Add connect-redis.properties configuration file
There are 6 nodes configured here
address1=*:*
address2=*:*
address3=*:*
address4=*:*
address5=*:*
address6=*:*
4.
add Java class:
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 {private resource addressconfig;private string addressKeyPrefix ;p rivate jediscluster jediscluster;private integer timeout; private integer maxredirections;private genericobjectpoolconfig genericobjectpoolconfig; Private pattern p = pattern.compile ("^.+[:]\\d{1,5}\\s*$"); @Overridepublic Jediscluster getobject () throws excEption {return jediscluster;} @Overridepublic class<? extends jediscluster> getobjecttype () {return ( This.jediscluster != null ? this.jediscluster.getclass () : jediscluster.class);} @Overridepublic boolean issingleton () {return true;} Private set 5.
To this configuration complete
When used, it can be injected directly, as shown below:
@Autowired
Jediscluster Jediscluster;
Spring Integrated Redis Cluster