reids 5.0.4 cluster mode deployment practice.

1, Preparatory work

The redis compressed package of 5.0.4 can be downloaded from the official website.
linux environment

2, Unzip and install

1. Create 6 new folders to install redis directory
The directory path is / root/tools/7001

[root@my 7001]# pwd
/root/tools/7001
[root@my 7001]# ls
redis-5.0.4.tar.gz


2. Unzip and install redis

tar -zxvf redis-5.0.4.tar.gz 
cd redis-5.0.4
make && make install

After executing the test, make test reports the following errors. tcl is not available, so you have to install it

terms of settlement

wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz  
tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/  
cd  /usr/local/tcl8.6.1/unix/  
./configure  
make  
make install

The error of executing make test again will not be solved without affecting the test. It also shows that life is not perfect. Learn to tolerate imperfections.

3. copy these compiled files to another five folders.
Modify profile

# I write the address of this machine as long as it is a machine
bind 127.0.0.1
# Port number
port 7001
# Process file
pidfile "/var/run/redis_7001.pid"
#log file
logfile "/root/tools/7001/redis-5.0.4/access.log"
# data directory
dir "/root/tools/7001/redis-5.0.4/"
# Enable aof persistence
appendonly yes
#Start cluster
cluster-enabled yes
# configuration file
cluster-config-file "nodes-7001.conf"
# Timeout
cluster-node-timeout 15000
#Background start
daemonize yes

The configuration files of several other machines are modified similar to this one.
4. Start redis

[root@my redis-5.0.4]# redis-server redis.conf
[root@my redis-5.0.4]# ps -ef|grep redis
root      94318      1  0 21:44 ?        00:00:00 redis-server 127.0.0.1:7001 [cluster]
root      94330  94276  0 21:44 pts/2    00:00:00 grep --color=auto redis

The rest operate the same way. Start redis.

3, Cluster startup

Here, replica 1 means that there is one replica, that is, there are three master redis, and there is a slave redis under each master redis

redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1

Enter yes to confirm the configuration

IV. test cluster

-c indicates the cluster mode, redis client indicates the client mode enters redis, and - p indicates the port number
That is, enter the redis of 7001

[root@my redis-5.0.4]# redis-cli -p 7001 -c 
127.0.0.1:7001> cluster nodes
a4b7c18d6eccd520a5a186ad8d3dcbdbdac5f92d 127.0.0.1:7002@17002 master - 0 1652883032330 2 connected 5461-10922
63956c905f54e9f17402cf85814b8a43a14b5b8b 127.0.0.1:7003@17003 master - 0 1652883031322 3 connected 10923-16383
208a5250236c36dd898023aa2d8504b0f0de9a4d 127.0.0.1:7004@17004 slave a4b7c18d6eccd520a5a186ad8d3dcbdbdac5f92d 0 1652883030314 4 connected
65eaae02c1ab075f416c259d349032f4d748645e 127.0.0.1:7006@17006 slave 3fd4d7af171cc73f97f8910636081b0e2253d2d1 0 1652883030000 6 connected
87449deef72cdbb210bea73dbe44d8b73874a989 127.0.0.1:7005@17005 slave 63956c905f54e9f17402cf85814b8a43a14b5b8b 0 1652883029000 3 connected
3fd4d7af171cc73f97f8910636081b0e2253d2d1 127.0.0.1:7001@17001 myself,master - 0 1652883028000 1 connected 0-5460

Use cluster nodes to see what other node information is available.

127.0.0.1:7001> get v
-> Redirected to slot [7761] located at 127.0.0.1:7002
(nil)
127.0.0.1:7002> get c
(nil)
127.0.0.1:7002> get aa
-> Redirected to slot [1180] located at 127.0.0.1:7001

You can see that different key values are assigned to different machines, that is, the redis cluster cluster mode is successfully deployed

5, Using springboot to connect to the cluster

Add POM. Redis

        <!--    redis    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. Add redistemplate

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)  {
        Jackson2JsonRedisSerializer serializer=new Jackson2JsonRedisSerializer(Object.class);
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(serializer); //Set key serialization
        template.setValueSerializer(serializer);//Set value serialization
        return template; } }

3.application. Add redis cluster to the properties file

spring.redis.cluster.expire-seconds=120
#Set the node information of redis cluster
spring.redis.cluster.nodes=192.168.31.96:7001,192.168.31.96:7002,192.168.31.96:7003,192.168.31.96:7004,192.168.31.96:7005,192.168.31.96:7006
#Set the execution time of the command. If it exceeds this time, an error will be reported
spring.redis.cluster.command-timeout=5000

4. The first visit timed out,
The following changes have been made
4.1 kill all redis processes
4.2 delete nodes - *. Under folder conf

4.3 start modifying redis bind ip address
4.4 start redis and reconfigure the cluster
redis-cli --cluster create 192.168.31.96:7001 192.168.31.96:7002 192.168.31.96:7003 192.168.31.96:7004 192.168.31.96:7005 192.168.31.96:7006 --cluster-replicas 1
4.5 opening ports

[root@my redis-5.0.4]# /sbin/iptables -I INPUT -p tcp --dport 7002 -j ACCEPT
[root@my redis-5.0.4]# /sbin/iptables -I INPUT -p tcp --dport 7003 -j ACCEPT
[root@my redis-5.0.4]# /sbin/iptables -I INPUT -p tcp --dport 7004 -j ACCEPT
[root@my redis-5.0.4]# /sbin/iptables -I INPUT -p tcp --dport 7005 -j ACCEPT
[root@my redis-5.0.4]# /sbin/iptables -I INPUT -p tcp --dport 7006 -j ACCEPT

  1. redis setting value is used in the code

    6. Use tool access interface

    You can see that the redis value set in the code has been returned.

5, Summary

1. In this test, the cluster configuration cluster mode most commonly used in redis production is tested, and version 5.0 is the stable version officially recommended.
2. Test the springboot connection cluster and use it.
3. There will be a firewall port problem here, which needs attention.

Tags: Java Redis Cache

Posted by sarijit on Thu, 19 May 2022 00:34:17 +0300