Using spring Data Redis to operate Redis (Cluster edition)

Source: Internet
Author: User

Following the previous article http://www.cnblogs.com/EasonJim/p/7804545.html using spring Data Redis to operate Redis is a standalone version, if the integration of the cluster version is actually not very different. The main ideas are as follows:

1, first establish a connection factory, this connection factory is used to set IP, port, account password and so on. (At this step, pass a list of addresses for a cluster, no longer a separate one to specify)

2. Set up session by connecting factory.

3, then inject the session into the code to use.

A brief implementation step, the method of operation and a standalone version of the similar:

One, the use of Spring-data-redis jar package preparation

First spring-data-redis you need to be 1.7 above the version.

POM:

<Dependency>    <groupId>Org.springframework.data</groupId>    <Artifactid>Spring-data-redis</Artifactid>    <version>1.7.5.RELEASE</version></Dependency>

He will rely on some packages, for example spring-data-commons , in the spring-data-x series, to rely on the package. If you have used a series of packages in your previous project, you spring-data-x may need to upgrade them because they all depend on each other, but it is important to note that the current cluster needs to be in spring-data-commons the same spring-data-redis spring-data-commons 1.12.x version.

Second, Spring-data-redis configuration file configuration

<Description>Jedis Cluster Configuration</Description><!--Load Configuration Properties file load on Demand -<Context:property-placeholderignore-unresolvable= "true" Location= "Classpath:redis.cluster.properties" /><!--Configure cluster -<BeanID= "Redisclusterconfiguration"class= "Org.springframework.data.redis.connection.RedisClusterConfiguration">    < Propertyname= "MaxRedirects"value= "3"></ Property>    <!--node Configuration -    < Propertyname= "Clusternodes">        <Set>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30006"></Constructor-arg>            </Bean>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30001"></Constructor-arg>            </Bean>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30002"></Constructor-arg>            </Bean>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30003"></Constructor-arg>            </Bean>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30004"></Constructor-arg>            </Bean>            <Beanclass= "Org.springframework.data.redis.connection.RedisClusterNode">                <Constructor-argname= "Host"value= "192.168.0.201"></Constructor-arg>                <Constructor-argname= "Port"value= "30005"></Constructor-arg>            </Bean>        </Set>    </ Property></Bean><BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig">    < Propertyname= "Maxidle"value= "+" />    < Propertyname= "Maxtotal"value= "All" /></Bean><BeanID= "Jeidsconnectionfactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <Constructor-argref= "Redisclusterconfiguration" />    <Constructor-argref= "Jedispoolconfig" /></Bean><!--templates for Redis access -<BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate">    < Propertyname= "ConnectionFactory"ref= "Jeidsconnectionfactory" /></Bean>

Operation:

@Autowired redistemplate redistemplate; @Test Public voidTestuserinfo () {Redistemplate.execute (NewRediscallback<integer>() {            //here The return value is a generic type in the above rediscallback<integer>,             PublicInteger Doinredis (redisconnection connection) {inti = 0;  for(; i < i++;) {                    byte[] key = ("Key:" +i). GetBytes (); byte[] Value = ("Value:" +i). GetBytes ();                Connection.set (key, value); }                //here The return value is a generic type in the above rediscallback<integer>,                returni;            }        }); }

Reference:

https://docs.spring.io/spring-data/redis/docs/1.8.8.RELEASE/reference/html/#cluster (Official document, annotation-based injection)

Http://www.sojson.com/blog/203.html (above from this article, XML-based configuration)

http://blog.csdn.net/MOTUI/article/details/52903397

http://blog.csdn.net/moshenglv/article/details/72637305

Using spring Data Redis to operate Redis (Cluster edition)

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.