redis 集群搭建

Redis3.0之后,Redis官方提供了完成的集群解决方案

方案采用去中心化的方案  概念包括:sharding(分区),replication(复制),failover(故障转移)

Redis5.0前采用redis-trib进行集群的创建和管理,需要ruby支持

Redis5.0可以直接使用Redis-cli进行集群的创建和管理

集群架构

1.准备主机

主机名master1:8100master2:8101master3:8100salve1:8102salve2:8103salve3:8102
redis1  
redis2    

2/对redis1 和redis2进行无秘登录

3.安装redis 到/usr/local/redis下

4.拷贝redis.conf文件到/usr/local/redis下

5.修改redis.conf文件

#bing 127.0.0.1  注释掉 本地ip
protected-mode no 关闭保护模式
port 8100 #修改端口
daemonize yes #后台启动
cluster-enabled yes
pidfile /var/run/redis_8100.pid
logfile "redis-8100.log"
dir /data/redis-8100 
dump redis-8100.dump
cluster-enabled yes #开启集群

3.分别启动服务 通过./redis-server redis-*.conf 启动服务

通过ps -ef | grep redis 可以查询到启动的后台reids服务

4.初始化集群信息 通过redis-cli创建集群 设置副本为1

redis-8100/bin/redis-cli --cluster create 192.168.163.110:8100 192.168.163.110:8101 192.168.163.111:8100 192.168.163.110:8103 192.168.163.110:8102 192.168.163.111:8101 --cluster-replicas 1

初始化时,需要yes 统一默认的分配

初始化成功后,可以去redis的nodes.conf文件查看节点的初始化信息 可以看到master 以及相应的salve信息

通过命令行来连接集群 bin/redis-cli -p 8100 -c

可以看到设置键值对的时候,可能出现请求重定向的信息

可以通过 cluster info 查看集群信息

扩容节点 首先准备扩容的服务 ,配置信息遵循上面的配置

bin/redis-cli --cluster add-node 192.168.163.111:8102 192.168.163.111:8100 #添加节点

bin/redis-cli --cluster reshard 192.168.163.111:8102 # 为新的节点分配slot 

需要指定分配slot的个数 已经分配的slot来源,all是对已有的master中平均分slot个新节点

执行bin/redis-cli --cluster add-node 192.168.163.111:8103 192.168.163.111:8102 --cluster-slave --cluster-master-id 0e7208d7d9c2901b34c55d7862fdafa78b31bf7a 进行添加从节点

jedisCluster 操作集群

package com.haozhen;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author haozhen
 * @email haozh@ync1.com
 * @date 2020/11/2  0:59
 */
public class JedisClusterTest {

    public static JedisCluster jedisCluster;

    static {
        Set<HostAndPort> hostAndPorts = new HashSet<>();

        String host1 = "192.168.163.110";
        String host2 ="192.168.163.111";
        hostAndPorts.add(new HostAndPort(host1,8100));
        hostAndPorts.add(new HostAndPort(host1,8101));
        hostAndPorts.add(new HostAndPort(host1,8102));
        hostAndPorts.add(new HostAndPort(host1,8103));
        hostAndPorts.add(new HostAndPort(host2,8100));
        hostAndPorts.add(new HostAndPort(host2,8101));
        hostAndPorts.add(new HostAndPort(host2,8102));
        hostAndPorts.add(new HostAndPort(host2,8103));

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

        jedisPoolConfig.setMaxIdle(100);
        jedisPoolConfig.setMaxTotal(500);
        jedisPoolConfig.setMinIdle(0);
        jedisPoolConfig.setMaxWaitMillis(2000);
        jedisPoolConfig.setTestOnBorrow(true);

        jedisCluster = new JedisCluster(hostAndPorts,jedisPoolConfig);
    }

}
测试添加数据和趋势数据
package com.haozhen;

import org.junit.Test;
import redis.clients.jedis.JedisCluster;

/**
 * @author haozhen
 * @email haozh@ync1.com
 * @date 2020/11/2  1:08
 */
public class MyJedisClusterTest {

    private JedisCluster jedisCluster = JedisClusterTest.jedisCluster;

    @Test
    public void testKey(){
        System.out.println(jedisCluster.exists("redis-cluster:date"));
        jedisCluster.set("redis-cluster:date","20201102");
        System.out.println(jedisCluster.get("redis-cluster:date"));
        System.out.println(jedisCluster.exists("redis-cluster:date"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值