The Redis cluster I encountered at work is used

Source: Internet
Author: User
Tags mysql connection pool redis cluster

Write an essay, write a note to your work study

Nonsense not much to say, directly on the code:

Features: Store relevant information in Redis and set the expiration time, if any in Redis, get from Redis, if not, get from MySQL. Redis is configured with three clustered environments

1: First, the configuration file is the relevant configuration information, in the Java code, the relevant configuration directly from the configuration file read:

#redis配置MaxActive=10#最大空闲连接数MaxIdle=5#最小空闲连接数MinIdle=3#最大连接数MaxTotal=8  #jedis集群地址AJedisA. Host=192.168.0.191portA=6300#jedis集群地址BJedisB. Host=192.168.0.192  PORTB=6300#jedis集群地址CJedisC. Host=192.168.0.193portc=6300#是否先进先出Lifo=  True#逐出连接的最小空闲时间, default 10 minutes timemillis=600000#用户信息过期时间为1天userPastTime=86400# Split time setting expires at 5 minutes loanpasttime=300#产品过期时间ProductPastTime=60

2: When the project is started, the Redis is initialized:

 // redis Initialize,  Jedispoolconfig is a Parameter object for Jedis, Redis is the object in Java encapsulation, and parameter information is read from the configuration file 
New Jedispoolconfig ();
Config.setmaxidle (Integer.parseint (businessservice.getinstance () getparameter("Maxidle"));
Config.setmaxtotal (Integer.parseint (businessservice.getinstance () getparameter("Maxtotal"));
Config.setminidle (Integer.parseint (businessservice.getinstance () getparameter("Minidle"));
Config.setlifo (Boolean.parseboolean ((Businessservice.getinstance () getparameter("Lifo")));
Config.setminevictableidletimemillis (Long.parselong (businessservice.getinstance () GetParameter(" Timemillis ")));

redisutils (config) is a tool class that is called once at initialization time to initialize the Jedis object.
New Redisutils (config);

3:redisutils class

 Publicredisutils (jedispoolconfig config) {        synchronized( This)        {            if(jediscluster==NULL) {Set<HostAndPort> jedisclusternodes =NewHashset(); Jedisclusternodes.add (NewHostandport (Businessservice.getinstance (). GetParameter ("Jedisa.host"), Integer.parseint ( Businessservice.getinstance (). GetParameter ("PortA")))); Jedisclusternodes.add (NewHostandport (Businessservice.getinstance (). GetParameter ("Jedisb.host"), Integer.parseint ( Businessservice.getinstance (). GetParameter ("PORTB")))); Jedisclusternodes.add (NewHostandport (Businessservice.getinstance (). GetParameter ("Jedisc.host"), Integer.parseint ( Businessservice.getinstance (). GetParameter ("PORTC")))); Jediscluster=NewJediscluster (jedisclusternodes,2000,2000, config); }        }                    }

4:jediscluster stored value

/**     *      * @authorWeishaolong * @date 2016-3-29 5:13:10 Input Parameters *@paramKey Key *@paramseconds effective Time unit seconds *@paramstrvalue Value*/     Public Static voidSetjedis (String Key,intseconds,string strvalue) {        Try        {Jediscluster.setex (Key,seconds,strvalue); }        Catch(Exception e) {log.error ("Redis stored value Exception!" By:RedisUtils.setJedis "+e.tostring ()); }            }


5:jediscluster value

 public  static   string Getjedisvalue (string key) {string Strjedisval  = null  ;  try   {  Strjedisval  =jediscluster.get (key);         return   Strjedisval;             catch   (Exception e) { Log.error ( "Redis value Exception!        By:RedisUtils.getJedisValue "+e.tostring ());             return   Strjedisval; }

6: In this way, the use of jediscluster can be used, Java business code directly Redisutils.getjedisvalue (""); you can get the value.

7 : Shardjedispool can also be used in clusters, but shardjedispool I have always reported

Jedis.exceptions.JedisMovedDataException:MOVED 632 192.168.0.192:6301, how to solve, for the moment I have not found a solution, Just know to change the 193redis server to 191 or 192 to solve

Initialization

List<jedisshardinfo> jdsinfolist =NewArraylist<jedisshardinfo> (3); Jedisshardinfo Infoa=NewJedisshardinfo (Businessservice.getinstance (). GetParameter ("Jedisa.host"), Integer.parseint (Businessservice.getinstance (). GetParameter ("PortA"))); Jedisshardinfo Infob=NewJedisshardinfo (Businessservice.getinstance (). GetParameter ("Jedisb.host"), Integer.parseint (Businessservice.getinstance (). GetParameter ("PORTB"))); Jedisshardinfo INFOC=NewJedisshardinfo (Businessservice.getinstance (). GetParameter ("Jedisc.host"), Integer.parseint (Businessservice.getinstance (). GetParameter ("PORTC")));         Jdsinfolist.add (INFOA);         Jdsinfolist.add (INFOB);         Jdsinfolist.add (INFOC); Shardjedispool=NewShardedjedispool (config, jdsinfolist);

Stored value:

 Public Static void Setjedis (String key,int  seconds,string strvalue)    {        try        {             Shardedjedis Shardjedis=shardjedispool.getresource ();            Shardjedis.setex (key,seconds,strvalue);            Shardjedispool.returnresourceobject (Shardjedis);                     }        catch(Exception e)        {            log.error ("Redis stored value Exception! By:RedisUtils.setJedis "+e.tostring ());        }            }

Value:

 Public Staticstring Getjedisvalue (String key) {string Strjedisval=NULL; Try{Shardedjedis Shardjedis=Shardjedispool.getresource (); Strjedisval=Shardjedis.get (key);            Shardjedispool.returnresourceobject (Shardjedis); returnStrjedisval; }        Catch(Exception e) {log.error ("Redis Value Exception!" By:RedisUtils.getJedisValue "+e.tostring ()); }        returnStrjedisval; }

Finally, one thing to note: Shardjedispool is a connection pool, and MySQL connection pool is very similar, each time you get the value from the pool, remember to return this connection to the pool

Shardjedispool.returnresourceobject (Redis);

Close the connection pool when the project is stopped

 Public Static void Closejedispool ()    {        if(shardjedispool!=null) {            Shardjedispool.destroy ();        }    }}

Well, the main is the code level of explanation, the principle of not how to say, later to add ....

The Redis cluster I encountered at work is used

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.