【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版) 简介 准备工具 第一步:准备Redis配置文件(redis.conf) 第二步:分别启动6个Redis 第三步:  安装Ruby运行环境 第四步:安装Redis的驱动 Redis gem 第五步:redis-trib.rb create 创建集群 测试验证

学习Redis Cluster的第一步,即本地搭建Redis Cluster。但是在Redis的官方文档中,是介绍在Linux系统中搭建Redis Cluster。本文主要介绍在Windows系统中如何快速创建一个3主/3从的Redis Cluster(Redis集群)。

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

准备工具

1)在GitHub中下载由Microsoft发布的Windows版Redis文件(当前最新版本为3.2.100, 下载ZIP文件,解压后双击 redis-server.exe 就可以启用一个单机的Reids服务)。下载地址:https://github.com/MicrosoftArchive/redis/releases

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

2)Redis 3版本需要使用  redis-trib.rb create 来执行集群的创建工作,也需要从Github中下载 redis-trib.rb文件(如无法下载,可在本文的附录中复制)。下载地址:https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

3)Ruby 运行环境(因为redis-trib.rb是Ruby语言编写,所以需要在Windows本机中安装Ruby运行环境)。下载地址:https://rubyinstaller.org/downloads/

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

4)Ruby Redis驱动 Redis gem。下载地址:https://rubygems.org/gems/redis/versions/4.5.1

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

For Redis version 3 or 4, there is the older tool called redis-trib.rb which is very similar. You can find it in the src directory of the Redis source code distribution. You need to install redis gem to be able to run redis-trib.

Source: https://redis.io/topics/cluster-tutorial#creating-the-cluster

第一步:准备Redis配置文件(redis.conf)

创建 cluster_test文件夹,这次实验使用7000,7001,7002,7003,7004,7005 这六个端口。

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005

分别创建好这个6个子文件夹后,创建redis.conf文件,把下面最基本的配置文件内容分别放在在这六个文件夹中

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

注: 每个子文件夹中的端口需要对应修改为7001,... 7005。

最终效果为:

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

第二步:分别启动6个Redis

复制redis-server.exe文件到cluster-test中,然后打开6个CMD窗口,分别进入到7000, ... 7005 目录中。运行启动Redis Service命令

..
edis-server .
edis.conf

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

注:需要在6个CMD中对7000,7001,7002,7003,7004,7005 启动Reids Server

第三步:  安装Ruby运行环境

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

双击安装即可,所有选项保持默认。

第四步:安装Redis的驱动 Redis gem

复制 redis-4.5.1.gem 文件到Ruby的安装目录,运行   gem install --local C:Ruby30-x64 edis-4.5.1.gem   ,等待安装成功。

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

第五步:redis-trib.rb create 创建集群

把下载的redis-trib.rb文件放在cluster_test目录中,CMD窗口进入 cluster_test 目录,执行  redis-trib.rb create 

redis-trib.rb create --replicas 1 127.0.0.1:7000 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

当从日志中看见 [OK] All 16384 slots covered 日志,表示集群创建完成。表示至少一个主节点可以对16384个槽(slots)提供服务了。

创建动画图:

 【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

 

测试验证

使用 redis-cli.exe 工具(包含Redis的下载ZIP文件中)可以非常容易的查看Cluster Node信息和 Set, Get Key

redis-cli.exe -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7002> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

使用 cluster nodes查看节点信息:

【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介
准备工具
第一步:准备Redis配置文件(redis.conf)
第二步:分别启动6个Redis
第三步:  安装Ruby运行环境
第四步:安装Redis的驱动 Redis gem
第五步:redis-trib.rb create 创建集群
测试验证

附件1:redis-trib.rb 文件内容

#!/usr/bin/env ruby

# TODO (temporary here, we'll move this into the Github issues once
#       redis-trib initial implementation is completed).
#
# - Make sure that if the rehashing fails in the middle redis-trib will try
#   to recover.
# - When redis-trib performs a cluster check, if it detects a slot move in
#   progress it should prompt the user to continue the move from where it
#   stopped.
# - Gracefully handle Ctrl+C in move_slot to prompt the user if really stop
#   while rehashing, and performing the best cleanup possible if the user
#   forces the quit.
# - When doing "fix" set a global Fix to true, and prompt the user to
#   fix the problem if automatically fixable every time there is something
#   to fix. For instance:
#   1) If there is a node that pretend to receive a slot, or to migrate a
#      slot, but has no entries in that slot, fix it.
#   2) If there is a node having keys in slots that are not owned by it
#      fix this condition moving the entries in the same node.
#   3) Perform more possibly slow tests about the state of the cluster.
#   4) When aborted slot migration is detected, fix it.

require 'rubygems'
require 'redis'

ClusterHashSlots = 16384

def xputs(s)
    case s[0..2]
    when ">>>"
        color="29;1"
    when "[ER"
        color="31;1"
    when "[OK"
        color="32"
    when "[FA","***"
        color="33"
    else
        color=nil
    end

    color = nil if ENV['TERM'] != "xterm"
    print "