Redis Sentinel High-availability configuration and C # access

Source: Internet
Author: User
Tags failover

The environment of this article is as follows:

Operating system: UBUNTU-14.04.1-DESKTOP-AMD64

redis:2.8.19

If you use a virtual machine, set each network as a bridge, or they can connect to each other, and the LAN is not connected.

System Design

I. Installation of Redis

You can refer to the previous article, take the offline installation, this time sentinel configuration is heavy, so take a networked installation as an example.

Exhale the Ubuntu terminal and execute the following command in turn:

wget http://download.redis.io/releases/redis-2.8.19.tar.gztar xzf redis-2.8.  + . TAR.GZCD Redis-2.8.  +  Make

Can be installed successfully, 4 machines followed by the same steps to install.

Second, configure the master-slave relationship (principal relationship)

This section has nothing to do with DATA4.

such as Data1 for Master machine, then do not have to do any configuration, direct start on the line, start the command and the following, skip.

Enter the DATA2, Data3 Redis-2.8.19 directory, copy and paste redis.conf file original directory, renamed to Redis10.conf, Redis11.conf (just back it up for fear of bad configuration), then open this configuration file to find the master-slave configuration, configured as follows:

192.168. 1.9 6379

Note that the previous # is removed.

Then start the 3 services in turn, open a new terminal, start the command such as:

CD redis-2.8.  + src/redis-server redis.conf

Other machines are:

Src/redis-server redis10.conf

Then open a new terminal on master and deposit the Key/value data:

CD redis-2.8.  + src/redis-cliset  name Zhangsanget name

If you can see "Zhangsan" then succeed, then turn on the terminal on each slave machine to type:

Get Name

should also be able to see "Zhangsan"

Conversely, if you type

Set name Lisi

The error will be.

The reason is: Master-slave mode only the master can read and write, and then distributed to the slave machine, slave can only read, can not write. This allows for read-write separation and backup.

Third, configure Sentinel

DATA4 has not yet moved, into the redis-2.8.19 directory, a new text file, named sentinelmaster.conf, the content is (only these lines, no other):

6379192.168. 9.18 6379 1 Sentinel Down  the Sentinel Failover 900000 Sentinel can-failover master1 yessentinel parallel2

The above configuration instructions are as follows:

Monitor: Indicates which master host is monitored.

Down-after-milliseconds: is when a Sentinel fails to reach an instance in milliseconds (or if the instance does not reply to pings or responds incorrectly) it considers the time value required for the instance to be closed.

Can-failover is used to determine whether a sentinel initiates a failover when the instance is in the objectively state. You can configure all Sentinels to perform failover, and if necessary, you can also configure to use only a few sentinels to reach an agreement, while others are actually responsible for performing the failover.

The PARALLEL-SYNCS option specifies the maximum number of times a failover can be synchronized from the server to the new primary server at the same time, and the smaller the number, the longer it takes to complete a failover, but the larger it means that the more from the server is unavailable because of replication. You can ensure that only one from the server is in a state that cannot process a command request at a time by setting this value.

Then launch Sentinel:

CD redis-2.8.  + src/redis-sentinel sentinelmaster.conf

Then open the log file to see that the host has been connected and has obtained the slave information.

Four, analog failure transfer:

To open a new terminal on DATA1, type:

CD redis-2.8.  + src/redis-clishutdown

Check the service startup terminal or the last line of the log should be bye bye description of the success of the downline.

Then, on the Data2 and DATA3 respectively write and read out a key/value test which becomes the new host, and then go to the DATA4 log to the control.

When DATA1 is restarted, it becomes a new slave and is no longer master.

V. Use C # to connect to this "cluster"

This example uses Csredis, a client that has wasted a lot of time looking for new full-featured clients (at least Sentinel) since the commercialization of an open source client.

The idea is to first connect Sentinel to get the online service address and port number, and then read-only operation connection slave, add and delete changes to the Operation connection master. The code for obtaining sentinel information is as follows:

using(varSentinel =NewRedissentinelclient ("192.168.1.12",6379)) {     varmaster = Sentinel. Slaves ("Master1");//This is the name on Sentinel above for the master host, to be consistent    foreach(varIteminchMaster) {Console.WriteLine (item. Masterhost+":"+item. Masterport);//Get master Machine informationConsole.WriteLine (item. ip+":"+item. Port);//get slave machine informationConsole.WriteLine (); } } 

How to connect no need to say Ah, above all by memory + Baidu Knock out, open a machine and start these services is really troublesome, if there is no step to go past please private messages me.

Redis Sentinel High-availability configuration and C # access

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.