Redis and Spring Integration

Source: Internet
Author: User
Tags aop redis cluster

Integration of Redis and spring frameworks

I'm creating a MAVEN project here that manages dependencies between jar packages by locking the version number with Maven

1. In the Pom file, introduce the coordinates of the spring and Redis jar packages:

<properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.release</s pring.version> <jedis.version>2.7.2</jedis.version> </properties><!--Spring--&        Gt <dependency> <groupId>org.springframework</groupId> <artifactid>spring-conte xt</artifactid> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK&L t;/groupid> <artifactId>spring-beans</artifactId> </dependency> <dependen Cy> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</artifa ctid> </dependency> <dependency> <groupid>org.springframework</groupid&gt            ; <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupid> <artifactId>spring-aspects</artifactId> </depen dency> <dependency> <groupId>org.springframework</groupId> <artifacti d>spring-jms</artifactid> </dependency> <dependency> <groupid>org.spri Ngframework</groupid> <artifactId>spring-context-support</artifactId> </dependency&        Gt <!--Redis Client--<dependency> <groupId>redis.clients</groupId> <a Rtifactid>jedis</artifactid> </dependency>

2. Encapsulate the basic data type operation of Redis into a tool class

Define the interface jedisclient:

 Package Nyist.e3.utils.redis;  Public Interface jedisclient {    string Set (String key, String value);    String get (string key);    Boolean exists (String key);     int seconds);    A Long ttl (String key);    Long incr (String key);    Long Hset (String key, String field, String value);    String Hget (String key, string field);    Long Hdel (String key, String ... field);}

The standalone version implements the operation of this interface:

 PackageNyist.e3.utils.redis;Importorg.springframework.beans.factory.annotation.Autowired;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool; Public classJedisclientpoolImplementsjedisclient {PrivateJedispool Jedispool;  PublicJedispool Getjedispool () {returnJedispool; }     Public voidSetjedispool (Jedispool jedispool) { This. Jedispool =Jedispool; } @Override Publicstring Set (String key, String value) {Jedis Jedis=Jedispool.getresource (); String result=Jedis.set (key, value);        Jedis.close (); returnresult; } @Override Publicstring Get (String key) {Jedis Jedis=Jedispool.getresource (); String result=Jedis.get (key);        Jedis.close (); returnresult; } @Override PublicBoolean exists (String key) {Jedis Jedis=Jedispool.getresource (); Boolean result=jedis.exists (key);        Jedis.close (); returnresult; } @Override PublicLong expire (String key,intseconds) {Jedis Jedis=Jedispool.getresource (); Long result=Jedis.expire (key, seconds);        Jedis.close (); returnresult; } @Override PublicLong TTL (String key) {Jedis Jedis=Jedispool.getresource (); Long result=Jedis.ttl (key);        Jedis.close (); returnresult; } @Override PublicLong incr (String key) {Jedis Jedis=Jedispool.getresource (); Long result=JEDIS.INCR (key);        Jedis.close (); returnresult; } @Override PublicLong Hset (String key, String field, String value) {Jedis Jedis=Jedispool.getresource (); Long result=Jedis.hset (Key, field, value);        Jedis.close (); returnresult; } @Override Publicstring Hget (String key, String field) {Jedis Jedis=Jedispool.getresource (); String result=Jedis.hget (key, field);        Jedis.close (); returnresult; } @Override PublicLong Hdel (string key, String ... field) {Jedis Jedis=Jedispool.getresource (); Long result=Jedis.hdel (key, field);        Jedis.close (); returnresult; }}

The cluster implements the interface:

 PackageNyist.e3.utils.redis;ImportRedis.clients.jedis.JedisCluster; Public classJedisclientclusterImplementsjedisclient {PrivateJediscluster Jediscluster;  PublicJediscluster Getjediscluster () {returnJediscluster; }     Public voidSetjediscluster (Jediscluster jediscluster) { This. Jediscluster =Jediscluster; } @Override Publicstring Set (String key, String value) {returnJediscluster.set (key, value); } @Override Publicstring Get (String key) {returnJediscluster.get (key); } @Override PublicBoolean exists (String key) {returnjediscluster.exists (key); } @Override PublicLong expire (String key,intseconds) {        returnJediscluster.expire (key, seconds); } @Override PublicLong TTL (String key) {returnJediscluster.ttl (key); } @Override PublicLong incr (String key) {returnJEDISCLUSTER.INCR (key); } @Override PublicLong Hset (String key, String field, String value) {returnJediscluster.hset (Key, field, value); } @Override Publicstring Hget (String key, String field) {returnJediscluster.hget (key, field); } @Override PublicLong Hdel (string key, String ... field) {returnJediscluster.hdel (key, field); }}

3. Configure a standalone connection pool in spring

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" 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.2.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp//Code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd"><!--the creation of a Redis object to the Spring container--<!--to register a standalone Redis, create a jedispool, you need to inject host and Port-to-<!--to connect to Redis standalone- <!--register a standalone version of the implementation class--<bean id= "Jedisclientpool"class= "Nyist.e3.utils.redis.JedisClientPool" > <property name= "jedispool" ref= "Jedispool" ></property> & lt;/bean> <bean id= "Jedispool"class= "Redis.clients.jedis.JedisPool" > <constructor-arg name= "host" value= "IP"/> <constructor-arg na Me= "Port" value= "6379"/> </bean></beans>

4.redis cluster configuration in spring

<bean id= "Jediscluster"class= "Redis.clients.jedis.JedisCluster" > <!--to inject a list of nodes by constructing and then create Hostandport objects in the list, by constructing the host and Port-to-< Constructor-arg> <set> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx1" ></constructor-arg> </be An> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx2" ></constructor-arg> </be An> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx3" ></constructor-arg> </be An> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx4" ></constructor-arg> </be An> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx5" ></constructor-arg> </be An> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "IP" ></constructor-a rg> <constructor-arg name= "Port" value= "xxxx6" ></constructor-arg> </be an> </set> </constructor-arg> </bean> <!--Configuring a Redis Cluster implementation class--<bea n id= "Jedisclientcluster"class= "Nyist.e3.utils.redis.JedisClientCluster" > <property name= "jediscluster" ref= "Jediscluster" ></ Property> </bean>

Note that: The configuration of the standalone version and the configuration of the cluster version can not exist at the same time, if using the cluster version, the single-machine version of the comments, using a standalone version, commenting on the configuration of the cluster version. In short, configure only one.

6. Words not much to say, directly on the test code:

@Test      Public void Testpool () {        new classpathxmlapplicationcontext ("classpath:spring/ Applicationcontext-redis.xml ");         = (Jedisclientpool) context.getbean ("Jedisclientpool");        Jedisclientpool.set ("School", "Nyist");         = Jedisclientpool.get ("school");        System.out.println (value);    }
View Code

Redis and Spring Integration

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.