Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Twemproxy (Nutcracker) 安裝與配置完整實戰記錄:解決 Redis 多集群代理問題

編輯:Linux 系統常見問題解答

背景:為什麼需要 Twemproxy?

Redis Cluster 解決了分片與高可用,但獨立的多個 Redis Cluster 之間無法共享資料。業務增長導致建立多個獨立集群時,客戶端需維護多組連線資訊,複雜度大增。Twemproxy (又稱 Nutcracker) 作為輕量級 Redis 代理層,採用一致性哈希分發請求,對客戶端呈現單一入口,完美解決多集群統一接入問題。

安裝環境與依賴套件

OS: RedHat 6.3 | 需自行編譯安裝較新版本的建構工具:

  • autoconf 2.69+
  • automake 1.12+
  • libtool 2.2+

編譯安裝步驟 (順序不可錯誤)

# 1. 移除系統舊版 autoconf
yum remove autoconf

# 2. 依序編譯安裝建構工具
tar -xf autoconf-2.69.tar.gz
cd autoconf-2.69 && ./configure && make && make install

tar -xf automake-1.12.1.tar.gz
cd automake-1.12.1 && ./configure && make && make install

tar -xf libtool-2.2.4.tar.gz
cd libtool-2.2.4 && ./configure && make && make install

# 3. 安裝 Twemproxy
unzip twemproxy-master.zip
cd twemproxy-master
autoreconf -ivf      # 關鍵步驟:生成 configure 腳本
./configure
make && make install

常見編譯錯誤與解決方案

錯誤訊息原因解決方法
./configure: command not found未執行 autoreconf先執行 autoreconf -ivf
Can't exec "automake": No such file缺少 automake安裝 automake 後再執行 autoreconf
undefined macro: AC_PROG_LIBTOOL缺少 libtool安裝 libtool 後再執行 autoreconf

基本配置範例 (nutcracker.yml)

alpha:
  listen: 0.0.0.0:22121
  hash: fnv1a_64
  hash_tag: "{}"
  distribution: ketama
  auto_eject_hosts: true
  redis: true
  servers:
   - 192.168.1.10:6379:1 server1
   - 192.168.1.11:6379:1 server2
   - 192.168.1.12:6379:1 server3

啟動與驗證

nutcracker -d -c nutcracker.yml -o nutcracker.log -v 11
# -d: 背景執行, -c: 設定檔, -o: 日誌, -v: 詳細度

# 驗證連線
redis-cli -p 22121 info

運維建議

  • 監控 Twemproxy 狀態端點 (預設 :22222) 獲取即時統計
  • 配合 Keepalived/VIP 實現 Twemproxy 自身高可用
  • 生產環境建議調整 client_connections、server_retry_timeout 等參數
Copyright © Windows教程網 All Rights Reserved