Java Integrated Redis cluster

Source: Internet
Author: User
Tags redis cluster

Spring-redis.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.1.xsd ">    <!--Jedis cluster config -    <Beanname= "Genericobjectpoolconfig"class= "Org.apache.commons.pool2.impl.GenericObjectPoolConfig" >        < Propertyname= "Maxwaitmillis"value= "-1" />        < Propertyname= "Maxtotal"value= "+" />        < Propertyname= "Minidle"value= "8" />        < Propertyname= "Maxidle"value= "+" />    </Bean>    <BeanID= "Jediscluster"class= "Qgs.npms.redis.JedisClusterFactory">        < Propertyname= "Addressconfig">            <value>Classpath:/redis/redis-config.properties</value>        </ Property>        < Propertyname= "Addresskeyprefix"value= "Address" />        < Propertyname= "Timeout"value= "300000" />        < Propertyname= "Maxredirections"value= "6" />        < Propertyname= "Genericobjectpoolconfig"ref= "Genericobjectpoolconfig" />    </Bean></Beans>
Create yourself a class Jedisclusterfactory:
ImportJava.util.HashSet;ImportJava.util.Iterator;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 {PrivateResource Addressconfig; PrivateString Addresskeyprefix; PrivateJediscluster Jediscluster; PrivateInteger timeout; PrivateInteger maxredirections; PrivateGenericobjectpoolconfig Genericobjectpoolconfig; PrivatePattern p = pattern.compile ("^.+[:]\\d{1,5}\\s*$");  Publicjedisclusterfactory () {} PublicJediscluster GetObject ()throwsException {return  This. Jediscluster; }     Publicclass<?extendsJediscluster>Getobjecttype () {return  This. jediscluster! =NULL? This. Jediscluster.getclass (): Jediscluster.class; }     Public BooleanIssingleton () {return true; }    PrivateSetthrowsException {Try{Properties ex=NewProperties (); Ex.load ( This. Addressconfig.getinputstream ()); HashSet Haps=NewHashSet (); Iterator i$=Ex.keyset (). iterator ();  while(I$.hasnext ()) {Object key=I$.next (); if((String) key). StartsWith ( This. Addresskeyprefix)) {String Val=(String) ex.get (key); BooleanIsipport = This. P.matcher (Val). Matches (); if(!Isipport) {                        Throw NewIllegalArgumentException ("IP or port is not valid"); } string[] Ipandport= Val.split (":"); Hostandport Hap=NewHostandport (Ipandport[0], Integer.parseint (ipandport[1]));                Haps.add (HAP); }            }            returnhaps; } Catch(IllegalArgumentException var9) {ThrowVar9; } Catch(Exception var10) {Throw NewException ("Parsing Jedis configuration file failed", VAR10); }    }     Public voidAfterpropertiesset ()throwsException {Set haps= This. Parsehostandport ();  This. Jediscluster =NewJediscluster (Haps, This. Timeout.intvalue (), This. Maxredirections.intvalue (), This. Genericobjectpoolconfig); }     Public voidsetaddressconfig (Resource addressconfig) { This. Addressconfig =Addressconfig; }     Public voidSetTimeout (intTimeout) {         This. Timeout =integer.valueof (timeout); }     Public voidSetmaxredirections (intmaxredirections) {         This. maxredirections =integer.valueof (maxredirections); }     Public voidSetaddresskeyprefix (String addresskeyprefix) { This. Addresskeyprefix =Addresskeyprefix; }     Public voidsetgenericobjectpoolconfig (Genericobjectpoolconfig genericobjectpoolconfig) { This. Genericobjectpoolconfig =Genericobjectpoolconfig; }}

Redis-config.properties:

address1=192.168.1.36:6379address2=192.168.1.37:6379address3=192.168.1.38:6379address4 =192.168.1.40:6379address5=192.168.1.41:6379address6=192.168.1.42:6379

Java calls:

@Autowired
Jediscluster; jediscluster is the corresponding Bean ID can be automatically injected
System. out.println (jediscluster.get ("foo"));
System. out.println (jediscluster.set ("QQ","222"));
System. out.println (jediscluster.get ("QQ"));

Java Integrated Redis cluster

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.