Redis集群(单机多实例)

Redis介绍

  Redis是一个分布式缓存数据库服务器,提供基于内存访问的缓存服务,并且无论是在单服务器还是服务器集群上都有着较为灵活方便的扩展能力。
  单个的Redis实例是单进程单线程的,由于这个原因,Redis对于实例本身不需要考虑种种访问线程共享资源所带来的并发性问题,因为所有的线程访问都是队列顺序执行的。如果需要扩容,需要配置多实例。

单机多实例配置

  Redis实例是基于配置文件的,redis-server根据conf生成并运行实例,因此需要多实例的话需要配置对应数目的Conf,

    bind 127.0.0.1    # 绑定主机ip
    port 7001    # 开启的端口
    daemonize yes    # 是否后台运行
    pidfile /var/run/redis_7001.pid    # pid文件名称
    cluster-enabled yes    # 开启集群
    cluster-config-file nodes-7001.conf    # 集群节点文件
    dbfilename dump-7001.rdb    # 数据备份名称

  单机情况下通过监听多端口的方式实现多实例,建立集群时只需要复制多份conf,并根据上面的conf修改对应的端口号即可。
  


  在开启实例后(7001,7002),进入到redis-cli之中

    127.0.0.1:7001> cluster help
     1) CLUSTER <subcommand> arg arg ... arg. Subcommands are:
     2) ADDSLOTS <slot> [slot ...] -- Assign slots to current node.
     3) BUMPEPOCH -- Advance the cluster config epoch.
     4) COUNT-failure-reports <node-id> -- Return number of failure reports for <node-id>.
     5) COUNTKEYSINSLOT <slot> - Return the number of keys in <slot>.
     6) DELSLOTS <slot> [slot ...] -- Delete slots information from current node.
     7) FAILOVER [force|takeover] -- Promote current replica node to being a master.
     8) FORGET <node-id> -- Remove a node from the cluster.
     9) GETKEYSINSLOT <slot> <count> -- Return key names stored by current node in a slot.
    10) FLUSHSLOTS -- Delete current node own slots information.
    11) INFO - Return onformation about the cluster.
    12) KEYSLOT <key> -- Return the hash slot for <key>.
    13) MEET <ip> <port> [bus-port] -- Connect nodes into a working cluster.
    14) MYID -- Return the node id.
    15) NODES -- Return cluster configuration seen by node. Output format:
    16)     <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ... <slot>
    17) REPLICATE <node-id> -- Configure current node as replica to <node-id>.
    18) RESET [hard|soft] -- Reset current node (default: soft).
    19) SET-config-epoch <epoch> - Set config epoch of current node.
    20) SETSLOT <slot> (importing|migrating|stable|node <node-id>) -- Set slot state.
    21) REPLICAS <node-id> -- Return <node-id> replicas.
    22) SLOTS -- Return information about slots range mappings. Each range is made of:
    23)     start, end, master and replicas IP addresses, ports and ids

  使用cluster meet 127.0.0.1 7002与7002实例建立握手连接:
  


  使用cluster nodes查看节点信息:
  

Redis 5.0以上版本不再使用redis-trib.rb

  Redis 5.0以上版本不再使用redis-trib.rb,而是统一使用Cli进行集群创建和配置

![](https://img2018.cnblogs.com/blog/915947/201903/915947-20190325113611458-1397645995.png)

总结

  上面是一个最简单的单机多实例集群搭建过程,事实上Redis还提供了一个很方便的集群搭建脚本redis-trib.rb,利用这个脚本能更轻松地搭建多机器多实例集群。下一篇将介绍如何使用redis-trib.rb来构建多机器多实例的集群。

posted @ 2019-03-22 11:50  hyj2357  阅读(1892)  评论(0编辑  收藏  举报