centos7 64bit redis 单机 与 redis cluster 集群 配置 ,在阿里云ecs部署, 并本地连接


----------------------------------------------------------------
========
centos7 64bit 安装 redis 
========
博文地址
https://www.cnblogs.com/zuidongfeng/p/8032505.html

#查看本机是否安装curl工具
[root@iZuf6hyvanq21thxm114s6Z ~]# curl -h
#查看本机 curl 工具的安装位置
[root@iZuf6hyvanq21thxm114s6Z ~]# which curl
/usr/bin/curl
#新建 一个 redis 目录存放 redis 文件
mkdir /usr/local/redis
# 下载 url 文件到  /usr/local/redis/redis-5.0.5.tar.gz 
curl -o /usr/local/redis/redis-5.0.5.tar.gz http://download.redis.io/releases/redis-5.0.5.tar.gz
# #tar 命令 解压到当前目录 z 通过gzip解压 x 拉出 v 冗长 f 定位归档文件
tar -zxvf redis-5.0.5.tar.gz
# 查看本机 是否安装 gcc
[root@iZuf6hyvanq21thxm114s6Z ~]# which gcc
# 定位到 redis 的 解压目录
cd /usr/local/redis/redis-5.0.5
# 编译安装
make MALLOC=libc
# 将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin目录
cd src && make install
# 备注 将 redis 的 redis.conf 的 bind 127.0.0.1 注解掉,标识 任意IP地址都能连当前redis server服务 【重】
vi /usr/local/redis/redis-5.0.5/redis.conf
# 将 redis.conf 里的 logs 设置为 /usr/local/redis/redis-5.0.5/logs/redis_6379.log
# 这里先测试的是 6379 作为单机单redis实例测试用的,所以 在 redis.conf 中 cluster-enabled yes 设置为 cluster-enabled no 【重】
vi /usr/local/redis/redis-5.0.5/redis.conf
# redis.conf 配置文件需要几个注意的地方如下
--------------
# 将这行注解掉, bind 即 允许指定ip与当前redis server 建立连接,若注解bind 则 允许网络任意ip与当前redis server建立连接
bind 127.0.0.1 
# 即 当前redis server 监听的端口号,如果是0则 redis server 将不会监听
port 6379 
# 客户端与redis server 连接口空闲N秒后关闭 两者之间的连接, 0 即 标识禁用
timeout 0
# 设置成 yes 即 redis将以后台守护进程模式启动,并会在 /var/run/reds.pin 中编写一个 pid 文件(见下面 pidfile 字段)
daemonize yes
# redis server 以后端模式启动时 编写的pid文件的位置
pidfile /var/run/redis_6379.pid
# redis启动时的正常日志文件,备注: redis_6379 文件是启动时新建的,但是 这个文件的文件夹 以及 父文件夹 必须在启动前创建好,否则启动报错
# redis 启动与否 都可以到 这个日志文件中来进行查看
logfile "/usr/local/redis/redis-5.0.5/logs/redis_6379.log"
# redis client 与 redis server 进行连接时的密码
requirepass yuchao#redis
# 因为现在设置的是 单机 单redis实例, 并非多机多redis实例(即集群),所以 单机单redis实例时这里设置no,集群时设置yes
cluster-enabled no
# 是否有启动的
ps -ef | grep redis
# 没启动时, 启 redis server 
/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis.conf
# 登client 并关 (备注: 47.103.137.122 是阿里云公网ip 即 外网ip)
[root@iZuf6hyvanq21thxm114s6Z logs]# /usr/local/redis/redis-5.0.5/src/redis-cli -a yuchao#redis -h 47.103.137.122 -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
47.103.137.122:6379> get name
"yuchao"
# 有时启动前面 可能 还要删下面的几个 
rm -rf /usr/local/redis/redis-5.0.5/dump.md
rm -rf /usr/local/redis/redis-5.0.5/nodes.conf
rm -rf /var/run/redis_6379.pid
# 如果启动失败 查看一下 redis 启动日志
view /usr/local/redis/redis-5.0.5/logs/redis_6379.log

========
centos7 64bit 下 redis 的 三种启动方式 
========
###############
redis 启动方式一
###############
# 进 src 目录 
[root@iZuf6hyvanq21thxm114s6Z src]# ./redis-server
12077:C 09 Jun 2019 00:23:55.507 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12077:C 09 Jun 2019 00:23:55.507 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12077, just started
12077:C 09 Jun 2019 00:23:55.507 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 12077
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

12077:M 09 Jun 2019 00:23:55.508 # Server initialized
12077:M 09 Jun 2019 00:23:55.508 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
12077:M 09 Jun 2019 00:23:55.508 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
12077:M 09 Jun 2019 00:23:55.508 * Ready to accept connections
备注: 非后台守护进程模式启动方式时,此 窗口 需一直打开
###############
redis 启动方式二
###############
# 修改 redis 配置文件 为 守护线程 即 daemonize no 的 no 设置为 yes 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# vi redis.conf  
# 然后 在 执行 启动可执行文件时 传入 redis.conf 的绝对路径
[root@iZuf6hyvanq21thxm114s6Z src]# ./redis-server /usr/local/redis/redis-5.0.5/redis.conf
12411:C 09 Jun 2019 00:30:19.745 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12411:C 09 Jun 2019 00:30:19.745 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12411, just started
12411:C 09 Jun 2019 00:30:19.745 # Configuration loaded
# 查看 redis 进程
[root@iZuf6hyvanq21thxm114s6Z src]# ps -ef | grep redis
root     12412     1  0 00:30 ?        00:00:00 ./redis-server 127.0.0.1:6379
# kill redis 
kill -9 12412

###############
redis 启动方式三
###############
# 设置 redis 为开机启动 (备注: 至于在指定目录下 新建 指定名称的文件 可以 细看 /usr/local/redis/redis-5.0.5/utils/redis_init_script 文件)
# /etc 目录下新建 redis 目录 
mkdir /etc/redis
# 将 /usr/local/redis/redis-5.0.5/redis.conf 文件拷贝并重命名到 /etc/redis/6379.conf 
cp /usr/local/redis/redis-5.0.5/redis.conf /etc/redis/6379.conf
# 将 redis 启动脚本  拷贝并重命名到 /etc/ini.d/redisd
cp /usr/local/redis/redis-5.0.5/utils/redis_init_script /etc/init.d/redisd
# 验证一下 启动项
[root@iZuf6hyvanq21thxm114s6Z init.d]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed
# 解决上面错误的思路,博文地址
# https://www.jianshu.com/p/65a27254f2b8
# 删除 下术文件
[root@iZuf6hyvanq21thxm114s6Z init.d]# rm -rf /var/run/redis_6379.pid
# 借助启动项启动测试 
[root@iZuf6hyvanq21thxm114s6Z init.d]# service redisd start
Starting Redis server...
13276:C 09 Jun 2019 00:46:36.242 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13276:C 09 Jun 2019 00:46:36.242 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=13276, just started
13276:C 09 Jun 2019 00:46:36.242 # Configuration loaded
[root@iZuf6hyvanq21thxm114s6Z init.d]# 
# 添加到启动项目
[root@iZuf6hyvanq21thxm114s6Z init.d]# chkconfig --add redisd
# 重新启动后 查看 redis进程(不用自己去 start redis )
#linux 下的 init脚本 执行 init.d 目录下的可执行文件时 会默认传 start 参数到 里面去 
博文地址
https://www.centos.bz/2016/10/adding-nginx-as-a-system-service/
init脚本(也称为服务启动脚本或甚至sysv脚本)是一个符合某种标准的shell脚本。 
该脚本通过诸如开始,停止和其他等命令来控制守护进程应用程序。
首先,当计算机启动时,init守护程序将使用start参数运行脚本。 另一种可能性是通过从shell手动执行脚本
========
centos7 64bit 下 redis 的 关闭
========
######################
#正常停止redis server 
######################
# 正常停止redis server 借助  redis-cli shutdown
[root@iZuf6hyvanq21thxm114s6Z src]# /usr/local/redis/redis-5.0.5/src/redis-cli -p 6379 shutdown
[root@iZuf6hyvanq21thxm114s6Z src]# ps -ef | grep redis
root     13739  3722  0 00:55 pts/0    00:00:00 grep --color=auto redis

 redis-cli -p 6379 shutdown

######################
#非正常停止redis server 
######################
# 找到线程号,然后 kill -9 的形式杀死它 
========
将 redis 的可执行 文件放到 当前用户的 PATH 目录下去
========
#定位当前登录用户 自己的 home 目录
[root@iZuf6hyvanq21thxm114s6Z etc]# cd ~
# 当前目录
[root@iZuf6hyvanq21thxm114s6Z ~]# pwd
/root
# 查看当前目录文件
[root@iZuf6hyvanq21thxm114s6Z ~]# ll -a
total 68
dr-xr-x---.  6 root root  4096 Jun  8 15:49 .
dr-xr-xr-x. 19 root root  4096 Jun  9 00:57 ..
-rw-------   1 root root 12407 Jun  8 21:40 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--   1 root root   391 Jun  8 15:49 .bash_profile
-rw-r--r--.  1 root root   176 Dec 29  2013 .bashrc
drwxr-xr-x   3 root root  4096 Dec 12 15:59 .cache
-rw-r--r--.  1 root root   100 Dec 29  2013 .cshrc
drwxr-xr-x   2 root root  4096 Dec 12 15:59 .pip
drwxr-----   3 root root  4096 May 28 10:23 .pki
-rw-r--r--   1 root root   205 Jan 31 14:56 .pydistutils.cfg
drwx------   2 root root  4096 Jan 31 14:56 .ssh
-rw-r--r--.  1 root root   129 Dec 29  2013 .tcshrc
-rw-------   1 root root  2428 May 29 11:33 .viminfo
# 对 .bash_profile 文件添加如下两行
#备注:  当前 redis 的安装目录 /usr/local/redis/redis-5.0.5
#查看 .bash_profile 文件内容
#########################################
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

#zookeeper安装目录
ZOOKEEPER_HOME=/usr/local/zookeeper/apache-zookeeper-3.5.5-bin
#放到 PATH中去,目录分隔符是 :
PATH=$ZOOKEEPER_HOME/bin:$PATH
# 扩大PATH变量的作用域到 父shell
export PATH
#########################################
将上面的文件设置成下面的样子(备注:ZOOKEEPER_HOME是本人安装的目录,所以别人不用必关心这个)
#########################################
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

#zookeeper安装目录
ZOOKEEPER_HOME=/usr/local/zookeeper/apache-zookeeper-3.5.5-bin
#放到 PATH中去,目录分隔符是 :
PATH=$ZOOKEEPER_HOME/bin:$PATH
# 扩大PATH变量的作用域到 父shell
export PATH

# redis 安装 home 目录
REDIS_HOME=/usr/local/redis/redis-5.0.5
PATH=$REDIS_HOME/src:$PATH
export PATH
#########################################
#用 source 或 . 执行shell脚本,将shell脚本中的变量 导出到当前shell中来
# 第一个. 是命令 第二个点是当前目录,两个点之间有空格
[root@iZuf6hyvanq21thxm114s6Z ~]# source .bash_profile
# 查看当前SHELL的环境变量中 是否有 redis 的 src 目录
[root@iZuf6hyvanq21thxm114s6Z ~]# echo $PATH
/usr/local/redis/redis-5.0.5/src:/usr/local/zookeeper/apache-zookeeper-3.5.5-bin/bin:/usr/local/zookeeper/apache-zookeeper-3.5.5-bin/bin:/usr/java/jdk-11.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
# 尝试在 非 redis 的 src 目录下 执行 redis 可执行文件的命令
# redis-cli -h {hostIp} -p {port} 连接 redis server 服务 ,设置密码时 要 redis-cli -a {password} -h {hostIp} -p {port}  
[root@iZuf6hyvanq21thxm114s6Z /]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set name yuchao 
OK
127.0.0.1:6379> get name
"yuchao"
127.0.0.1:6379> exit
[root@iZuf6hyvanq21thxm114s6Z /]# 
# 具体的一些 命令 参见 下面 redis 命令整理
========
redis 密码设置 的 两种方式
========
博文地址
https://blog.csdn.net/qq_26440803/article/details/82967433

######################
#初始化 redis 密码 【备注: 这种方式需要重新启动】
######################
# 到 redis 安装的home 目录下
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# cd /usr/local/redis/redis-5.0.5
# 当前目录与子孙目录下的 conf 文件
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# find ./ -name "*conf*"
./redis.conf
./deps/lua/src/luaconf.h
./deps/jemalloc/configure
./deps/jemalloc/build-aux/config.guess
./deps/jemalloc/build-aux/config.sub
./deps/jemalloc/config.stamp.in
./deps/jemalloc/bin/jemalloc-config.in
./deps/jemalloc/configure.ac
./src/config.c
./src/config.h
./src/config.o
./sentinel.conf
./tests/sentinel/tests/01-conf-update.tcl
./tests/sentinel/tests/03-runtime-reconf.tcl
./tests/sentinel/tests/02-slaves-reconf.tcl
./tests/assets/default.conf
# 打开 ./redis.conf 配置文件 并找到 # requirepass foobared 并设置成 requirepass yuchao#redis 并保存文件
# 通过客户端 正常关闭 redis server 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# redis-cli shutdown
# 查看是否还有 redis 线程 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     21237  3722  0 14:28 pts/0    00:00:00 grep --color=auto redis
# 启动 redis server 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# /usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis.conf
21282:C 09 Jun 2019 14:29:38.378 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21282:C 09 Jun 2019 14:29:38.378 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=21282, just started
21282:C 09 Jun 2019 14:29:38.378 # Configuration loaded
# 查看 是否有 redis 进程
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     21283     1  0 14:29 ?        00:00:00 redis-server 127.0.0.1:6379
# redis 客户端 与 redis server 建立连接 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# redis-cli -h 127.0.0.1  -p 6379 
127.0.0.1:6379> get name
(error) NOAUTH Authentication required.
# 在连接时 输入密码 
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# redis-cli -h 127.0.0.1  -p 6379 -a yuchao#redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get name
"yuchao"
# 先建立连接 然后 在 命令行 输入密码
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# redis-cli -h 127.0.0.1  -p 6379
127.0.0.1:6379> auth yuchao#redis
OK
######################
#初始化 redis 密码  【redis-server 重启后会失效】
######################
# redis client 与 redis server 建立连接
127.0.0.1:6379> config set requirepass yuchao#redis
OK
127.0.0.1:6379> auth yuchao#redis
OK
127.0.0.1:6379> config get requirepass 
1) "requirepass"
2) "yuchao#redis"
========
centos 7 开启 6379 端口号
========
#centos 7 开启 6379 端口号
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=6379/tcp
success
--zone=public:表示作用域为公共的;
--add-port=8080/tcp:添加tcp协议的端口8080;
--permanent:永久生效,如果没有此参数,则只能维持当前服务生命周期内,重新启动后失效;
#备注: 开端口号之后记得重新启动防火墙
#重启防火墙
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --reload
success
#查看已开放得所有端口
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --list-ports
80/tcp 8080/tcp 6379/tcp
备注: 如果是 想要连接 阿里云 redis server 时,必须 在 阿里云ecs实例 的 安全规则 添加 对 6379 端口的放行

========
java客户端代码如下
========

    public void testRedisStandalone() {
        logger.info(" 类 testRedisStandalone 方法 开始====");
        try {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            // 最大连接数
            poolConfig.setMaxTotal(2);
            // 最大空闲数
            poolConfig.setMaxIdle(2);
            // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
            // Could not get a resource from the pool 
            // 47.103.137.122 是 阿里云公网ip 即 外网ip
            poolConfig.setMaxWaitMillis(1000);
            JedisPool pool = new JedisPool(poolConfig, "47.103.137.122", 6379, 0, "redis server 配置redis.conf里requirepass的值 即 密码");
            Jedis jedis = null;
            try {
                for (int i = 0; i < 5; i++) {
                    jedis = pool.getResource();
                    jedis.set("foo" + i, "bar" + i);
                    System.out.println("第" + (i + 1) + "个连接, 得到的值为" + jedis.get("foo" + i));
                    // 用完一定要释放连接
                    jedis.close();
                }
            } finally {
                pool.close();
            }
        } catch (Exception e) {
            logger.info("\n 类 testRedisStandalone 方法 异常信息如下: \n",e);
        }
        logger.info(" 类 testRedisStandalone 方法 结束====\n");
    }

执行日志输出如下
2019-06-15 15:39:53,038 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.standalone.test.RedisStandaloneTest] -  类 testRedisStandalone 方法 开始====
  第1个连接, 得到的值为bar0
第2个连接, 得到的值为bar1
第3个连接, 得到的值为bar2
第4个连接, 得到的值为bar3
第5个连接, 得到的值为bar4
2019-06-15 15:39:53,250 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.standalone.test.RedisStandaloneTest] -  类 testRedisStandalone 方法 结束====


========
centos7 redis 在单个机器上 布  redis集群
========
博文地址
https://blog.csdn.net/zh15732621679/article/details/78507579
# 新建文件夹 redis-cluster
[root@iZuf6hyvanq21thxm114s6Z redis]# cd /usr/local/redis
[root@iZuf6hyvanq21thxm114s6Z redis]# mkdir redis-cluster
# 拷贝单机版 的redis 到 新建目录下(备注目标目录有/号), 备注: 拷贝文件夹时是 迭代拷贝
cp /usr/local/redis/redis-5.0.5 /usr/local/redis/redis-cluster/ -r
# 定位到 redis-cluster
cd /usr/local/redis/redis-cluster
# 更名为 redis01,删除 dump.rdb,
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# mv redis-5.0.5 redis01
#修改 /usr/local/redis/redis-cluster/redis01/redis.conf 三个地方 有如下几个地方
------------------------
# 将这行注解掉, bind 即 允许指定ip与当前redis server 建立连接,若注解bind 则 允许网络任意ip与当前redis server建立连接
bind 127.0.0.1 
# 即 当前redis server 监听的端口号,如果是0则 redis server 将不会监听
port 7001 
# 客户端与redis server 连接口空闲N秒后关闭 两者之间的连接, 0 即 标识禁用
timeout 0
# 设置成 yes 即 redis将以后台守护进程模式启动,并会在 /var/run/reds.pin 中编写一个 pid 文件(见下面 pidfile 字段)
daemonize yes
# redis server 以后端模式启动时 编写的pid文件的位置
pidfile /var/run/redis_7001.pid
# redis启动时的正常日志文件,备注: redis_6379 文件是启动时新建的,但是 这个文件的文件夹 以及 父文件夹 必须在启动前创建好,否则启动报错
# redis 启动与否 都可以到 这个日志文件中来进行查看
logfile "/usr/local/redis/redis-cluster/logs/redis_7001.log"
# redis client 与 redis server 进行连接时的密码
requirepass yuchao#redis
#单机 单redis实例 设置no,多机多redis实例即集群时设置yes
cluster-enabled yes
# 非保护模式
protected-mode no
# 集群时设置此值与 requirepass 的值相同,因为主从同步时是需要鉴权的
masterauth yuchao#redis
# 集群时设置此值为yes
cluster-enabled yes
# 集群中每个redis实例都有一个配置文件,且这个文件是自动创建的
cluster-config-file nodes-7001.conf
------------------------
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# rm ./redis01/dump.rdb 
rm: remove regular file ‘./redis01/dump.rdb’? y
# 拷贝5个相同的 redis01 并更名为 redis02 redis03 redis04 redis05 redis06,并修 port 分别为 7002 7003 7004 7005 7006
# 备注 将 redis01下 redis.conf 文件中所有的 7001 都替换成 7002  7003  7004  7005  7006
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cd /usr/local/redis/redis-cluster 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cp -r redis01  redis02 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cp -r redis01  redis03
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cp -r redis01  redis04
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cp -r redis01  redis05
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cp -r redis01  redis06
# 创建 shell 脚本启动 上述 所有 redis 实例 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cd /usr/local/redis/redis-cluster
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# touch start-all.sh
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# chmod 744 start-all.sh 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# vi start-all.sh 
# start-all.sh 内容如下
-------------------------
echo start
cd redis01
./src/redis-server redis.conf
cd ..


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

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

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

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

cd redis06
./src/redis-server redis.conf
cd ..
echo start
-------------------------
# 在写一个 关闭的脚本 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# touch stop-all.sh
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# chmod 744 stop-all.sh 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# vi stop-all.sh 
# stop-all.sh 内容如下 备注: -a {密码} -h {内网ip} -p {端口号}  shutdown 
# 备注 -h 是 内网ip 
-------------------------
echo start
cd redis01
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7001 shutdown
cd ..

cd redis02
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7002 shutdown
cd ..

cd redis03
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7003 shutdown
cd ..

cd redis04
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7004 shutdown
cd ..

cd redis05
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7005 shutdown
cd ..

cd redis06
./src/redis-cli -a yuchao#redis -h 172.18.10.245 -p 7006 shutdown
cd ..
echo end
-------------------------

-------------------------
# 创建 shell 脚本清理集群构建失败时的配置文件 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# cd /usr/local/redis/redis-cluster
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# touch clear-all.sh
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# chmod 744 clear-all.sh 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# vi clear-all.sh 
# clear-all.sh 内容如下
-------------------------
cd redis01
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7001.conf

cd ..


cd redis02
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7002.conf
cd ..

cd redis03
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7003.conf
cd ..

cd redis04
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7004.conf
cd ..

cd redis05
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7005.conf
cd ..

cd redis06
rm -rf ./dump.rdb
rm -rf ./nodes.conf
rm -rf ./appendonly.aof
rm -rf ./nodes-7006.conf
-------------------------


# 执行脚本
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# /usr/local/redis/redis-cluster/start-all.sh 
# 看一下 redis 是否已启动
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# ps -ef | grep redis
root     14030     1  0 Jun14 ?        00:01:24 /usr/local/redis/redis-5.0.5/src/redis-server *:6379
root     16424 16283  0 15:08 pts/2    00:00:00 view redis.conf
root     16564     1  0 16:38 ?        00:00:00 ./src/redis-server *:7001 [cluster]
root     16566     1  0 16:38 ?        00:00:00 ./src/redis-server *:7002 [cluster]
root     16568     1  0 16:38 ?        00:00:00 ./src/redis-server *:7003 [cluster]
root     16570     1  0 16:38 ?        00:00:00 ./src/redis-server *:7004 [cluster]
root     16581     1  0 16:38 ?        00:00:00 ./src/redis-server *:7005 [cluster]
root     16586     1  0 16:38 ?        00:00:00 ./src/redis-server *:7006 [cluster]
root     16601 15942  0 16:39 pts/0    00:00:00 grep --color=auto redis
# 备注: 无论成功与否,都到 redis 实例 的 日志下看 一下是否有 Error 与 Exception 
view /usr/local/redis/redis-cluster/logs/*.log
# 在redis源码文件夹下的src下的 ruby脚本 redis-trib.rb 拷贝到 redis-cluster 目录下
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# cp /usr/local/redis/redis-5.0.5/src/redis-trib.rb  /usr/local/redis/redis-cluster/
# 执行 构建集群 的命令 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# ./redis-trib.rb create --replicas 1 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006
# 上面 redis-trib.rb 命令 的 输出如下  开始########################
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

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

To get help about all subcommands, type:
redis-cli --cluster help
# 上面 redis-trib.rb 命令 的 输出如下  结束########################
# 上面的意思是说 不推荐 redis-trib.rb 构建集群了,redis-trib.rb 的所有命令和功能都已经移到 redis-cli 中了
# 备注 redis-cli会按照给定的顺序设置主节点和从节点, 这里的 7001 7002 7003 是主节点
# 查看 redis-cli 是否已经在 PATH 环境变量中 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# echo $PATH
# 因为 redis-cli 是在 redis 的 src 下, 且 PATH 中有 redis 的 src 路径,所以 可以在 任意目录下 使用 redis-cli 命令
/usr/local/redis/redis-5.0.5/src:/usr/local/zookeeper/apache-zookeeper-3.5.5-bin/bin:/usr/local/zookeeper/apache-zookeeper-3.5.5-bin/bin:/usr/java/jdk-11.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
# 构建集群 博文地址 https://blog.csdn.net/u012062455/article/details/87280467
# 备注 这些ip 要使用 外网ip 而不是 内网ip ,外ip  47.103.137.122
# 备注 执行集群构建命令后 输入 yes 并按回车即可
redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 47.103.137.122:7005 to 47.103.137.122:7001
Adding replica 47.103.137.122:7006 to 47.103.137.122:7002
Adding replica 47.103.137.122:7004 to 47.103.137.122:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4665fff2e6579a3b98f9e4a0a431b5782c64468d 47.103.137.122:7001
   slots:[0-5460] (5461 slots) master
M: 69e65b543d00babed7d1410773ae5765843b9289 47.103.137.122:7002
   slots:[5461-10922] (5462 slots) master
M: df50a52c1c57925fdf7c5f0586a58440c4e2a393 47.103.137.122:7003
   slots:[10923-16383] (5461 slots) master
S: 43579afb51bee7bec06b206f1b1958f23b985414 47.103.137.122:7004
   replicates 69e65b543d00babed7d1410773ae5765843b9289
S: a4d379acaab3f5ef3f8a5eee2c04722387d073f2 47.103.137.122:7005
   replicates df50a52c1c57925fdf7c5f0586a58440c4e2a393
S: 0931c318d513a9d875acc13d11b6bdc16a8c2b4c 47.103.137.122:7006
   replicates 4665fff2e6579a3b98f9e4a0a431b5782c64468d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
# 这里无论成功与否 都要去 看一下 每个redis 实例的日志文件 是否有 error 或 exception 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# view /usr/local/redis/redis-cluster/logs/*.log
----------------------
# 集群构建不成功时要重新做的事情
# 如果启动不成功, 则 需要先停止所有redis实例,删除 启动失败时的一些生成文件,然后重新启动所有redis实例,再重新构建集群
# 停止所有redis实例
/usr/local/redis/redis-cluster/stop-all.sh
# 删除 启动失败时的一些生成文件
/usr/local/redis/redis-cluster/clear-all.sh 
# 重新启动所有redis实例
/usr/local/redis/redis-cluster/start-all.sh 
# 重新构建集群
redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
---------------------- 
# 备注 执行上面命令 后 输入 yes 后 一直处于 redis集群 Waiting for the cluster to join 一直等待状态, 
# 百度后 的解决方式 https://blog.csdn.net/xianzhixianzhixian/article/details/82392172
# 开发总线端口  和 客户端端口
 # centos 7 开启 7001到7006  ,  17001到17006  端口
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=6379/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7001/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7002/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7003/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7004/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7005/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=7006/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=16379/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17001/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17002/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17003/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17004/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17005/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=17006/tcp
#重启防火墙
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --reload
#查看已开放得所有端口
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# firewall-cmd --list-ports
80/tcp 8080/tcp 6379/tcp 7001/tcp 7002/tcp 7003/tcp 7004/tcp 7005/tcp 7006/tcp 17001/tcp 16379/tcp 17002/tcp 17003/tcp 17004/tcp 17005/tcp 17006/tcp
---------------------- 
# 然后 再次 重新关闭 redis 实例 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# ./stop-all.sh 
# 清理启动失败时的文件
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# ./clear-all.sh 
# 然后 再次 重新开启 redis 实例 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# ./start-all.sh 
# 重新构建集群
# 备注 这些ip 要使用 外网ip 而不是 内网ip ,外ip  47.103.137.122
# 备注 执行集群构建命令后 输入 yes 并按回车即可
redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
# 备注 执行集群构建命令后 输入 yes 并按回车即可
redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# redis-cli  -a yuchao#redis  --cluster create 47.103.137.122:7001 47.103.137.122:7002 47.103.137.122:7003 47.103.137.122:7004 47.103.137.122:7005 47.103.137.122:7006 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 47.103.137.122:7005 to 47.103.137.122:7001
Adding replica 47.103.137.122:7006 to 47.103.137.122:7002
Adding replica 47.103.137.122:7004 to 47.103.137.122:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4665fff2e6579a3b98f9e4a0a431b5782c64468d 47.103.137.122:7001
   slots:[0-5460] (5461 slots) master
M: 69e65b543d00babed7d1410773ae5765843b9289 47.103.137.122:7002
   slots:[5461-10922] (5462 slots) master
M: df50a52c1c57925fdf7c5f0586a58440c4e2a393 47.103.137.122:7003
   slots:[10923-16383] (5461 slots) master
S: 43579afb51bee7bec06b206f1b1958f23b985414 47.103.137.122:7004
   replicates 69e65b543d00babed7d1410773ae5765843b9289
S: a4d379acaab3f5ef3f8a5eee2c04722387d073f2 47.103.137.122:7005
   replicates df50a52c1c57925fdf7c5f0586a58440c4e2a393
S: 0931c318d513a9d875acc13d11b6bdc16a8c2b4c 47.103.137.122:7006
   replicates 4665fff2e6579a3b98f9e4a0a431b5782c64468d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 47.103.137.122:7001)
M: 4665fff2e6579a3b98f9e4a0a431b5782c64468d 47.103.137.122:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 69e65b543d00babed7d1410773ae5765843b9289 47.103.137.122:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a4d379acaab3f5ef3f8a5eee2c04722387d073f2 47.103.137.122:7005
   slots: (0 slots) slave
   replicates df50a52c1c57925fdf7c5f0586a58440c4e2a393
M: df50a52c1c57925fdf7c5f0586a58440c4e2a393 47.103.137.122:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 43579afb51bee7bec06b206f1b1958f23b985414 47.103.137.122:7004
   slots: (0 slots) slave
   replicates 69e65b543d00babed7d1410773ae5765843b9289
S: 0931c318d513a9d875acc13d11b6bdc16a8c2b4c 47.103.137.122:7006
   slots: (0 slots) slave
   replicates 4665fff2e6579a3b98f9e4a0a431b5782c64468d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 这里无论成功与否 都要去 看一下 每个redis 实例的日志文件 是否有 error 或 exception 
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# view /usr/local/redis/redis-cluster/logs/*.log

# redis 集群 各种命令 博文地址:  https://www.cnblogs.com/wxd0108/p/5798332.html
# 登录 7001 的 redis server 客户端 (备注 这里的ip就是内网ip了)
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# redis-cli -a yuchao#redis -h 172.18.10.245 -p 7001 
172.18.10.245:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:844
cluster_stats_messages_pong_sent:890
cluster_stats_messages_sent:1734
cluster_stats_messages_ping_received:885
cluster_stats_messages_pong_received:844
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1734
#登录 (备注 这里的ip就是内网ip了)
redis-cli -a yuchao#redis -a yuchao#redis -h 172.18.10.245 -p 7001
172.18.10.245:7001> set name yuchao
(error) MOVED 5798 172.18.10.245:7002
# 出现上面错误的原因是没有以集群的方式打开客户端 ,下面 对 redis-cli 添加入参 -c 即 以 集群的模式启动
[root@iZuf6hyvanq21thxm114s6Z redis-cluster]# redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.18.10.245:7001> set name yuchao
-> Redirected to slot [5798] located at 172.18.10.245:7002
OK
#下面是登录 各ip 下 port 下 redis 客户端的命令 
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7001
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7002 
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7003 
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7004 
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7005 
redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7006 
# 如果想让 java 客户端 连接到 阿里云 ecs 服务器 里部署的 redis server 就 还要在 阿里云ecs 服务器 设置安全规则
# 即 7001到7006, 17001到17006 端口 能接收  0.0.0.0/0 即 任意ip地址的计算机 来访问 这些端口
# 博文地址 https://www.jianshu.com/p/13e7a8dfcb63 添加安全规则
# 如果想要让 本地java 应用程序访问,还要必须 在 阿里云 添加两个安全规则 即 对 17001/17006 与 7001/7006 的 端口 接收 任意ip任意端口的请求
# java 客户端 单独连 6379 时, 本地能ping 通 47.103.137.122 ,但是 并不能 telnet 47.103.137.122 6379 即 情况如下 
# windows cmd 窗口 ping 与 telnet 情况如下开始=================
C:\Users\Administrator>ping 47.103.137.122

正在 Ping 47.103.137.122 具有 32 字节的数据:
来自 47.103.137.122 的回复: 字节=32 时间=5ms TTL=54
来自 47.103.137.122 的回复: 字节=32 时间=7ms TTL=54
来自 47.103.137.122 的回复: 字节=32 时间=4ms TTL=54
来自 47.103.137.122 的回复: 字节=32 时间=5ms TTL=54

47.103.137.122 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 4ms,最长 = 7ms,平均 = 5ms

C:\Users\Administrator>telnet 47.103.137.122 6379
正在连接47.103.137.122...无法打开到主机的连接。 在端口 6379: 连接失败
# 如果连接失败 说明 linux 防火墙没开 这个端口号,开端口号的 命令 上面已经 写了
# windows cmd 窗口 ping 与 telnet 情况如下结束=================
#java客户端抛出的异常是 
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 47.103.137.122:6379
# 这里 在 阿里云 服务器 实 添加规则 让 6379 端口 接收 任意 0.0.0.0/0 所有网络段
# centos7 开通 6379 端口,并重启 防火墙
centos 7 开启 6379 端口号
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=6379/tcp
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --permanent --zone=public --add-port=16379/tcp
#重启防火墙
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --reload
#查看已开放得所有端口
[root@iZuf6hyvanq21thxm114s6Z logs]# firewall-cmd --list-ports

redis-cli -c -a yuchao#redis -h 172.18.10.245 -p 7001

# 登
redis-cli -a yuchao#redis -h 47.103.137.122 -p 6379
# 启
/usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis.conf
========
redis 集群命令 汇总
========
# 显示当前连接的集群的各种信息
cluster info
 # 获取集群上的所有的节点信息
cluster nodes

========
java 客户端 jedis  与 redis server 建立连接时异常:
redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections? 
========
博文地址
https://blog.csdn.net/weixin_42629535/article/details/86490692
# 到 redis.conf 文件中找到 bind 127.0.0.1 并注掉   
vi /usr/local/redis/redis-5.0.5/redis.conf
# 正常停止redis server 借助  redis-cli shutdown
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# /usr/local/redis/redis-5.0.5/src/redis-cli -p 6379
127.0.0.1:6379> auth yuchao#redis
OK
127.0.0.1:6379> shutdown
# 查看 redis server 是否已经关闭
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     32498  3722  0 18:04 pts/0    00:00:00 grep --color=auto redis
# 然后 在 执行 启动可执行文件时 传入 redis.conf 的绝对路径
[root@iZuf6hyvanq21thxm114s6Z src]# /usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis.conf
# 查看 redis server  是否已成功启动
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     32532     1  0 18:05 ?        00:00:00 /usr/local/redis/redis-5.0.5/src/redis-server *:6379
========
java 客户端 jedis  与 redis server 建立连接时异常:
ERR This instance has cluster support disabled
========
博文地址
https://blog.csdn.net/z960339491/article/details/80521851
# redis server 以集群启动
# 到 redis.conf 文件中找到 cluster-enabled yes 并放开   
vi /usr/local/redis/redis-5.0.5/redis.conf
# 正常停止redis server 借助  redis-cli shutdown
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# /usr/local/redis/redis-5.0.5/src/redis-cli -p 6379
127.0.0.1:6379> auth yuchao#redis
OK
127.0.0.1:6379> shutdown
# 查看 redis server 是否已经关闭
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     32498  3722  0 18:04 pts/0    00:00:00 grep --color=auto redis
# 然后 在 执行 启动可执行文件时 传入 redis.conf 的绝对路径
[root@iZuf6hyvanq21thxm114s6Z src]# /usr/local/redis/redis-5.0.5/src/redis-server /usr/local/redis/redis-5.0.5/redis.conf
# 查看 redis server  是否已成功启动
[root@iZuf6hyvanq21thxm114s6Z redis-5.0.5]# ps -ef | grep redis
root     32532     1  0 18:05 ?        00:00:00 /usr/local/redis/redis-5.0.5/src/redis-server *:6379

========
java客户端 连接 redis cluster 集群 代码
========
    @Button(value = "testRedisCluster", level = 1)
    public void testRedisCluster() {
        logger.info(" 类 testRedisCluster 方法 开始====");
        // https://www.cnblogs.com/dijia478/p/10755580.html
        /** 全局jedis */
        JedisCluster jedisCluster;
        try {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            // 最大连接数
            poolConfig.setMaxTotal(5);
            // 最大空闲数
            poolConfig.setMaxIdle(5);
            // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
            // Could not get a resource from the pool
            poolConfig.setMaxWaitMillis(10000);
            Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
            // telnet 47.103.137.122 7001
            // telnet 47.103.137.122 7002
            // telnet 47.103.137.122 7003
            // telnet 47.103.137.122 7004
            // telnet 47.103.137.122 7005
            // telnet 47.103.137.122 7006
            nodes.add(new HostAndPort("47.103.137.122", 7001));
            nodes.add(new HostAndPort("47.103.137.122", 7002));
            nodes.add(new HostAndPort("47.103.137.122", 7003));
            nodes.add(new HostAndPort("47.103.137.122", 7004));
            nodes.add(new HostAndPort("47.103.137.122", 7005));
            nodes.add(new HostAndPort("47.103.137.122", 7006));
            // JedisCluster cluster = new JedisCluster(nodes, poolConfig);
            logger.info("连接前");

           //备注 将 连接超时时间 设置的大一些 否则  会抛出  No reachable node in cluster 异常
            jedisCluster = new JedisCluster(nodes, 5000, 5000, 10, "yuchao#redis", poolConfig);
            logger.info("连接后");
            logger.info("set前");
            String setRet=jedisCluster.set("name", "yuchao");
            logger.info("set后 返回值:{}",setRet);
            String name = jedisCluster.get("name");
            logger.info(" 键name的值: [{}] " ,name);
        } catch (Exception e) {
            logger.info("\n 类 testRedisCluster 方法 异常信息如下: \n", e);
        }
        logger.info(" 类 testRedisCluster 方法 结束====\n");
    }
打印日志如下
2019-06-15 17:15:42,022 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] -  类 testRedisCluster 方法 开始====
2019-06-15 17:15:42,025 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] - 连接前
2019-06-15 17:15:42,175 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] - 连接后
2019-06-15 17:15:42,175 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] - set前
2019-06-15 17:15:42,206 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] - set后 返回值:OK
2019-06-15 17:15:42,212 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] -  键name的值: [yuchao] 
2019-06-15 17:15:42,212 [AWT-EventQueue-0] INFO  [com.yu.chao.redis.rediscluster.test.RedisClusterTest] -  类 testRedisCluster 方法 结束====
----------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值