Spring Integrated Redis Cluster

Source: Internet
Author: User
Tags redis cluster

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

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.