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來構建多機器多實例的集群。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM