Redis Master-Slave Mode and Sentinel Mode

1. Hardware environment

Assume there are four machines, IP and hostname are as follows:

192.168.100.105 c1
192.168.100.110 c2
192.168.100.115 c3
192.168.100.120 c4

 

2. Software environment

Operating System: Ubuntu Server 18.04

 

3. Install Single Machine Edition

1. Download the source code package

https://redis.io/

Download redis-6.2.6.tar

 

2. Upload and Unzip

Single-machine version is tested first with c1 (192.168.100.105), assuming it is in / home / directory

cd /home
rz
tar -xvf redis-6.2.6.tar
mv redis-6.2.6 redis

 

3. Install compile and run dependent components

Since this article uses source code for installation and deployment, it needs to be compiled before it can run

apt install make
apt install make-guile
apt install pkg-config

 

4. Compile

cd redis
make

The compiled run files are in / redis/src / directory

 

5. Run

cd src
./redis-server

Background runs only need to be followed by running commands with &, that is. / Redis-server &

 

6. Testing

./redis-cli

Enter exit to exit redis-cli command tool

 

7. Stop

If it's running in the foreground, just ctrl+c to exit

If running in the background, run the following command:

./redis-cli shutdown

 

 

4. Master-Subordinate Mode

1. Upload Installation

Each machine executes point 1-4 of point 3, that is, until the compilation step

 

2. Configuration

Assume that c1 is the host machine and the other three are slave machines.

(1) Edit redis.conf

cd /home/redis
vim redis.conf

 

(2) Edit binding IP

*This step is performed on each machine

Find bind 127.0.0.1 -::1, change this line to bind 0.0.0.0

 

(3) Configure the IP and port of the host

*This step only needs to be executed from the slave machine, i.e. c2, c3, c4 machines

At redis. Add a line to conf

replicaof 192.168.100.105 6379

*The new version is replicaof, the old version is slaveof

If OCD, you can set the slave to read-only at redis. Add a line to conf:

replica-read-only yes

*The new version is replica-read-only, the old version is slave-read-only

 

3. Run

Run Redis for c1 first, then the other three.

cd /home/redis
src/redis-server redis.conf

Background runs need only be followed by a command, src/redis-server redis. Conf &

 

4. Testing

Execute the following commands on each machine:

src/redis-cli info replication

 

 

5. Sentry Mode

*The following steps need to be performed on each machine

1. Edit sentinel.conf

cd /home/redis
vim sentinel.conf

(1) Turn off protection mode

Anti-comment # protected-mode no, which means deleting the'#'sign, refers to keeping protected-mode no

 

(2) Configure the host for monitoring

sentinel monitor mymaster 192.168.100.105 6379 2

Where mymaster is the proxy name of the host (for the convenience of configuring other properties), you can customize it to another name, such as clothomaster

 

(3) Configure wait time for switching after the host is disconnected

This property refers to how long the sentinel will automatically switch other slaves to be hosts when it finds that the host is disconnected. Default is 30 seconds, change to 10 seconds here

sentinel down-after-milliseconds mymaster 10000

 

2. Run

cd /home/redis
src/redis-sentinel sentinel.conf

Background runs only need to be followed by the run command by &, which is src/redis-sentinel sentinel. Conf &

 

3. Testing

src/redis-cli -h 192.168.100.105 -p 26379 sentinel sentinels mymaster

Where mymaster corresponds to sentinel. The host generation name in the conf file, if any other name is customized, is also changed here

 

4. Code testing

(1) Introducing dependent packages

In pom. Add the following configurations to xml's <dependencies></dependencies>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>

Since JedisSentinelPool uses commons-pool2, if not referenced, errors will occur when SpringBoot runs.

 

(2) Code access Redis

Operating Redis in Sentry mode does not bind only the host IP, but all Sentry Entrances.

public class RedisHandler
{
    public static void main(String[] args)
    {
        try
        {
            String masterName = "mymaster";
            
            HashSet<String> sentinels = new HashSet<>(Arrays.asList(
                    "192.168.100.105:26379",
                    "192.168.100.110:26379",
                    "192.168.100.115:26379",
                    "192.168.100.120:26379"
            ));
            
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(50);
            config.setMaxIdle(10);
            config.setMinIdle(5);

            JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config);
            Jedis connection = jedisSentinelPool.getResource();
            Set<String> keySet = connection.keys("*");
            for (String key : keySet)
            {
                System.out.println(key);
            }

            jedisSentinelPool.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

Keywords: Java

Added by AmandaF on Tue, 11 Jan 2022 19:16:55 +0200