Redis集群(单机多实例)

Redis介绍

  Redis是一个分布式缓存数据库服务器,提供基于内存访问的缓存服务,而且不管是在单服务器仍是服务器集群上都有着较为灵活方便的扩展能力。
  单个的Redis实例是单进程单线程的,因为这个缘由,Redis对于实例自己不须要考虑种种访问线程共享资源所带来的并发性问题,由于全部的线程访问都是队列顺序执行的。若是须要扩容,须要配置多实例。node

单机多实例配置

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

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进行集群建立和配置
服务器

总结

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

相关文章
相关标签/搜索