1. 程式人生 > >Redis 高可用: twemproxy實現快取伺服器分片叢集

Redis 高可用: twemproxy實現快取伺服器分片叢集

Twemproxy 又稱 nutcracker ,是一個memcache、redis協議的輕量級代理,一個用於sharding 的中介軟體。有了Twemproxy,客戶端不直接訪問Redis伺服器,而是通過twemproxy 代理中介軟體間接訪問。 twemproxy 為 twitter 開源產品(參考:https://github.com/twitter/twemproxy

一個簡單的 Twemproxy 分片測試:


本人測試在一臺伺服器上,啟動3個redis例項。

本機已經安裝了一個redis 例項(192.168.1.222 6379),現在再開啟兩個例項。

# 建立新的 redis 目錄


[[email protected] ~]# mkdir -p /var/lib/redis/6380 /var/lib/redis/6381

# 複製2份新的配置檔案
[[email protected] ~]# cp /etc/redis/6379.conf /etc/redis/6380.conf
[[email protected] ~]# cp /etc/redis/6379.conf /etc/redis/6381.conf

# 分別修改配置埠及檔案位置(或者批量替換修改 %s#6379#6380#g  %s#6379#6381#g)
[[email protected] ~]# vi /etc/redis/6380.conf
port 6380
pidfile "/var/run/redis_6380.pid"
logfile "/var/log/redis_6380.log"
dir "/var/lib/redis/6380"
daemonize yes
#requirepass
#masterauth

[
[email protected]
~]# vi /etc/redis/6381.conf port 6381 pidfile "/var/run/redis_6381.pid" logfile "/var/log/redis_6381.log" dir "/var/lib/redis/6381" daemonize yes #requirepass #masterauth

# 啟動服務(第一個為預設埠 6379)
[[email protected] ~]# service redisd start
[[email protected] ~]# /usr/local/bin/redis-server /etc/redis/6380.conf
[
[email protected]
~]# /usr/local/bin/redis-server /etc/redis/6381.conf

# 訪問例項(正常)
[[email protected] ~]# redis-cli -p 6379
[[email protected] ~]# redis-cli -p 6380
[[email protected] ~]# redis-cli -p 6381


# 安裝 autoconf

[[email protected] ~]# mkdir -p /usr/local/autoconf
[[email protected] ~]# cd /usr/local/src
[[email protected] ~]# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
[[email protected] ~]# tar -zxvf autoconf-2.69.tar.gz
[[email protected] ~]# cd autoconf-2.69
[[email protected] ~]# ./configure --prefix=/usr/local/autoconf/
[[email protected] ~]# make && make install

# 更改新版本 autoconf 2.69 為預設程式
[[email protected] ~]# autoconf -V
[[email protected] ~]# autoreconf -V
[[email protected] ~]# /usr/local/autoconf/bin/autoconf -V
[[email protected] ~]# /usr/local/autoconf/bin/autoreconf -V
[[email protected] ~]# mv /usr/bin/autoconf /usr/bin/autoconf_backup
[[email protected] ~]# mv /usr/bin/autoreconf /usr/bin/autoreconf_backup
[[email protected] ~]# ln -s /usr/local/autoconf/bin/autoconf /usr/bin/autoconf
[[email protected] ~]# ln -s /usr/local/autoconf/bin/autoreconf /usr/bin/autoreconf

# 安裝 twemproxy
[[email protected] ~]# mkdir -p /usr/local/twemproxy
[[email protected] ~]# cd /usr/local/src
[[email protected] ~]# wget https://github.com/twitter/twemproxy/archive/master.zip
[[email protected] ~]# unzip master
[[email protected] ~]# cd /usr/local/src/twemproxy-master
[[email protected] ~]# autoreconf -fvi
[[email protected] ~]# ./configure --prefix=/usr/local/twemproxy/
[[email protected] ~]# make && make install

# 配置環境變數
[[email protected] ~]# echo "PATH=$PATH:/usr/local/twemproxy/sbin/" >> /etc/profile
[[email protected] ~]# source /etc/profile

# 配置檔案 nutcracker.yml
[[email protected] ~]# mkdir -p /etc/nutcracker
[[email protected] ~]# cp /usr/local/src/twemproxy-master/conf/nutcracker.yml /etc/nutcracker/
[[email protected] ~]# vi /etc/nutcracker/nutcracker.yml

alpha:
  listen: 0.0.0.0:22121			#twemproxy監聽的埠
  hash: fnv1a_64				#md5/crc16/crc32/crc32a/fnv1_64/fnv1a_64/fnv1_32/fnv1a_32/hsieh/murmur/jenkins
  distribution: ketama			#ketama/modula/random
  auto_eject_hosts: true		#自動臨時剔除不可用節點
  redis: true					#監控是否為redis
  server_retry_timeout: 10000	#剔除節點嘗試恢復時間間隔(毫秒)
  server_failure_limit: 2		#節點訪問失敗N次則被自動剔除
  servers:						#(IP:埠號:權重)
   - 192.168.1.222:6379:1
   - 192.168.1.222:6380:1
   - 192.168.1.222:6381:1

# 測試配置檔案
[[email protected] ~]# nutcracker -t -c /etc/nutcracker/nutcracker.yml
nutcracker: configuration file '/etc/nutcracker/nutcracker.yml' syntax is ok

# 後臺執行,配置完成!
[[email protected] ~]# nutcracker -d -c /etc/nutcracker/nutcracker.yml

#訪問 twemproxy 節點,並設定 3 個鍵值
[[email protected] ~]# redis-cli -p 22121
127.0.0.1:22121> 
127.0.0.1:22121> set name aaaaa
OK
127.0.0.1:22121> set age 33
OK
127.0.0.1:22121> set value 100000
OK

# 此時分別連線3個例項,檢視鍵值情況:
[[email protected] ~]# redis-cli -p 6379
127.0.0.1:6379> keys *
1) "age"

[[email protected] ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "name"

[[email protected] ~]# redis-cli -p 6381
127.0.0.1:6381> keys *
1) "value"

可以看到,代理節點 22121 將資料水平劃分到了每個節點。

測試結果如下圖:


參考: