当前位置:   article > 正文

RedisCluster的安装、部署、扩容和Java客户端调用_redis cluster java客户端配置

redis cluster java客户端配置

开篇前言

文章内容来源:拉勾教育大数据开发高薪训练营。

需求描述

如图:
1、搭建Redis5.0集群,要求三主三从,记录下安装步骤;
2、能够添加一主一从(Master4和Slaver4),记录下安装步骤;
3、能够通过JedisCluster向RedisCluster添加数据和取出数据。

需求分析

Redis单机版安装

第一步:安装C语言需要的GCC环境

  1. yum install -y gcc-c++
  2. yum install -y wget

第二步:下载并解压缩Redis源码压缩包

  1. # 本地有安装包无需再次下载,解压即可
  2. wget http://download.redis.io/releases/redis-5.0.5.tar.gz
  3. tar -zxf redis-5.0.5.tar.gz

第三步:编译Redis源码,进入redis-5.0.5目录,执行编译命令

  1. cd redis-5.0.5/src
  2. make

第四部:安装Redis,需要通过PREFIX指定安装路径

  1. mkdir /usr/redis -p
  2. make install PREFIX=/usr/redis

注:从第三步后可以直接使用编译过的Redis包,后续利用编译后的Redis安装包,只需要执行第四步即可(前提:每台节点需满足第一步的条件)

第五步:后端启动,进行$REDIS_HOME/bin/redis.conf配置文件修改

5.1、拷贝 redis-5.0.5/redis.conf 配置文件到 Redis 安装目录的 bin 目录

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

5.2、修改 redis.conf,vi $REDIS_HOME/bin/redis.conf

  1. # 将`daemonize`由`no`改为`yes`
  2. daemonize yes
  3. # 默认绑定的是回环地址,默认不能被其他机器访问
  4. # bind 127.0.0.1
  5. # 是否开启保护模式,由yes该为no
  6. protected-mode no

5.3、后端模式启动与关闭服务

  1. # 后端模式启动服务
  2. cd /usr/redis;bin/redis-server bin/redis.conf
  3. # 进入命令行模式
  4. cd /usr/redis;bin/redis-cli -h hostname -p 端口号
  5. cd /usr/redis;bin/redis-cli -h 127.0.0.1 -p 6379 #默认
  6. # 退出命令行模式
  7. exit
  8. # 后端模式关闭服务(推荐)
  9. cd /usr/redis;bin/redis-cli shutdown
  10. # 前端模式启动服务(不推荐)
  11. cd /usr/redis;bin/redis-server

Redis单机版Java API操作

1、导入依赖jar包

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0</version>
  5. </dependency>

2、示例代码

  1. package com.lagou.redis;
  2. import org.junit.Test;
  3. import redis.clients.jedis.Jedis;
  4. public class JedisDemo {
  5. @Test
  6. public void testConn() {
  7. //与Redis建立连接 IP+port
  8. Jedis redis = new Jedis("linux121", 6379);
  9. //在Redis中写字符串 key value
  10. redis.set("jedis:name:1", "jd-zhangfei");
  11. //获得Redis中字符串的值
  12. System.out.println(redis.get("jedis:name:1"));
  13. //在Redis中写list
  14. redis.lpush("jedis:list:1", "1", "2", "3", "4", "5");
  15. //获得list的长度
  16. System.out.println(redis.llen("jedis:list:1"));
  17. }
  18. }

3、运行结果

  1. jd-zhangfei
  2. 5
  3. Process finished with exit code 0

搭建三主三从Redis5.0集群

集群规划

1、RedisCluster最少需要三台主服务器,三台从服务器;
2、端口号分别为:7001-7006[7008];
3、路径规划(linux121上):mkdir -p /root/redis-cluster。

第一步:创建7001实例,并复制redis.conf文件

  1. # 编译安装
  2. cd /opt/lagou/software/redis-5.0.5/src
  3. make install PREFIX=/root/redis-cluster/7001
  4. # 复制redis.conf文件
  5. cp /opt/lagou/software/redis-5.0.5/redis.conf /root/redis-cluster/7001/bin/

第二步:修改redis.conf配置文件

  1. vi /root/redis-cluster/7001/bin/redis.conf
  2. # 修改port为7001
  3. port 7001
  4. # 打开cluster-enable yes
  5. cluster-enable yes
  6. # ----------------------------------------------
  7. # 配置后端开启模式
  8. # 将`daemonize`由`no`改为`yes`
  9. daemonize yes
  10. # 默认绑定的是回环地址,默认不能被其他机器访问
  11. # bind 127.0.0.1
  12. # 所有机器可以访问
  13. # bind 0.0.0.0
  14. # 建议注释掉 bind x.x.x.x
  15. # 是否开启保护模式,由yes该为no
  16. protected-mode no

第三步:复制7001,创建7002~7008实例,注意端口的修改

  1. # 拷贝7001
  2. cp -r /root/redis-cluster/7001 /root/redis-cluster/700*
  3. # 重新修改:先删除再复制新的redis.conf文件
  4. # 好处:只需要先修改7001的配置文件,再复制,再修改port即可
  5. # rm -rf /root/redis-cluster/700*/bin/redis.conf
  6. # cp /root/redis-cluster/7001/bin/redis.conf /root/redis-cluster/700*/bin/redis.conf
  7. # 修改对应的端口号
  8. vi /root/redis-cluster/700*/bin/redis.conf

第四部:创建start.sh,启动所有实例

  1. # 编写群起脚本
  2. cd /root/redis-cluster/;vi start-local-redis-cluster.sh
  3. # 启动7001
  4. cd /root/redis-cluster/7001/bin;redis-server redis.conf
  5. # 启动7002
  6. cd /root/redis-cluster/7002/bin;redis-server redis.conf
  7. # 启动7003
  8. cd /root/redis-cluster/7003/bin;redis-server redis.conf
  9. # 启动7004
  10. cd /root/redis-cluster/7004/bin;redis-server redis.conf
  11. # 启动7005
  12. cd /root/redis-cluster/7005/bin;redis-server redis.conf
  13. # 启动7006
  14. cd /root/redis-cluster/7006/bin;redis-server redis.conf
  15. # 启动指令
  16. cd /root/redis-cluster/;bash start-local-redis-cluster.sh

进入客户端

  1. # 进入客户端(单节点进入:非集群模式)
  2. # /root/redis-cluster/700*/bin/redis-cli -h linux121 -p 700*
  3. /root/redis-cluster/7001/bin/redis-cli -h linux121 -p 7001

创建stop.sh,关闭所有的实例

  1. # 编写群起脚本
  2. cd /root/redis-cluster/;vi stop-local-redis-cluster.sh
  3. # 关闭7001
  4. cd /root/redis-cluster/7001/bin;redis-cli -h linux121 -p 7001 shutdown
  5. # 关闭7002
  6. cd /root/redis-cluster/7002/bin;redis-cli -h linux121 -p 7002 shutdown
  7. # 关闭7003
  8. cd /root/redis-cluster/7003/bin;redis-cli -h linux121 -p 7003 shutdown
  9. # 启关闭7004
  10. cd /root/redis-cluster/7004/bin;redis-cli -h linux121 -p 7004 shutdown
  11. # 关闭7005
  12. cd /root/redis-cluster/7005/bin;redis-cli -h linux121 -p 7005 shutdown
  13. # 关闭7006
  14. cd /root/redis-cluster/7006/bin;redis-cli -h linux121 -p 7006 shutdown
  15. # Redis集群关闭指令
  16. cd /root/redis-cluster/;bash stop-local-redis-cluster.sh

第五步:创建Redis集群(创建时Redis中不要有数据)

  1. # 删除集群遗留任何数据
  2. # cd /root/redis-cluster/700*/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  3. cd /root/redis-cluster/7001/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  4. cd /root/redis-cluster/7002/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  5. cd /root/redis-cluster/7003/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  6. cd /root/redis-cluster/7004/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  7. cd /root/redis-cluster/7005/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  8. cd /root/redis-cluster/7006/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  9. cd /root/redis-cluster/7007/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  10. cd /root/redis-cluster/7008/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  11. # 创建集群
  12. # 注:必须写192.168.1.121(而不是linux121),否则出现:
  13. # 1)ERR Invalid node address specified: linux121:7001;
  14. # 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;
  15. # 3)jedis-2.9.3:redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
  16. cd /root/redis-cluster/7001/bin;redis-cli --cluster create 192.168.1.121:7001 192.168.1.121:7002 192.168.1.121:7003 192.168.1.121:7004 192.168.1.121:7005 192.168.1.121:7006 --cluster-replicas 1
  17. # 输入yes继续部署

  验证信息:

  

进入集群客户端

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

  验证信息:

  

集群扩容

需求:为集群添加一主一从

1、添加主节点

  1. # 启动node7007
  2. cd /root/redis-cluster/7007/bin;redis-server redis.conf
  3. # 添加node7007为集群主节点
  4. cd /root/redis-cluster/7007/bin/;redis-cli --cluster add-node 192.168.1.121:7007 192.168.1.121:7001

  验证信息:

  

2、为新添加的主节点分配slots槽

  1. cd /root/redis-cluster/7007/bin/;redis-cli --cluster reshard 192.168.1.121:7007
  2. # How many slots do you want to move(from 1 to 16384)?
  3. # 输入3000(数值不超过18364)
  4. 3000
  5. # 输入分slots的主节点ID(新添加的主节点ID)
  6. 04969194dd9b051d16dc41ef8cd4a72a71f03c51
  7. # 通过cluster nodes可查看
  8. # 选择分槽方式
  9. # 选择all
  10. all
  11. # 一直输入yes继续完成
  12. yes

3、为新添加的主节点添加一个从节点

  1. # 启动node7008
  2. cd /root/redis-cluster/7008/bin;redis-server redis.conf
  3. # 为新添加的主节点添加从节点
  4. cd /root/redis-cluster/7007/bin/;redis-cli --cluster add-node 192.168.1.121:7008 192.168.1.121:7007 --cluster-slave --cluster-master-id 04969194dd9b051d16dc41ef8cd4a72a71f03c51

  验证信息:

  

4、创建start.sh,启动所有实例(7001~7008)

  1. # 编写群起脚本
  2. cd /root/redis-cluster/;vi start-local-redis-cluster.sh
  3. # 启动7001
  4. cd /root/redis-cluster/7001/bin;redis-server redis.conf
  5. # 启动7002
  6. cd /root/redis-cluster/7002/bin;redis-server redis.conf
  7. # 启动7003
  8. cd /root/redis-cluster/7003/bin;redis-server redis.conf
  9. # 启动7004
  10. cd /root/redis-cluster/7004/bin;redis-server redis.conf
  11. # 启动7005
  12. cd /root/redis-cluster/7005/bin;redis-server redis.conf
  13. # 启动7006
  14. cd /root/redis-cluster/7006/bin;redis-server redis.conf
  15. # 启动7007
  16. cd /root/redis-cluster/7007/bin;redis-server redis.conf
  17. # 启动7008
  18. cd /root/redis-cluster/7008/bin;redis-server redis.conf
  19. # 启动指令
  20. cd /root/redis-cluster/;bash start-local-redis-cluster.sh

5、进入客户端

  1. # 进入客户端(集群模式)
  2. # /root/redis-cluster/700*/bin/redis-cli -h linux121 -p 700* -c
  3. /root/redis-cluster/7001/bin/redis-cli -h linux121 -p 7001 -c

6、创建stop.sh,关闭所有的实例(7001~7008)

  1. # 编写群起脚本
  2. cd /root/redis-cluster/;vi stop-local-redis-cluster.sh
  3. # 关闭7001
  4. cd /root/redis-cluster/7001/bin;redis-cli -h linux121 -p 7001 shutdown
  5. # 关闭7002
  6. cd /root/redis-cluster/7002/bin;redis-cli -h linux121 -p 7002 shutdown
  7. # 关闭7003
  8. cd /root/redis-cluster/7003/bin;redis-cli -h linux121 -p 7003 shutdown
  9. # 启关闭7004
  10. cd /root/redis-cluster/7004/bin;redis-cli -h linux121 -p 7004 shutdown
  11. # 关闭7005
  12. cd /root/redis-cluster/7005/bin;redis-cli -h linux121 -p 7005 shutdown
  13. # 关闭7006
  14. cd /root/redis-cluster/7006/bin;redis-cli -h linux121 -p 7006 shutdown
  15. # 关闭7007
  16. cd /root/redis-cluster/7007/bin;redis-cli -h linux121 -p 7007 shutdown
  17. # 关闭7008
  18. cd /root/redis-cluster/7008/bin;redis-cli -h linux121 -p 7008 shutdown
  19. # Redis集群关闭指令
  20. cd /root/redis-cluster/;bash stop-local-redis-cluster.sh

JedisCluster操作RedisCluster

1、导入依赖jar包

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.3</version>
  5. </dependency>

2、示例代码

  1. package com.lagou.redis;
  2. import redis.clients.jedis.HostAndPort;
  3. import redis.clients.jedis.JedisCluster;
  4. import redis.clients.jedis.JedisPoolConfig;
  5. import java.io.IOException;
  6. import java.util.HashSet;
  7. public class JedisClutserDemo {
  8. public static void main(String[] args) throws IOException {
  9. JedisPoolConfig config = new JedisPoolConfig();
  10. HashSet<HostAndPort> jedisClusterNodes = new HashSet<>();
  11. //添加集群节点
  12. jedisClusterNodes.add(new HostAndPort("linux121",7001));
  13. jedisClusterNodes.add(new HostAndPort("linux121",7002));
  14. jedisClusterNodes.add(new HostAndPort("linux121",7003));
  15. jedisClusterNodes.add(new HostAndPort("linux121",7004));
  16. jedisClusterNodes.add(new HostAndPort("linux121",7005));
  17. jedisClusterNodes.add(new HostAndPort("linux121",7006));
  18. jedisClusterNodes.add(new HostAndPort("linux121",7007));
  19. jedisClusterNodes.add(new HostAndPort("linux121",7008));
  20. //获取集群连接
  21. JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, config);
  22. //设置值
  23. jedisCluster.set("name:001","zhangfei");
  24. //获取值
  25. String value = jedisCluster.get("name:001");
  26. System.out.println(value);
  27. //关闭连接
  28. jedisCluster.close();
  29. }
  30. }

3、运行结果

  1. zhangfei
  2. Process finished with exit code 0

温馨提示

假如第一次未顺利搭建成功,建议进行重新部署,此时部署会很快(基于之前的一些配置的基础上)

  1. # 执行以下指令报下面的Error
  2. cd /root/redis-cluster/7001/bin;redis-cli --cluster create 192.168.1.121:7001 192.168.1.121:7002 192.168.1.121:7003 192.168.1.121:7004 192.168.1.121:7005 192.168.1.121:7006 192.168.1.121:7007 192.168.1.121:7008 --cluster-replicas 1
  3. # [ERR] Node 192.168.1.121:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
  4. # 关闭集群
  5. cd /root/redis-cluster/;bash stop-local-redis-cluster.sh
  6. # 删除原先集群信息以便于重新构建
  7. # cd /root/redis-cluster/700*/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  8. cd /root/redis-cluster/7001/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  9. cd /root/redis-cluster/7002/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  10. cd /root/redis-cluster/7003/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  11. cd /root/redis-cluster/7004/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  12. cd /root/redis-cluster/7005/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  13. cd /root/redis-cluster/7006/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  14. cd /root/redis-cluster/7007/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  15. cd /root/redis-cluster/7008/bin/;rm -rf dump.rdb nodes.conf appendonly.aof
  16. # 启动集群
  17. cd /root/redis-cluster/;bash start-local-redis-cluster.sh
  18. # 查看集群
  19. ps -ef | grep redis
  20. # 再次执行
  21. cd /root/redis-cluster/7001/bin;redis-cli --cluster create 192.168.1.121:7001 192.168.1.121:7002 192.168.1.121:7003 192.168.1.121:7004 192.168.1.121:7005 192.168.1.121:7006 192.168.1.121:7007 192.168.1.121:7008 --cluster-replicas 1
  22. # 测试集群
  23. # 进入客户端(集群模式)
  24. # /root/redis-cluster/700*/bin/redis-cli -h linux121 -p 700* -c
  25. /root/redis-cluster/7001/bin/redis-cli -h linux121 -p 7001 -c
  26. cluster info
  27. cluster nodes

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/785822
推荐阅读
相关标签
  

闽ICP备14008679号