"Redis cluster (installation and use)"

Redis cluster

Why build a cluster

  • Generally, in order to improve website response speed, hot data is always stored in memory instead of directly reading from the back-end database.
  • Redis is a good Cache tool. For large-scale website applications, the amount of hot data is often huge, and it is normal for dozens of gigabytes to hundreds of gigabytes.
  • Redis acts as a cache database. But how to ensure the consistency of data storage, at this time, you need to build a redis cluster. Use a reasonable mechanism to ensure the normal access needs of users.
  • The use of redis cluster can ensure the decentralized storage of data, while ensuring the consistency of data storage. And the high availability mechanism is realized internally. The automatic migration of service failures is realized.

Cluster construction plan

  • Master-slave division:

3台主机 3台从机共6台 端口划分7000-7005

premise

We build in linux, the working directory is

/usr/local/src/redis

Cluster construction

1. Prepare the cluster folder

1.1

mkdir cluster

1.2

在cluster文件夹中分别创建7000-7005文件夹
mkdir 7000 7001 7002 7003 7004 7005

2. Copy the configuration file

Description:

Copy the redis.conf file in the redis root directory to cluster/7000/ and save it under the original name

cp redis.conf cluster/7000/

3. Edit the configuration file

  • 1-Note the local binding IP address
    Insert picture description here
  • 2-Turn off protection mode
    Insert picture description here
  • 3-Modify the port number
    Insert picture description here
  • 4-Start background startup
    Insert picture description here
  • 5-Modify the pid file
    Insert picture description here
  • 6-Modify the persistent file path
    Insert picture description here
  • 7-Set memory optimization strategy
    Insert picture description here
  • 8-Turn off AOF mode
    Insert picture description here
  • 9-Open the cluster configuration
    Insert picture description here
  • 10-Open the cluster configuration file
    Insert picture description here
  • 11-Modify the cluster timeout period
    Insert picture description here

4. Copy the modified configuration file

Description:

Copy the redis.conf files in the 7000 folder to 7001-7005 respectively
[root@localhost cluster]# cp 7000/redis.conf  7001/
[root@localhost cluster]# cp 7000/redis.conf  7002/
[root@localhost cluster]# cp 7000/redis.conf  7003/
[root@localhost cluster]# cp 7000/redis.conf  7004/
[root@localhost cluster]# cp 7000/redis.conf  7005/

5. Batch modification

Description:

Respectively change 7000 in the 7001-7005 file to the name of the corresponding port number, and pay attention to the use of the arrow keys when modifying

Process:

Enter the configuration file through the vim command and enter the following vim command

:%s/7000/700/g

Explain that changing the 7000 of the configuration file to 70001 and the g behind it is all

6. Edit startup/shutdown instructions via script

1-Create a startup script

vim start.sh

Insert picture description here
2-Edit the closed script

vim  shutdown.sh

Insert picture description here
3-Start the redis node

sh start.sh

4-Check whether the redis node is started normally

ps -eg | grep redis

Insert picture description here

7. Create a redis cluster

#5.0版本执行 使用C语言内部管理集群
redis-cli --cluster create --cluster-replicas 1 192.168.35.130:7000 192.168.35.130:7001 192.168.35.130:7002 192.168.35.130:7003 192.168.35.130:7004 192.168.35.130:7005

–Cluster-replicas 1: It represents 1 master and 1 slave

Insert picture description here
Ask us if we want to cluster we enter yes
Insert picture description here

master ---- master
slave ---- slave
0-5460 ---- slots (slots are allocated to each master)
16384 ---- there are 16384 slots in total

Test cluster

Insert picture description here
Command: (Enter after entering redis)

info replication

role-indicates the slave or host-master (host) slave (slave)
port-indicates who the host is or who the slave is


SpringBoot integrates Redis cluster

Edit the properties configuration file

Insert picture description here

#准备6个redis节点
# redis集群的节点
redis.nodes=192.168.126.131:7000,192.168.126.131:7001,192.168.126.131:7002,192.168.126.131:7003,192.168.126.131:7004,192.168.126.131:7005

Edit configuration class

Insert picture description here
Insert picture description here

Get the port number and node address, write the following code
    @Bean   //将方法的返回值结果,交给spring容器进行管理.
    public JedisCluster jedisCluster(){
    
    
        Set<HostAndPort> nodesSet= new HashSet<>();
        String[] nodeArray=nodes.split(",");
        for (String node : nodeArray){
    
    
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            HostAndPort hostAndPort = new HostAndPort(host, port);
            nodesSet.add(hostAndPort);
        }
        return new JedisCluster(nodesSet);
    }

Edit CacheAOP

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45103228/article/details/113790823