# 高性能分布式缓存Redis_分布式 Redis 解决方案Codis(Twemproxy替代方案) **Repository Path**: zhouhtb_admin/redis ## Basic Information - **Project Name**: 高性能分布式缓存Redis_分布式 Redis 解决方案Codis(Twemproxy替代方案) - **Description**: 阶段五第一模块 高性能分布式缓存Redis、分布式 Redis 解决方案Codis(Twemproxy替代方案) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-11-29 - **Last Updated**: 2023-11-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RedisCluster分片集群 Redis5.0可以直接使用Redis-cli进行集群的创建和管理 # 1 环境搭建 先分别搭建三主三从的主从集群,再使用redis-cli搭建分片集群。 伪分布式搭建: | IP | 端口 | | | -------------- | ---- | ---------- | | 192.168.11.116 | 8001 | **主**服务 | | 192.168.11.116 | 8002 | **从**服务 | | 192.168.11.116 | 8003 | **主**服务 | | 192.168.11.116 | 8004 | **从**服务 | | 192.168.11.116 | 8005 | **主**服务 | | 192.168.11.116 | 8006 | **从**服务 | - 目录结构 ```reStructuredText /opt/server/redis-5.0.5/redis-cluster/conf/ip/ ├── 8001 │   └── redis.conf ├── 8002 │   └── redis.conf ├── 8003 │   └── redis.conf ├── 8004 │   └── redis.conf ├── 8005 │   └── redis.conf └── 8006 └── redis.conf ``` - 配置文件 六份配置文件 只需要改下端口、数据备份目录、pidfile,其余默认即可 ```properties # 绑定的ip bind 192.168.11.116 # 是否开启保护模式 protected-mode no # 端口 port 8001 # 后台启动 daemonize yes # 数据备份目录 dir /opt/server/redis-5.0.5/redis-cluster/conf/ip/8001/ # pid pidfile /var/run/redis_8001.pid # 开启集群 cluster-enabled yes # 其余默认 ``` - 创建8001-8006六个目录,分别放入上述的redis.conf文件 ![1600003866528](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/213117-29054.png) - 分别启动8001-8006的redis服务 使用脚本启动 ```shell #!/bin/sh ./redis/bin/redis-server ./redis-cluster/conf/ip/8001/redis.conf ./redis/bin/redis-server ./redis-cluster/conf/ip/8002/redis.conf ./redis/bin/redis-server ./redis-cluster/conf/ip/8003/redis.conf ./redis/bin/redis-server ./redis-cluster/conf/ip/8004/redis.conf ./redis/bin/redis-server ./redis-cluster/conf/ip/8005/redis.conf ./redis/bin/redis-server ./redis-cluster/conf/ip/8006/redis.conf ``` ![1600004600105](C:\Users\程林\AppData\Roaming\Typora\typora-user-images\1600004600105.png) - 创建分片集群 创建时Redis里不要有数据 ```shell # --cluster-replicas : 1 1从机 前三个为主 ./redis/bin/redis-cli --cluster create 192.168.11.116:8001 192.168.11.116:8002 192.168.11.116:8003 192.168.11.116:8004 192.168.11.116:8005 192.168.11.116:8006 --cluster-replicas 1 ``` ![1600005141948](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/215222-393883.png) - 连接分片集群 ```shell ./redis/bin/redis-cli -h 192.168.11.116 -p 8001 -c ``` * 查看集群信息 ```shell > cluster info ``` ![1600005377472](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/215643-135955.png) * 查看集群中的节点 ```shell > cluster nodes ``` ![1600005489392](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/215810-571287.png) # 2 扩容 * 在 /opt/server/redis-5.0.5/redis-cluster/conf/ip/ 创建8007和8008两个目录,并copy两份redis.cof文件修改相关信息 * 添加主节点8007 注意无数据 ```shell # 先启动8007 ./redis/bin/redis-server ./redis-cluster/conf/ip/8007/redis.conf ``` ```shell # 将8007添加到集群中 # ./redis-cli --cluster add-node命令说明: # --cluster-slave #新节点作为从节点,默认随机一个主节点 # --cluster-master-id #给新节点指定主节点 # # 添加节点,把新节点加入到指定的集群,默认添加主节点 # new_host:new_port existing_host:existing_port ./redis/bin/redis-cli --cluster add-node 192.168.11.116:8007 192.168.11.116:8001 ``` ![1600006315135](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/221157-83004.png) * 查看集群节点信息 ```shell > cluster nodes ``` ![1600006412357](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/221333-118325.png) * 重新分配hash槽(数据迁移) 添加完主节点需要对主节点进行hash槽分配,这样该主节才可以存储数据 * 给刚添加的8007结点分配槽 连接上集群(任意一个节点) ```shell ./redis/bin/redis-cli --cluster reshard 192.168.11.116:8001 ``` * 输入需要分配的hash槽数 ```reStructuredText How many slots do you want to move (from 1 to 16384)? 4096 ``` * 输入接受hash槽的节点id(8007的id) ```reStructuredText What is the receiving node ID? ed6b5d286e02d3c566748d8c15c834607a0628af ``` * 输入源的id ```reStructuredText Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. source:all ``` * 是否移动hash槽到目标节点 ```reStructuredText Do you want to proceed with the proposed reshard plan (yes/no)? yes ``` * 查看分片集群的节点信息 ![1600007261091](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/222741-329063.png) * 添加从主节点8007的从节点8008 注意8008无数据 ```shell # 先启动8008 ./redis/bin/redis-server ./redis-cluster/conf/ip/8008/redis.conf ``` * 添加从主节点8007的从节点8008 ```shell ./redis/bin/redis-cli --cluster add-node 192.168.11.116:8008 192.168.11.116:8007 --cluster-slave --cluster-master-id ed6b5d286e02d3c566748d8c15c834607a0628af ``` ![1600007752855](C:\Users\程林\AppData\Roaming\Typora\typora-user-images\1600007752855.png) * 查看8008是否已成为8007的从节点 ![1600007864878](https://gitee.com/clj2ee/typeor/raw/master/image/202009/13/223745-305475.png) # 3 Java客户端 * 使用redisCluster客户端连接Redis分片集群 ![1600008037472](C:\Users\程林\AppData\Roaming\Typora\typora-user-images\1600008037472.png) ```java JedisPoolConfig config = new JedisPoolConfig(); // Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8001)); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8002)); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8003)); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8004)); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8005)); jedisClusterNode.add(new HostAndPort("192.168.11.116", 8006)); // JedisCluster jcd = new JedisCluster(jedisClusterNode, config); jcd.set("name:1","zhaoliying"); String value = jcd.get("name:1"); ``` # redis-cli --cluster说明 ```properties redis-cli --cluster help Cluster Manager Commands: create host1:port1 ... hostN:portN #创建集群 --cluster-replicas #从节点个数 check host:port #检查集群 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点 info host:port #查看集群状态 fix host:port #修复集群 --cluster-search-multiple-owners #修复槽的重复分配问题 reshard host:port #指定集群的任意一节点进行迁移slot,重新分slots --cluster-from #需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入 --cluster-to #slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入 --cluster-slots #需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。 --cluster-yes #指定迁移时的确认输入 --cluster-timeout #设置migrate命令的超时时间 --cluster-pipeline #定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10 --cluster-replace #是否直接replace到目标节点 rebalance host:port #指定集群的任意一节点进行平衡集群节点slot数量  --cluster-weight #指定集群节点的权重 --cluster-use-empty-masters #设置可以让没有分配slot的主节点参与,默认不允许 --cluster-timeout #设置migrate命令的超时时间 --cluster-simulate #模拟rebalance操作,不会真正执行迁移操作 --cluster-pipeline #定义cluster getkeysinslot命令一次取出的key数量,默认值为10 --cluster-threshold #迁移的slot阈值超过threshold,执行rebalance操作 --cluster-replace #是否直接replace到目标节点 add-node new_host:new_port existing_host:existing_port #添加节点,把新节点加入到指定的集群,默认添加主节点 --cluster-slave #新节点作为从节点,默认随机一个主节点 --cluster-master-id #给新节点指定主节点 del-node host:port node_id #删除给定的一个节点,成功后关闭该节点服务 call host:port command arg arg .. arg #在集群的所有节点执行相关命令 set-timeout host:port milliseconds #设置cluster-node-timeout import host:port #将外部redis数据导入集群 --cluster-from #将指定实例的数据导入到集群 --cluster-copy #migrate时指定copy --cluster-replace #migrate时指定replace help ```