Spring uses Redis cluster configuration

Source: Internet
Author: User
Tags getmessage redis cluster

The above two introduces the configuration of the Redis cluster with some basic concepts, so of course it is to be used in the project, Redis Java support has done very well, so we try to use these APIs for Redis operations, first we need to operate Redis's rack package:

    <dependency>        <groupId>redis.clients</groupId>        <artifactid>jedis</artifactid >        <version>2.7.3</version>    </dependency>

All we need to do is inject these basic classes into the spring configuration file and implement the DAO ourselves, and here's the configuration file:

<context:property-placeholder ignore-unresolvable= "true" location= "classpath:yonyou.properties"/> <bean i          D= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "maxtotal" value= "$"/> <property name= "Maxidle" value= "ten"/> <property name= "Minidle" value= "1"/> <propert Y name= "Maxwaitmillis" value= "30000"/> <property name= "Testonborrow" value= "true"/> <property Name= "Testonreturn" value= "true"/> <property name= "Testwhileidle" value= "true"/> <!--<prope Rty name= "Testwhileidle" value= "true"/>-</bean> <bean id= "Shardedjedispool" class= "redis.c         Lients.jedis.ShardedJedisPool "destroy-method=" destroy "> <constructor-arg ref=" Jedispoolconfig "/> <constructor-arg>
      <!--If you need to expand your cluster later, just copy a redis, modify the port, and configure it here--<list> <bean class= "redis.client S.jedis.jedisshardinfo "> <constructor-arg index=" 0 "value=" 127.0.0.1 "/> <constr Uctor-arg index= "1" value= "6379"/> <constructor-arg index= "2" value= "instance:01"/> &lt ;/bean> <bean class= "Redis.clients.jedis.JedisShardInfo" > <constructor-arg index= "0" Value= "127.0.0.1"/> <constructor-arg index= "1" value= "6380"/> <constructor-arg index= "2" value= "instance:02"/> </bean> <bean class= "Redis.clients.jedis.JedisShardInfo "> <constructor-arg index=" 0 "value=" 127.0.0.1 "/> <constructor-arg index=" 1 "Val Ue= "6381"/> <constructor-arg index= "2" value= "instance:03"/> </bean> & Lt;/list> </constructor-arg> </bean> <!--Java helps us synchronize Sentinel information, synchronizing the master-slave information to the client--<bean class= "Redis.clien Ts.jedis.JedisSentinelPool "> <constructor-arg index=" 0 "value=" MyMaster "/> <constructor-arg Inde x= "1" > <set> <value>127.0.0.1:26379</value> </set> </constructor-arg> <constructor-arg index= "2" ref= "Jedispoolconfig"/> </bean>

After we've configured it, we're going to have to map the Redis cluster, and we just need to do something about the data crud.

Package Com.yonyou.hotusm.module.nosql.redis;import redis.clients.jedis.shardedjedis;//This interface is Operation Sharedjedispublic Interface Redisdatasource {public        abstract Shardedjedis getredisclient ();    public void Returnresource (Shardedjedis shardedjedis);    public void Returnresource (Shardedjedis shardedjedis,boolean broken);

Realize:

Package Com.yonyou.hotusm.module.nosql.redis;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.repository;import Redis.clients.jedis.shardedjedis;import Redis.clients.jedis.ShardedJedisPool; @Repository ("Redisdatasource") public class Redisdatasourceimpl implements Redisdatasource {private static final Logger log = Loggerfactory.getlogger    (Redisdatasourceimpl.class);    @Autowired private Shardedjedispool Shardedjedispool;            Public Shardedjedis getredisclient () {try {Shardedjedis Shardjedis = Shardedjedispool.getresource ();        return Shardjedis;        } catch (Exception e) {log.error ("Getredisclent error", E);    } return null;        } public void Returnresource (Shardedjedis shardedjedis) {shardedjedispool.close ();    Shardedjedispool.returnresource (Shardedjedis); } public void Returnresource (ShardedjediS Shardedjedis, Boolean broken) {if (broken) {shardedjedispool.close ();        Shardedjedispool.returnbrokenresource (Shardedjedis);            } else {shardedjedispool.close ();        Shardedjedispool.returnresource (Shardedjedis); }    }}

Here are the specific classes for manipulating Jedis:

@Repository ("Jedistemplate") public class Jedistemplate {private static final Logger log = Loggerfactory.getlogger (Jedi    Stemplate.class);    @Autowired private Redisdatasource Redisdatasource;        public void Disconnect () {Shardedjedis Shardedjedis = redisdatasource.getredisclient ();    Shardedjedis.disconnect (); }/** * Sets a single value * * @param key * @param value * @return */Public String Set (string key, Stri        Ng value) {String result = null;        Shardedjedis Shardedjedis = Redisdatasource.getredisclient ();        if (Shardedjedis = = null) {return result;        } Boolean broken = false;        try {result = Shardedjedis.set (key, value);            } catch (Exception e) {e.printstacktrace ();            Log.error (E.getmessage (), E);        Broken = true;        } finally {Redisdatasource.returnresource (Shardedjedis, broken);    } return result; }/** * Get singleA value of * * @param key * @return */public string get (string key) {string result = NULL;        Shardedjedis Shardedjedis = Redisdatasource.getredisclient ();        if (Shardedjedis = = null) {return result;        } Boolean broken = false;        try {result = Shardedjedis.get (key);            } catch (Exception e) {log.error (E.getmessage (), E);        Broken = true;        } finally {Redisdatasource.returnresource (Shardedjedis, broken);    } return result; }

There are really a lot of operations, List,set, hashes and so on, I don't put the code out, look at the API to know

Spring uses Redis cluster configuration

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.