1, the use of Jedis native Jediscluster
Spring's applicationcontext.xml configuration for redis connections, connection pooling, jediscluster beans
<BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>Classpath:redis.properties</value> </List> </ Property></Bean><!--Redis config start - <!--Redis Pool config - <BeanID= "Genericobjectpoolconfig"class= "Org.apache.commons.pool2.impl.GenericObjectPoolConfig"> < Propertyname= "Maxtotal"value= "${redis.maxactive}" /> < Propertyname= "Maxidle"value= "${redis.maxidle}" /> < Propertyname= "Maxwaitmillis"value= "${redis.maxwaitmillis}" /> < Propertyname= "Testonborrow"value= "${redis.testonborrow}"/> </Bean> <!--jediscluster config - <BeanID= "Jediscluster"class= "Redis.clients.jedis.JedisCluster"> <Constructor-argIndex= "0"> <Set> <Beanclass= "Redis.clients.jedis.HostAndPort"> <Constructor-argtype= "String"value= "${redis.host1}"/> <Constructor-argtype= "int"value= "${redis.port1}"/> </Bean> <Beanclass= "Redis.clients.jedis.HostAndPort"> <Constructor-argtype= "String"value= "${redis.host2}"/> <Constructor-argtype= "int"value= "${redis.port2}"/> </Bean> </Set> </Constructor-arg> <Constructor-argIndex= "1"ref= "Genericobjectpoolconfig" /> </Bean> <!--redis config End -
Configuration of the Redis.xml:
#redis Configredis.maxactive=1000redis.maxidle=10redis.maxwaitmillis=30000redis.testonborrow=true#redis Host and Port configredis.host1=192.168.1.2redis.port1=6379redis.host2=192.168.1.2redis.port2=6380
Use of Jediscluster:
@Autowired Private Jediscluster Jedisclust;
2. Custom Spring Factory production Jediscluster
Jedisclusterfactory.java
Packagecom.www.core.utils;ImportJava.util.HashSet;Importjava.util.Properties;ImportJava.util.Set;ImportJava.util.regex.Pattern;ImportOrg.apache.commons.pool2.impl.GenericObjectPoolConfig;ImportOrg.springframework.beans.factory.FactoryBean;ImportOrg.springframework.beans.factory.InitializingBean;ImportOrg.springframework.core.io.Resource;ImportRedis.clients.jedis.HostAndPort;ImportRedis.clients.jedis.JedisCluster; Public classJedisclusterfactoryImplementsFactorybean<jediscluster>, Initializingbean {PrivateString address; PrivateJediscluster Jediscluster; PrivateInteger timeout; PrivateInteger maxredirections; PrivateGenericobjectpoolconfig Genericobjectpoolconfig; PrivatePattern p = pattern.compile ("^.+[:]\\d{1,5}\\s*$"); @Override PublicJediscluster GetObject ()throwsException {returnJediscluster; } @Override Publicclass<?extendsJediscluster>Getobjecttype () {return( This. jediscluster! =NULL? This. Jediscluster.getclass (): Jediscluster.class); } @Override Public BooleanIssingleton () {return true; } PrivateSetthrowsException {Try{string[] Addressarr=address.trim (). Split (","); Set<HostAndPort> haps =NewHashset(); for(String Addressstr:addressarr) {string[] Ipandport= Addressstr.trim (). Split (":"); Hostandport Hap=NewHostandport (Ipandport[0].trim (), Integer.parseint (ipandport[1].trim ())); Haps.add (HAP); } returnhaps; } Catch(IllegalArgumentException ex) {Throwex; } Catch(Exception ex) {Throw NewException ("Parsing Jedis configuration file failed", ex); }} @Override Public voidAfterpropertiesset ()throwsException {Set<HostAndPort> haps = This. Parsehostandport (); Jediscluster=Newjediscluster (haps, timeout, maxredirections,genericobjectpoolconfig); } Public voidSetTimeout (intTimeout) { This. Timeout =timeout; } Public voidSetmaxredirections (intmaxredirections) { This. maxredirections =maxredirections; } /*** @Param String address to set*/ Public voidsetaddress (String address) { This. Address =address; } Public voidsetgenericobjectpoolconfig (Genericobjectpoolconfig genericobjectpoolconfig) { This. Genericobjectpoolconfig =Genericobjectpoolconfig; }}
Spring's Applicationcontext.xml Configure the Redis connection pool and factory bean
<!--Redis Connection Configuration start - <Beanname= "Genericobjectpoolconfig"class= "Org.apache.commons.pool2.impl.GenericObjectPoolConfig" > < Propertyname= "Maxidle"value= "${redis.maxidle}"/> < Propertyname= "Maxtotal"value= "${redis.maxtotal}"/> < Propertyname= "Minidle"value= "${redis.minidle}" /> </Bean> <!--Redis Connection Configuration End - <BeanID= "Jediscluster"class= "Com.www.core.utils.JedisClusterFactory"> < Propertyname= "Address"value= "${redis.adress}" /> < Propertyname= "Timeout"value= "${redis.timeout}" /> < Propertyname= "Maxredirections"value= "${redis.maxredirections}" /> < Propertyname= "Genericobjectpoolconfig"ref= "Genericobjectpoolconfig" /> </Bean>
Redis.xml Configuration
#redis Configredis.maxtotal=200redis.maxidle=50redis.minidle=10#redis host and Port configredis.adress= 192.168.1.2:6379,192.168.1.2:6380,192.168.1.2:6381redis.timeout=300000redis.maxredirections=6
Use of Jediscluster
@Autowired Private Jediscluster Jediscluster;
Redis cluster Java Client jediscluster Spring integration method