Redis-Cluster模式集群搭建(windows)

环境准备

  • 需要 6 台 redis 服务器。搭建伪集群。
  • 需要 6 个 redis 实例。
  • 需要运行在不同的端口 6379-6384

Ruby语言运行环境

我们需要使用ruby脚本来实现集群搭建

  • Ruby 打包系统RubyGems

    • 注:RubyGems简称gems,是一个用于对 Ruby组件进行打包的 Ruby 打包系统
  • Redis的Ruby驱动redis-xxxx.gem

  • 创建Redis集群的工具redis-trib.rb


Redis安装

安装6台Redis

在Window搭建6个伪集群

每个Redis安装文件夹根目录 redis/ 下,创建启动脚本start.bat文件,内容如下:
注意端口号与对应redis配置的端口号匹配

title redis-6379
redis-server.exe redis.windows.conf
复制代码

Redis配置

  1. 修改redis.windows.conf,端口号分别对应:6379、6380、6381、6382、6383、6384。
  2. 开启cluster-enabled :cluster-enabled yes
  3. 指定集群配置文件: cluster-config-file nodes-6379.conf,cluster-config-file nodes-6379.conf 是为该节点的配置信息,这里使用 nodes-端口.conf命名方法。服务启动后会在目录生成该文件。
  4. 指定超时:cluster-node-timeout 15000
  5. 开启持久:appendonly yes

安装Ruby

rubyinstaller-2.6.3-1-x64.exe 傻瓜式安装即可

1573699878425.png


安装Ruby驱动

  1. 解压Ruby驱动(rubygems-2.7.7) , 进入根目录,执行
ruby setup.rb
复制代码

1573699927339.png

  1. 切入到Redis目录执行(6379的Redis目录) gem install redis

1573699963621.png


执行集群构建脚本

  1. 启动6个Redis
  2. 拷贝redis-trib.rb到Redis目录(6379的Redis目录)
  3. 在6379根目录执行构建脚本:
redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
复制代码
  1. 在出现 Can I set the above configuration? (type ‘yes’ to accept): 请确定并输入 yes 。成功后的结果如下:

1573701010499.png


测试集群命令

  • 启动客户端:redis-cli –c –h 127.0.0.1 –p 6379 , 可以跟任何节点的端口
  • 查看整个集群:cluster info
  • 查看当前Redis:info replication
  • 查看槽位:cluster nodes

集群代码测试

注意:代码测试前先导入Jedis所需jar包

@Test
public void testCluster() throws IOException, InterruptedException {
    Set<HostAndPort> nodes = new HashSet<>();
    nodes.add(new HostAndPort("127.0.0.1", 6379));
    nodes.add(new HostAndPort("127.0.0.1", 6380));
    nodes.add(new HostAndPort("127.0.0.1", 6381));
    nodes.add(new HostAndPort("127.0.0.1", 6382));
    nodes.add(new HostAndPort("127.0.0.1", 6383));
    nodes.add(new HostAndPort("127.0.0.1", 6384));
    JedisCluster cluster = new JedisCluster(nodes);
    try {
        String res = cluster.get("name");
        System.out.println(res);
    	//cluster.quit();
    } catch (Exception e) {
    	e.printStackTrace();
    	//cluster.quit();
    }
}
复制代码

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享