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 id="Jedispoolconfig" class="Redis.clients.jedis.JedisPoolConfig"> <property name="Maxtotal"Value=" +"/> <property name="Maxidle"Value="Ten"/> <property name="Minidle"Value="1"/> <property name="Maxwaitmillis"Value="30000"/> <property name="Testonborrow"Value="true"/> <property name="Testonreturn"Value="true"/> <property name="Testwhileidle"Value="true"/> <!--<property name="Testwhileidle"Value="true"/>-</bean> <bean id="Shardedjedispool" class="Redis.clients.jedis.ShardedJedisPool"Destroy-method="Destroy"> <constructor-argref="Jedispoolconfig"/> <constructor-arg>
<!--If you need to expand your cluster later, you only need to copy a Redis, modify the ports, and configure it here--<list> <beanclass="Redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0"Value="127.0.0.1"/> <constructor-arg index="1"Value="6379"/> <constructor-arg index="2"Value="instance:01"/> </bean> <beanclass="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> <beanclass="Redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0"Value="127.0.0.1"/> <constructor-arg index="1"Value="6381"/> <constructor-arg index="2"Value="instance:03"/> </bean> </list> </constructor-arg> </bean> <!--ja VA helps us synchronize Sentinel information, synchronizing the master and slave information to the client--<beanclass="Redis.clients.jedis.JedisSentinelPool"> <constructor-arg index="0"Value="MyMaster"/> <constructor-arg index="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 Sharedjedis  Publicinterface  redisdatasource {        publicabstract   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 classRedisdatasourceimpl implements Redisdatasource {Private StaticFinal Logger log = Loggerfactory.getlogger (Redisdatasourceimpl.class); @AutowiredPrivateShardedjedispool Shardedjedispool;  PublicShardedjedis getredisclient () {Try{Shardedjedis Shardjedis=Shardedjedispool.getresource (); returnShardjedis; } Catch(Exception e) {log.error ("getredisclent Error", E); }        return NULL; }     Public voidReturnresource (Shardedjedis shardedjedis) {shardedjedispool.close (); //Shardedjedispool.returnresource (Shardedjedis);    }     Public voidReturnresource (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 classJedistemplate {Private StaticFinal Logger log = Loggerfactory.getlogger (jedistemplate.class); @AutowiredPrivateRedisdatasource Redisdatasource;  Public voidDisconnect () {Shardedjedis Shardedjedis=redisdatasource.getredisclient ();    Shardedjedis.disconnect (); }    /** * Set a single value * * @param key * @param value * @return*/     PublicStringSet(string key, String value) {string result=NULL; Shardedjedis Shardedjedis=redisdatasource.getredisclient (); if(Shardedjedis = =NULL) {            returnresult; } 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); }        returnresult; }    /** * Get a single value * * @param key * @return*/     PublicStringGet(String key) {string result=NULL; Shardedjedis Shardedjedis=redisdatasource.getredisclient (); if(Shardedjedis = =NULL) {            returnresult; } Boolean Broken=false; Try{result= Shardedjedis.Get(key); } Catch(Exception e) {log.error (E.getmessage (), E); Broken=true; } finally{redisdatasource.returnresource (Shardedjedis, broken); }        returnresult; }

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.