美文网首页Redis
linux环境下Redis集群搭建

linux环境下Redis集群搭建

作者: MrRobot_ | 来源:发表于2019-02-18 12:38 被阅读52次

    Redis的安装

    Redis是c语言开发的。
    安装redis需要c语言的编译环境。如果没有gcc需要在线安装。yum install gcc-c++

    安装步骤:

    第一步:redis的源码包上传到linux系统。
    第二步:解压缩redis。
    第三步:编译。进入redis源码目录。make
    第四步:安装。make install PREFIX=/usr/local/redis
    PREFIX参数指定redis的安装目录。一般软件安装到/usr目录下

    redis的启动:

    前端启动:在redis的安装目录下直接启动redis-server
    [root@localhost bin]# ./redis-server


    前台启动

    后台启动:
    把/root/redis-3.0.0/redis.conf复制到/usr/local/redis/bin目录下
    [root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
    修改配置文件:


    把no改为yes表示后台启动

    [root@localhost bin]# ./redis-server redis.conf

    查看redis进程:

    [root@localhost bin]# ps aux|grep redis


    后台启动了

    关闭redis服务:

    [root@localhost bin]# ./redis-cli shutdown


    关闭redis

    4.Redis的持久化方案

    Redis的所有数据都是保存到内存中的。
    ①Rdb:快照形式,定期把内存中当前时刻的数据保存到磁盘。Redis默认支持的持久化方案。
    ②aof形式:append only file。把所有对redis数据库操作的命令,增删改操作的命令。保存到文件中。数据库恢复时把所有的命令执行一遍即可。
    在redis.conf配置文件中配置。

    Rdb(保存在磁盘上,时间段内操作次数决定同步次数,性能高,可能会丢数据):

    rdb

    Aof的配置(默认不开启,每秒钟同步一次,性能低):


    aof

    Redis集群的搭建

    redis-cluster架构图

    客户端连接到集群中的任意一个节点

    redis-cluster投票:容错机制

    投票机制,半数以上投票挂掉才挂掉,集群中必须保持三个节点以上

    架构细节:

    (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
    (2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
    (3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
    (4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
    Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点


    槽的作用是计算这个key对应哪个槽,然后找到在这个槽所对应的这台服务器,把这个key放在那台服务器上

    搭建步骤:

    ①创建一个redis-cluster目录用来存放多个redis实例



    ②修改redis.conf下的端口号


    修改端口
    配置集群环境必须把cluster-enabled的注释打开

    ③需要6个redis实例。需要运行在不同的端口7001-7006


    ④为了方便启动和关闭六个redis,可以写两个个批处理文件
    创建开启集群的批处理:vim start-all.sh

    cd redis01
    ./redis-server redis.conf
    cd ..
    cd redis02
    ./redis-server redis.conf
    cd ..
    cd redis03
    ./redis-server redis.conf
    cd ..
    cd redis04
    ./redis-server redis.conf
    cd ..
    cd redis05
    ./redis-server redis.conf
    cd ..
    cd redis06
    ./redis-server redis.conf
    cd ..
    

    创建关闭集群的批处理:vim shutdow-all.sh

    redis01/redis-cli -p 7001 shutdown
    redis01/redis-cli -p 7002 shutdown
    redis01/redis-cli -p 7003 shutdown
    redis01/redis-cli -p 7004 shutdown
    redis01/redis-cli -p 7005 shutdown
    redis01/redis-cli -p 7006 shutdown
    
    去redis源代码复制一个ruby的脚本语言到redis-cluster下

    ruby脚本需要环境才能运行,所以要安装它的环境

    安装ruby

    • yum install ruby
    • yum install rubygems


      需要的ruby的第三方库

      执行命令:

    • gem install redis-3.0.0.gem


    使用ruby脚本搭建集群

    执行命令:

    ./redis-trib.rb create --replicas 1 192.168.25.137:7001 192.168.25.137:7002 192.168.25.137:7003 192.168.25.137:7004 192.168.25.137:7005 192.168.25.137:7006
    

    自动分配节点和备用节点以及哈希槽的分配

    Redis-cli连接集群

    [root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c
    -c:代表连接的是redis集群




    Jedis

    需要把jedis依赖的jar包添加到工程中。Maven工程中需要把jedis的坐标添加到依赖
    推荐添加到服务层。E3-content-Service工程中。

    • 连接单机版
      第一步:创建一个Jedis对象。需要指定服务端的ip及端口。
      第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。
      第三步:打印结果。
      第四步:关闭Jedis
    @Test
        public void testJedis() throws Exception {
            // 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。
            Jedis jedis = new Jedis("192.168.25.137", 6379);
            // 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。
            String result = jedis.get("hello");
            // 第三步:打印结果。
            System.out.println(result);
            // 第四步:关闭Jedis
            jedis.close();
        }
    
    • 连接单机版使用连接池
      第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。
      第二步:从JedisPool中获得Jedis对象。
      第三步:使用Jedis操作redis服务器。
      第四步:操作完毕后关闭jedis对象,连接池回收资源。
      第五步:关闭JedisPool对象。
    @Test
        public void testJedisPool() throws Exception {
            //创建一个连接池对象,两个参数host、port
            JedisPool jedisPool = new JedisPool("192.168.25.137", 6379);
            //从连接池获得一个连接,就是一个jedis对象。
            Jedis jedis = jedisPool.getResource();
            //使用jedis操作redis
            String string = jedis.get("test123");
            System.out.println(string);
            //关闭连接,每次使用完毕后关闭连接。连接池回收资源。
            jedis.close();
            //关闭连接池。
            jedisPool.close();
        }
    
    • 连接集群版
      第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。
      第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
      第三步:打印结果
      第四步:系统关闭前,关闭JedisCluster对象。
    @Test
        public void testJedisCluster() throws Exception {
            //创建一个JedisCluster对象。有一个参数nodes是一个set类型。set中包含若干个HostAndPort对象。
            Set<HostAndPort> nodes = new HashSet<>();
            nodes.add(new HostAndPort("192.168.25.137", 7001));
            nodes.add(new HostAndPort("192.168.25.137", 7002));
            nodes.add(new HostAndPort("192.168.25.137", 7003));
            nodes.add(new HostAndPort("192.168.25.137", 7004));
            nodes.add(new HostAndPort("192.168.25.137", 7005));
            nodes.add(new HostAndPort("192.168.25.137", 7006));
            JedisCluster jedisCluster = new JedisCluster(nodes);
            //直接使用JedisCluster对象操作redis。
            jedisCluster.set("test", "123");
            String string = jedisCluster.get("test");
            System.out.println(string);
            //关闭JedisCluster对象
            jedisCluster.close();
        }
    

    相关文章

      网友评论

        本文标题:linux环境下Redis集群搭建

        本文链接:https://www.haomeiwen.com/subject/ltwgeqtx.html