Redis5.0 cluster installation (adding and removing master / slave nodes) and spring boot integration

Install redis standalone
Step 1: install the GCC environment required for the C language
yum install -y gcc-c++
yum install -y wget
Step 2: Download and unzip the Redis source code package
cd /root/redis
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -zxf redis-5.0.5.tar.gz
Step 3: compile the Redis source code, enter the redis-5.0.5 directory, and execute the compilation command
cd redis-5.0.5/src
make
Step 4: to install Redis, you need to specify the installation path through PREFIX
mkdir /root/redis -p
make install PREFIX=/root/redis
Step 5: copy the configuration file
cp /root/redis/redis-5.0.5/redis.conf /root/redis/bin/

# Modify profile
port 6379
# Change 'daemon' from 'no' to 'yes'`
daemonize yes
# The default binding is the loopback address, which cannot be accessed by other machines by default
# bind 127.0.0.1
# Whether to turn on the protection mode is determined by yes, and the value is no
protected-mode no
Install Redis cluster
If the password is set, add - a password when executing the command
Step 1: create a folder
mkdir -p /root/redis/redis_cluster
cd /root/redis/redis_cluster
mkdir 700{1..6}
Step 2: copy the installation directory of redis stand-alone to bin 7001:
cp -r /root/redis/bin/ 7001/
Step 3: modify the configuration file
vim 7001/bin/redis.conf
port 7001
cluster-enabled yes   Open the cluster and add the comments#Just remove it
Step 4: copy 7001 to 7002 ~ 7006, and pay attention to port modification.
cp -r 7001/* 7002
cp -r 7001/* 7003
cp -r 7001/* 7004
cp -r 7001/* 7005
cp -r 7001/* 7006
Step 5: create start-cluster.sh and start all instances
# The script is as follows
cd 7001/bin
./redis-server redis.conf
cd ../../7002/bin
./redis-server redis.conf
cd ../../7003/bin
./redis-server redis.conf
cd ../../7004/bin
./redis-server redis.conf
cd ../../7005/bin
./redis-server redis.conf
cd ../../7006/bin
./redis-server redis.conf

# Give scripts write and execute permissions
chmod u+x start-cluster.sh
# Startup script
./start-cluster.sh
View operation
ps -ef | grep redis
give the result as follows
Step 6: create a Redis cluster (no data in Redis when creating)
cd 7001/bin/
# create: creates a cluster
# --Cluster replicas 1: each host has at least one slave.
./redis-cli --cluster create --cluster-replicas 1 192.168.77.100:7001 192.168.77.100:7002 192.168.77.100:7003 192.168.77.100:7004 192.168.77.100:7005 192.168.77.100:7006
The results are as follows:
Connection cluster
# -c indicates that it is connected in redis cluster mode
./redis-cli -h 127.0.0.1 -p 7001 -c
View cluster information
Add new master and slave nodes to the Redis cluster
cd /root/redis/redis_cluster
mkdir 7007 7008
# Before copying, it is necessary to ensure that the original installed stand-alone version of redis (no data is stored)
cp -r /root/redis/bin 7007/
cp -r /root/redis/bin 7008/
Modify configuration
vim 7007/bin/redis.conf
port 7007
cluster-enabled yes   Open the cluster and add the comments#Just remove it
Then start 7007 and 7008
./redis-server redis.conf
Add master node
cd 7001/bin/
./redis-cli --cluster add-node 192.168.77.100:7007 192.168.77.100:7001
# 192.168.77.100:7007 new master node to be added to the cluster
# 192.168.77.100:7001 any node in the original cluster
The results are as follows:
View cluster nodes
./redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
The result is as follows: no slot has been allocated at present
Assign a slot to the new node, and the ip:port is any node in the current cluster
./redis-cli --cluster reshard 192.168.77.100:7001

# How many slots are allocated? Fill in according to the situation: 4000
# node ID? ID of 7007: 3d9e59debe81b17ee0c82fbe50a4295941c1
# Source node #1: all
# Continue with reshard plan: yes
Continue with reshard plan: yes
The results are as follows:
Check the cluster node again. 7007 has allocated slots
Add slave node
./redis-cli --cluster add-node 192.168.77.100:7008 192.168.77.100:7007
give the result as follows
View cluster nodes
./redis-cli -p 7001 -c
127.0.0.1:7001> cluster nodes
The results are as follows:
Client connecting the new node (7008)
./redis-cli -p 7008 -c
The slave node specified as 192.168.77.100:7007 node
cluster replicate 3d9e59debe81b17eeeeee0c82fbe50a4295941c1
The results are as follows:
Delete slave node
./redis-cli --cluster del-node 192.168.77.100:7008 6aae16dd69d4c7cfb0cfae03c4f0170ca1836ec4
The results are as follows:
Delete master node
To delete a master node, first assign the slot on the master node to a master node, which is placed in the 192.168.77.100:7001 master node.
./redis-cli --cluster reshard 192.168.77.100:7001
# How many slots need to be removed, where slot = slot occupied by the node to be deleted: 4000
# To which node, the value is the node id of node id: 7001
# Source node #1: Node id of 7007
# Source node #2: Enter done to execute the build plan
# Continue with reshard plan: yes
The results are as follows:
Last safe delete
./redis-cli --cluster del-node 192.168.77.100:7007 3d9e59debe81b17eeeeee0c82fbe50a4295941c1
The results are as follows:
After deleting the node, check the redis process and find that 7007 and 7008 have stopped
 
  SpringBoot integrates RedisCluster
  Import dependency
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

Add configuration

server:
  port: 8888
spring:
  redis:
    cluster:
      nodes: 192.168.77.100:7001,192.168.77.100:7002,192.168.77.100:7003,192.168.77.100:7004,192.168.77.100:7005,192.168.77.100:7006,192.168.77.100:7007,192.168.77.100:7008
    password:
    jedis:
      pool:
        max-active: 20
        max-wait: -1
        max-idle: 200
        min-idle: 20
    timeout: 10000

Jedis configuration

package com.lagou.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: RedisClusterConfig
 * @Description:
 * @Author: qjc
 * @Date: 2021/11/26 3:41 afternoon
 */
@Configuration
public class RedisClusterConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private long maxWait;

    @Bean
    public JedisCluster getJedisCluster() {
        return new JedisCluster(getNodes(), timeout, poolConfig());
    }

    private JedisPoolConfig poolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxTotal(maxActive);
        config.setMaxWaitMillis(maxWait);
        return config;
    }

    private Set<HostAndPort> getNodes() {
        String[] cNodes = clusterNodes.split(",");
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        // Partition cluster nodes
        String[] hp;
        for (String node : cNodes) {
            hp = node.split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        return nodes;
    }
}

test

package com.lagou;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

/**
 * @ClassName: RedisClusterWorkApplicationTest
 * @Description:
 * @Author: qjc
 * @Date: 2021/11/26 3:44 afternoon
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RedisClusterWorkApplication.class)
public class RedisClusterWorkApplicationTest {

    @Autowired
    private JedisCluster jedisCluster;

    @Test
    public void testRedisCluster() {
        String result = jedisCluster.set("name", "zhangfei");
        System.out.println("Insert result:" + result);
        String name = jedisCluster.get("name");
        System.out.println("Query results:" + name);
    }

}

Keywords: Cache

Added by Sorrow on Wed, 01 Dec 2021 17:45:16 +0200