Redis's Sentinel Master-slave switchover (failover) automatically re-connects to the Jedis thread pool

Source: Internet
Author: User

This article describes how to monitor Redis master-slave clusters through Sentinel and automatically switch IP and ports via Jedis.

1. Configuring Redis Master-Slave instances

10.93.21.21:6379

10.93.21.21:6389

10.93.21.21:6399

Master-Slave synchronization relationship

master:10.93.21.21:6379

slave:10.93.21.21:6389,10.93.21.21:6399

The master configuration is as follows:

10.93. 21.21  6379"/home/data_monitor/redis-3.2.9/log/6379.log"# Persistent data file dirdir /home/data_monitor/redis-3.2. 9/6379"6379.aof"

The slave is configured as follows (for example, 6389)

# instance IP and port bind10.93.21.21Port6389# Master node configuration slaveof10.93.21.21 6379# pid file pidfile redis_6389.pid# log file logfile"/home/data_monitor/redis-3.2.9/log/6389.log"# Persistent data file dirdir/home/data_monitor/redis-3.2.9/data# rdb file address Dbfilename6389. rdb# aof file Address Appendfilename"6389.aof"

Start Redis

Bin/redis-server conf/6379. Confbin/redis-server conf/6389. Confbin/ Redis-server conf/6399. conf

Verify

Master

$ bin/redis-cli-h10.93.21.21-P637910.93.21.21:6379>Inforeplication# replicationrole:masterconnected_slaves:2Slave0:ip=10.93.21.21, port=6389, state=online,offset=571888, lag=1Slave1:ip=10.93.21.21, port=6399, state=online,offset=571888, lag=1Master_repl_offset:571888repl_backlog_active:1repl_backlog_size:1048576Repl_backlog_first_byte_offset:2Repl_backlog_histlen:571887

Slave

$ bin/redis-cli-h10.93.21.21-P638910.93.21.21:6389>Inforeplication# replicationrole:slavemaster_host:10.93.21.21Master_port:6379Master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0Slave_repl_offset:530211slave_priority: -slave_read_only:1connected_slaves:0Master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576Repl_backlog_first_byte_offset:0Repl_backlog_histlen:0

2. Configuring Sentinel Cluster

10.93.21.21:26379

10.93.21.21:26389

10.93.21.21:26399

Start all nodes as long as you monitor the same Redis master and automatically connect to the cluster when you start

# Sentinel registered IP and port bind10.93.21.21Port26379# working Directorydir/home/data_monitor/redis-3.2.9/sentinel# Sentinel monitors which master Node Sentinel monitor MyMaster10.93.21.21 6379 1# How long the master node hangs and decides to hang up and start Failoversentinel down-after-milliseconds MyMaster10000# Failover by several Sentinel execution Sentinel parallel-syncs MyMaster1# Failover How long did not finish, timeout failed sentinel failover-timeout MyMaster180000

Launch Sentinel Cluster

Bin/redis-sentinel conf/s26379.confbin/redis-sentinel conf/s26389.confbin/redis-sentinel conf/ S26399.conf

3, Jedis automatically switch IP and port

Resolve Dependencies First: Pom.xml

<Dependency>    <groupId>Org.springframework.data</groupId>    <Artifactid>Spring-data-redis</Artifactid>    <version>1.0.2.RELEASE</version></Dependency><Dependency>    <groupId>Redis.clients</groupId>    <Artifactid>Jedis</Artifactid>    <version>2.7.0</version>    <type>Jar</type>    <Scope>Compile</Scope></Dependency>    

Connection Pool Code

 Public classJedispoolutil {Private StaticJedissentinelpool pool =NULL;  Public StaticProperties Getjedisproperties () {Properties Config=NewProperties (); InputStream is=NULL; Try{ is= Jedispoolutil.class. getClassLoader (). getResourceAsStream ("Cacheconfig.properties");        Config.load (IS); } Catch(IOException e) {logger.error ("", E); } finally {            if(Is! =NULL) {                Try{is.close (); } Catch(IOException e) {logger.error ("", E); }            }        }        returnconfig; }     /*** Create Connection Pool **/    Private Static voidCreatejedispool () {//establishing connection Pool configuration parametersJedispoolconfig config =NewJedispoolconfig (); Properties prop=getjedisproperties (); //set maximum number of connectionsConfig.setmaxtotal (Stringutil.nulltointeger (Prop.getproperty ("Max_active"))); //set the maximum blocking time, remembering the number of milliseconds millisecondsConfig.setmaxwaitmillis (Stringutil.nulltointeger (Prop.getproperty ("max_wait"))); //set up a spatial connectionConfig.setmaxidle (Stringutil.nulltointeger (Prop.getproperty ("Max_idle"))); //whether the Jedis instance is available        BooleanBorrow = Prop.getproperty ("test_on_borrow") = = "false"?false:true;        Config.settestonborrow (borrow); //Create a connection pool//pool = new Jedispool (config, prop.getproperty ("ADDR"), Stringutil.nulltointeger (Prop.getproperty ("PORT"), Stri Ngutil.nulltointeger (Prop.getproperty ("TIMEOUT" ));//number of threads limit, IP address, port, time-out//Get Redis PasswordString password = stringutil.nulltostring (prop.getproperty ("Password")); String Mastername= "MyMaster"; Set<String> sentinels =NewHashset<string>(); Sentinels.add ("192.168.137.128:26379"); Sentinels.add ("192.168.137.128:26380"); Sentinels.add ("192.168.137.128:26381"); Pool=NewJedissentinelpool (mastername, Sentinels, config); }     /*** Synchronous initialization in multithreaded environments*/    Private Static synchronized voidPoolinit () {if(Pool = =NULL) Createjedispool (); }     /*** Get a Jedis object * *@return     */     Public StaticJedis Getjedis () {if(Pool = =NULL) Poolinit (); returnPool.getresource (); }     /*** Release a connection * *@paramJedis*/     Public Static voidreturnres (Jedis Jedis) {pool.returnresource (Jedis); }     /*** Destroy a connection * *@paramJedis*/     Public Static voidreturnbrokenres (Jedis Jedis) {pool.returnbrokenresource (Jedis); }               Public Static voidMain (string[] args) {Jedis Jedis=Getjedis (); } }

Redis's Sentinel Master-slave switchover (failover) automatically re-connects to the Jedis thread pool

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.