Setup and Jedis of Sentinel cluster in Redis [II]

Source: Internet
Author: User
Tags redis server

The setup and testing of Sentinel clusters in Redis has been written in the setup and Jedis of the Sentinel cluster in Redis, and the Jedis test of Sentinel in Redis is mainly written in this chapter.

General Sentinel architecture diagram for at least 4 machine tests

I tested it with only two machines, one running Redis-sentinel and all the Redis Master-slave, one running Java test program.

Xserver.ini configuration file for Project engineering

[Redis]redis_numbers=3 redis_server_1=tcp://192.168.  0.86:26379  redis_server_2=tcp://192.168.  0.86:26479  redis_server_3=tcp://192.168.  0.86:26579  redis_master=MyMaster redis_password=vhreal redis_time_out= redis_database=0 

Redisconnector.java Encapsulation class for Redis operations

ImportJava.util.HashSet;ImportJava.util.Set;ImportOrg.apache.commons.pool2.impl.GenericObjectPoolConfig;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisSentinelPool;ImportAlbert.CfgFileReader.CfgFileReader; Public  class redisconnector {    Static  PublicHashset<string> Redis_server =NewHashset<string> ();Static  Public intRedis_numbers =1;Static  PublicString Redis_master ="Master";Static  PublicString Redis_password ="";Static  Public intRedis_time_out = +;Static  Public intRedis_database =0;Static  PublicJedissentinelpool Jedissentinelpool =NULL;Static  PublicObject syncobj =NewObject ();Static  Public void Start() {synchronized(syncobj) {Cfgfilereader cfg =NULL;Try{cfg =NewCfgfilereader ("Xserver.ini");//[Redis]Redis_numbers = Cfg.getinteger ("Redis_numbers",1); Redis_master = cfg.getstring ("Redis_master","Master"); Redis_password = cfg.getstring ("Redis_password","Master"); Redis_time_out = Cfg.getinteger ("Redis_time_out", the); Redis_database = Cfg.getinteger ("Redis_database",0);if(redis_password.length () = =0) Redis_password =NULL; for(inti =0; i < redis_numbers; ++i) {String key = String.Format ("redis_server_%d", i +1); String value = cfg.getstring (Key,""); Value = Value.replace ("tcp://",""); Value = Value.replace ("/",""); Value = value.tostring (). Trim ();if(Value.length () >0) {Redis_server.add (value); }                }if(Jedissentinelpool! =NULL) {Jedissentinelpool.destroy (); Jedissentinelpool =NULL; } System.out.println ("Creating jedissentinelpool ..."); Jedissentinelpool =NewJedissentinelpool (Redis_master, Redis_server,NewGenericobjectpoolconfig (), Redis_time_out, Redis_password, redis_database); System.out.println ("Create jedissentinelpool Success"); }Catch(Exception ex)            {Ex.printstacktrace (); }finally{if(CFG! =NULL) {cfg.close (); CFG =NULL; }            }        }    }Static  Public Boolean Set(string key, String value) {if(Jedissentinelpool = =NULL)return false;synchronized(syncobj) {Jedis Jedis =NULL;Try{Jedis = Jedissentinelpool.getresource ();if(Jedis! =NULL) {if(Value = =NULL) value =""; Jedis.set (key, value);return true; }            }Catch(Exception ex)                {Ex.printstacktrace ();            Jedissentinelpool.returnbrokenresource (Jedis); }finally{Jedissentinelpool.returnresource (Jedis); }return false; }    }Static  PublicStringGet(string key, String defaultval) {if(Jedissentinelpool = =NULL)returnDefaultval;synchronized(syncobj) {Jedis Jedis =NULL;Try{Jedis = Jedissentinelpool.getresource ();if(Jedis! =NULL) {String value = Jedis.get (key);if(Value = =NULL) value = Defaultval;returnValue }            }Catch(Exception ex)                {Ex.printstacktrace ();            Jedissentinelpool.returnbrokenresource (Jedis); }finally{Jedissentinelpool.returnresource (Jedis); }returnDefaultval; }    }Static  Public Boolean Del(String key) {if(Jedissentinelpool = =NULL)return false;synchronized(syncobj) {Jedis Jedis =NULL;Try{Jedis = Jedissentinelpool.getresource ();if(Jedis! =NULL) {Jedis.del (key);return true; }            }Catch(Exception ex)                {Ex.printstacktrace ();            Jedissentinelpool.returnbrokenresource (Jedis); }finally{Jedissentinelpool.returnresource (Jedis); }return false; }    }Static  Public Boolean Auth(String key) {if(Jedissentinelpool = =NULL)return false;synchronized(syncobj) {Jedis Jedis =NULL;Try{Jedis = Jedissentinelpool.getresource ();if(Jedis! =NULL) {Jedis.auth (key);return true; }            }Catch(Exception ex)                {Ex.printstacktrace ();            Jedissentinelpool.returnbrokenresource (Jedis); }finally{Jedissentinelpool.returnresource (Jedis); }return false; }    }Static  PublicSet<string>Select(String pattern) {if(Jedissentinelpool = =NULL)return NULL;synchronized(syncobj) {Jedis Jedis =NULL;Try{Jedis = Jedissentinelpool.getresource ();if(Jedis! =NULL{set<string> keys = Jedis.keys (pattern);returnKeys }            }Catch(Exception ex)                {Ex.printstacktrace ();            Jedissentinelpool.returnbrokenresource (Jedis); }finally{Jedissentinelpool.returnresource (Jedis); }return NULL; }    }}

Redistest.java to test the main function

publicclass RedisTest {    publicstaticvoidmain(String[] args) {        RedisConnector.Start();        RedisConnector.Set("str1""world");        System.out.println(RedisConnector.Get("str""default"));        System.out.println(RedisConnector.Get("str1""default"));    }}

Initial run of the Redistest.java file, you will find the following issues

Because I was tested with two machines, a running Redis server with a server IP of 192.168.0.86. A Run Test Java program, the test program is located on the machine's IP is 192.168.0.83. Two machines in a LAN segment.

But before I had sentinel configuration master-slave, some IP was set to 127.0.0.1, of course, the test program on 192.168.0.83 cannot connect to the 127.0.0.1 server and cannot get the resources.

Change the IP in Sentinel configuration to 192.168.0.86

The result of running the program is as follows:

Setup and Jedis of Sentinel cluster in Redis [II]

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.