1.下载Redis-x64-3.2.100.zip

下载网址:https://github.com/microsoftarchive/redis

百度网盘下载:百度网盘 请输入提取码 提取码:j2js

redis版本一定要大于3.0,原因:windows暂不支持redis-cli创建集群,只能使用redis-trib.rb,redis-trib.rb是redis官方推出的管理redis集群的工具,需要redis版本>=3.0.6

 

2.解压Redis-x64-3.2.100.zip重命名为7000,修改redis.windows.conf配置文件

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

3.复制7000文件夹5份,分别命名为7001到7005

修改redis.windows.conf配置文件,修改各自的port和cluster-config-file,如:

port 7001
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 15000

 4.启动redis

可以通过脚本启动

@echo off
title redis-server0
set ENV_HOME="D:\program\redis-cluster\7000"
D:
color 0a
cd %ENV_HOME%
.\redis-server.exe .\redis.windows.conf
exit

5.安装ruby

参考:windows 安装 Ruby 教程-百度经验

 我安装ruby会报连接超时的问题,但并不影响

 执行到这一步就不用管了

安装成功后,查看ruby版本,通过gem install redis来安装相关依赖 

6.下载redis-trib.rb

地址:https://github.com/beebol/redis-trib.rb

7.创建集群

三主三从

.\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. 则成功

8.java代码

public static void main(String[] args) {
        //set 集合,用来装集群的ip 和端口的
        Set<HostAndPort> nodes = new HashSet<>();
        //通过for循环把集群的主机信息装到集合
        nodes.add(new HostAndPort("127.0.0.1",7000));
        nodes.add(new HostAndPort("127.0.0.1",7001));
        nodes.add(new HostAndPort("127.0.0.1",7002));
        //创建JedisCluster
//        JedisCluster cluster = new JedisCluster(nodes);

        JedisPoolConfig config = new JedisPoolConfig();
        config .setMaxTotal(500);
        config .setMinIdle(2);
        config .setMaxIdle(500);
        config .setMaxWaitMillis(10000);
        config .setTestOnBorrow(true);
        config .setTestOnReturn(true);
//        JedisCluster cluster = new JedisCluster(nodes, 10000, 10000, 100, "123456", config);
        JedisCluster cluster = new JedisCluster(nodes);
        //set k v
        cluster.set("cluster_name","et2106");
        //hset k field value
        cluster.hset("cluster_user:1","id","1");
        cluster.hset("cluster_user:1","x","x");
        //sadd
        cluster.sadd("cluster_set","a","b","c");
        System.out.println(cluster.get("cluster_name"));
        cluster.hgetAll("cluster_user:1").forEach((k,v)->     
        System.out.println(k+":"+v));
        cluster.smembers("cluster_set").forEach(x-> System.out.println(x));
    }

下面是JedisCluster密码版:

JedisCluster cluster = new JedisCluster(nodes, 10000, 10000, 100, "密码", config);

开启密码,需要修复配置文件:

masterauth foobared
requirepass foobared

 开启密码后重新启动redis,会打印如下日志,不影响

 

 9.Another Redis Desktop Manager可视化工具连接集群,查看数据

 创建一个连接即可,127.0.0.1@7000和127.0.0.1@7001和127.0.0.1@7002都是为集群查看连接,下面则为连接成功:

 查看数据:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐