RedisCluster installation, deployment, expansion and use of Jedis to call the cluster.md

1. Requirements description

As shown in the figure:
1. To build a Redis5.0 cluster, three masters and three slaves are required, and the installation steps are recorded;
2. One master and one slave (Master4 and Slaver4) can be added and the installation steps
can be recorded; 3. Data can be added to RedisCluster through JedisCluster And fetch the data.

file

2. Steps to build the stand-alone version

2.1 The environment required to install the c language

yum install -y gcc-c++
yum install -y wget 

2.2 Download redis-5.0.5 and unzip, compile and install

## 编译
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -zxf redis-5.0.5.tar.gz
cd redis-5.0.5/src
make 
## 安装,后续直接安装即可,用现成编译好的包
mkdir /usr/redis -p
make install PREFIX=/usr/redis 

2.3 Configure redis, close the protected mode and start in the background

cp redis-5.0.5/redis.conf /usr/redis/bin/ 
vi usr/redis/bin/redis.conf

# 将`daemonize`由`no`改为`yes`
daemonize yes

# 默认绑定的是回环地址,默认不能被其他机器访问
# bind 127.0.0.1

# 是否开启保护模式,由yes该为no
protected-mode no

2.4 Several ways to start redis

# 后端模式启动服务
cd /usr/redis;bin/redis-server bin/redis.conf

# 进入命令行模式
cd /usr/redis;bin/redis-cli -h hostname -p 端口号
cd /usr/redis;bin/redis-cli -h 127.0.0.1 -p 6379 #默认
# 退出命令行模式
exit

# 后端模式关闭服务(推荐)
cd /usr/redis;bin/redis-cli shutdown

# 前端模式启动服务(不推荐)
cd /usr/redis;bin/redis-server

2.4 api method to call

// 1.导入依赖包
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency> 
// 2.示例代码
package com.lagou.redis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

public class JedisDemo {

    @Test
    public void testConn() {
        //与Redis建立连接 IP+port
        Jedis redis = new Jedis("linux121", 6379);
        //在Redis中写字符串 key value
        redis.set("jedis:name:1", "hoult");
        //获得Redis中字符串的值
        System.out.println(redis.get("jedis:name:1"));
        //在Redis中写list
        redis.lpush("jedis:list:1", "1", "2", "3", "4", "5");
        //获得list的长度
        System.out.println(redis.llen("jedis:list:1"));
    }
} 
//输出
hoult
5

3. Three-master and three-slave construction steps

3.1 Cluster planning

Pseudo-cluster mode, simulation with multiple ports on a single machine, that is, three master servers, three slave servers, port 7001-7006, installation locationmkdir -p /root/redis-cluster

3.2 Create an instance and configure the redis.conf file

# 编译安装
cd /opt/hoult/software/redis-5.0.5/src
make install PREFIX=/root/redis-cluster/7001

# 复制redis.conf文件
cp /opt/hoult/software/redis-5.0.5/redis.conf /root/redis-cluster/7001/bin/
vi /root/redis-cluster/7001/bin/redis.conf

# 修改port为7001
port 7001

# 打开cluster-enable yes
cluster-enable yes

# ----------------------------------------------
# 配置后端开启模式
# 将`daemonize`由`no`改为`yes`
daemonize yes

# 默认绑定的是回环地址,默认不能被其他机器访问
# bind 127.0.0.1
# 所有机器可以访问
# bind 0.0.0.0
# 建议注释掉 bind x.x.x.x

# 是否开启保护模式,由yes该为no
protected-mode no 

3.3 Make six copies of 7001, pay attention to modify the port

# 拷贝7001-->7002,7003,7004,7005,7006
cp -r /root/redis-cluster/7001 /root/redis-cluster/7002

# 重新修改:先删除再复制新的redis.conf文件
# 好处:只需要先修改7001的配置文件,再复制,再修改port即可
# rm -rf /root/redis-cluster/7002/bin/redis.conf
# cp /root/redis-cluster/7002/bin/redis.conf /root/redis-cluster/7002/bin/redis.conf
# 修改对应的端口号
vi /root/redis-cluster/7002/bin/redis.conf

3.4 Use the startup script to start all instances

cd /root/redis-cluster/ 
vim start-redis-cluster.sh
 # 启动7001
cd /root/redis-cluster/7001/bin;redis-server redis.conf
# 启动7002
cd /root/redis-cluster/7002/bin;redis-server redis.conf
# 启动7003
cd /root/redis-cluster/7003/bin;redis-server redis.conf
# 启动7004
cd /root/redis-cluster/7004/bin;redis-server redis.conf
# 启动7005
cd /root/redis-cluster/7005/bin;redis-server redis.conf
# 启动7006
cd /root/redis-cluster/7006/bin;redis-server redis.conf
# 启动7007
cd /root/redis-cluster/7007/bin;redis-server redis.conf
# 启动7008
cd /root/redis-cluster/7008/bin;redis-server redis.conf
# 启动
cd /root/redis-cluster/
sh start-redis-cluster.sh

## 登录客户端
/root/redis-cluster/7001/bin/redis-cli -h linux121 -p 7001 

3.5 Use the shutdown script to close all instances


# stop脚本
cd /root/redis-cluster/;
vim stop-redis-cluster.sh

# 关闭7001
cd /root/redis-cluster/7001/bin;redis-cli -h linux121 -p 7001 shutdown
# 关闭7002
cd /root/redis-cluster/7002/bin;redis-cli -h linux121 -p 7002 shutdown
# 关闭7003
cd /root/redis-cluster/7003/bin;redis-cli -h linux121 -p 7003 shutdown
# 启关闭7004
cd /root/redis-cluster/7004/bin;redis-cli -h linux121 -p 7004 shutdown
# 关闭7005
cd /root/redis-cluster/7005/bin;redis-cli -h linux121 -p 7005 shutdown
# 关闭7006
cd /root/redis-cluster/7006/bin;redis-cli -h linux121 -p 7006 shutdown
# 关闭7007
cd /root/redis-cluster/7007/bin;redis-cli -h linux121 -p 7007 shutdown
# 关闭7008
cd /root/redis-cluster/7008/bin;redis-cli -h linux121 -p 7008 shutdown

3.6 Delete the original data of the cluster and create a cluster

# 删除集群遗留任何数据

cd /root/redis-cluster/7001/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7002/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7003/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7004/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7005/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7006/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7007/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
cd /root/redis-cluster/7008/bin/;rm -rf dump.rdb nodes.conf appendonly.aof

# 创建集群
# 注:必须写172.18.0.9(而不是linux121),否则出现:
# 1)ERR Invalid node address specified: linux121:7001;
# 2)jedis-2.9.0:Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool. Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect;
# 3)jedis-2.9.3:redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
cd /root/redis-cluster/7001/bin;./redis-cli --cluster create 172.18.0.9:7001 172.18.0.9:7002 172.18.0.9:7003 172.18.0.9:7004 172.18.0.9:7005 172.18.0.9:7006 --cluster-replicas 1

# 输入yes继续部署

file

3.7 Log in to the client for verification


# 集群模式:-c必不可少
cd /root/redis-cluster/7001/bin/;redis-cli -h 127.0.0.1 -p 7001 -c

file

4. Cluster expansion

Add a master and a slave to the cluster

4.1 Add master node

## 跟前面一样,先复制好redis,配置好端口,但是注意,还是删除数据,再重新启动
cp -r /root/redis-cluster/7001 /root/redis-cluster/7007
vi /root/redis-cluster/7007/bin/redis.conf

# 启动node7007
cd /root/redis-cluster/7007/bin;redis-server redis.conf

# 添加node7007为集群主节点
cd /root/redis-cluster/7007/bin/;redis-cli --cluster add-node 172.18.0.9:7007 172.18.0.9:7001

4.2 Allocate slots for the newly added master node

cd /root/redis-cluster/7007/bin/;redis-cli --cluster reshard 172.18.0.9:7007

# How many slots do you want to move(from 1 to 16384)?
# 输入3000(数值不超过18364)
3000

# 输入分slots的主节点ID(新添加的主节点ID)
04969194dd9b051d16dc41ef8cd4a72a71f03c51
# 通过cluster nodes可查看

# 选择分槽方式
# 选择all
all
# 一直输入yes继续完成
yes

4.3 Add slave node for 7007

# 启动node7008
cd /root/redis-cluster/7008/bin;redis-server redis.conf

# 为新添加的主节点添加从节点
cd /root/redis-cluster/7007/bin/;redis-cli --cluster add-node 172.18.0.9:7008 172.18.0.9:7007 --cluster-slave --cluster-master-id 04969194dd9b051d16dc41ef8cd4a72a71f03c51

5. Use Jedis to access RedisCluster

5.1 Import dependent packages

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.3</version>
</dependency>

5.2 Code demo

package com.hoult.redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;

public class JedisClusterDemo {
    public static void main(String[] args) throws IOException {
        JedisPoolConfig config = new JedisPoolConfig();
        HashSet<HostAndPort> jedisClusterNodes = new HashSet<>();

        //添加集群节点
        jedisClusterNodes.add(new HostAndPort("centos",7001));
        jedisClusterNodes.add(new HostAndPort("centos",7002));
        jedisClusterNodes.add(new HostAndPort("centos",7003));
        jedisClusterNodes.add(new HostAndPort("centos",7004));
        jedisClusterNodes.add(new HostAndPort("centos",7005));
        jedisClusterNodes.add(new HostAndPort("centos",7006));
        jedisClusterNodes.add(new HostAndPort("centos",7007));
        jedisClusterNodes.add(new HostAndPort("centos",7008));

        //获取集群连接
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, config);

        //设置值
        jedisCluster.set("name:001","zhangfei");

        //获取值
        String value = jedisCluster.get("name:001");
        System.out.println(value);

        //关闭连接
        jedisCluster.close();
    }

}

5.3 Results

zhangfei

Wu Xie, Xiao San Ye, a little rookie in the background, big data, and artificial intelligence. Please pay attention to morefile

Guess you like

Origin blog.csdn.net/hu_lichao/article/details/110870321