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修改對應的端口號便可。
數據庫
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實例創建握手鍊接:
緩存
Redis 5.0以上版本再也不使用redis-trib.rb,而是統一使用Cli進行集羣建立和配置
服務器
上面是一個最簡單的單機多實例集羣搭建過程,事實上Redis還提供了一個很方便的集羣搭建腳本redis-trib.rb,利用這個腳本能更輕鬆地搭建多機器多實例集羣。下一篇將介紹如何使用redis-trib.rb來構建多機器多實例的集羣。併發