http://blog.csdn.net/gaogaoshan/article/details/41043685 html
本文基於redis-2.8.10和jedis2.4.2版本。java
redis 2.8以後的版本 開始支持Sentinel監聽客戶端功能。使用1個或多個的Sentinel做爲一個監聽的集羣。redis
後端N個(master+slave)組成一個redis集羣。spring
Sentinel經過檢測Redis的master實例是否存活,並在Redis master實例發生故障時,將Redis master的slave提高爲master,後端
並在老的master從新加入到redis sentinel的羣集以後,會被從新配置,做爲新master的slave。tcp
這意味着基於redis sentinel的HA羣集是可以自我管理的!測試
應用經過sentinel能夠動態的獲得當前master的鏈接,而不是直接在應該中配置寫死的master的IP和端口。spa
這樣就避免了master,slave切換後應用配置的鏈接不可用的狀況。.net
[html] view plaincopy日誌
http://download.redis.io/releases/redis-2.8.17.tar.gz
1.安裝redis:
tar zxvf redis-2.8.17.tar.gz (要先安裝tcl)
cd redis-2.8.8
(查看Liunx版本方式:getconf LONG_BIT)
若是是32位:make CFLAGS="-march=i686"
若是是64位:make
要是編譯失敗(make clean)
cd src
make test
make install
[html] view plaincopy
mkdir -p /opt/portal/redis/bin
mkdir -p /opt/portal/redis/conf
mkdir -p /opt/portal/redis/logs
cd /opt/portal/redis/redis-2.8.8
cp redis.conf sentinel.conf /opt/portal/redis/conf
cd /opt/portal/redis/redis-2.8.8/src
cp redis-server redis-cli redis-sentinel redis-benchmark mkreleasehdr.sh redis-check-aof redis-check-dump /opt/portal/redis/bin
設置別名
vi /etc/profile
alias redis-server='/opt/portal/redis/bin/redis-server /opt/portal/redis/conf/redis.conf'
alias redis-cli='/opt/portal/redis/bin/redis-cli'
使配置文件生效
. /etc/profile
slave 的 redis 的redis.conf須要配置master的IP和端口# slaveof <masterip> <masterport>
Sentinel官方文檔:官網:http://redis.io/topics/sentinel
[html] view plaincopy
<span style="font-size:24px;">sentinel.conf主要有6個配置項</span>
<span style="color:#ff0000;">port 26329
sentinel monitor mymaster 192.168.14.191 6379 2</span>
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel notification-script <master-name> <script-path>
port: 指定sentinel的偵聽端口(即與redis server或client創建tcp鏈接的端口)
monitor: 指定sentinel要monitor的redis實例,包括一個redis實例的別名(alias)及redis實例的ip+port,<span style="color:#006600;">該行最後的數字2表示至少2個setinel實例同時檢測到redis server異常時,纔將redis server的狀態判決爲real fail。也即,若這裏配置爲2,但實際部署中sentinel只部署了1套,則即便redis實例已經掛掉,sentinel也不會給出任何警告。這一點須要特別引發注意。</span>
down-after-milliseconds: 指定sentinel監控到redis實例持續異常多長時間後,會判決其狀態爲down。若實際業務須要sentinel儘快判決出redis實例異常,則可適當配小。
failover-timeout: 若sentinel在該配置值內未能完成failover操做(即故障時master/slave自動切換),則認爲本次failover失敗。該配置有4個用途,具體可參考sentinel.conf中的說明,限於篇幅,此處再也不贅述。
parallel-syncs: 指定failover過程當中,同時被sentinel reconfigure的最大slave實例數。因爲reconfigure過程當中,對應的slave會中斷響應客戶端請求,故爲避免全部的slave同時不可用,該值需適當配小。
notification-script: 指定sentinel檢測到master-name指向的實例異常時,調用的報警腳本。該配置項可選,但線上系統建議配置。
191,192上面各部署一個Sentinel監控同一個master。
192部署master,191部署slave(redis.conf 中配置master的地址 slave of 192.168.11.190 6379)
啓動redis,Sentinel
[html] view plaincopy
nohup ./bin/redis-sentinel ./conf/sentinel.conf> ./logs/redis-sentinel.log 2>&1&
redis-cli info 能夠查看當前redis實例的基本信息
redis-cli -p 26379 鏈接redis-sentinel客戶端 info 查看master地址,有幾個slave,有幾個監控
動態添加須要監控的master
192.168.14.192:26381> sentinel monitor mymaster 192.168.14.193 6379 2
查看一個指定的master有那些slaves:
192.168.14.192:26379> sentinel slaves mymaster
[html] view plaincopy
<span style="color:#3333ff;">192關閉master: redis-cli下shutdown </span>
在redis-sentinel.log中
+sdown master mymaster 192.168.14.192 6379 監控到192down了
+vote-for-leader bccb0d2d048a9a8497f87137a856add2ff57bceb 1 選舉新的master
+switch-master mymaster 192.168.14.192 6379 192.168.14.191 6379 將原來的master192 換成191
+slave slave 192.168.14.192:6379 192.168.14.192 6379 @ mymaster 192.168.14.191 6379 192變成了191的slave
redis-cli info Replication <span style="color:#3333ff;">191 變成role:master</span>
<span style="color:#3333ff;">從新開啓 192
redis-cli info Replication 192 role:slave</span>
<span style="color:#3333ff;">再把191 shutdown 192 又變回master了</span>
[html] view plaincopy
<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.14.192"></constructor-arg>
<constructor-arg name="port" value="26379"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.14.191"></constructor-arg>
<constructor-arg name="port" value="26379"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
配置文件要懶加載,java-dao 要用getBean獲取redisTemplate,getBean以前還要用Jedis jedis = new Jedis(ip, port);測試,否則redis鏈接不上,
[html] view plaincopy
Sentinel會死循環的不斷鏈接
sentinelConfiguration配置監聽的列表
connectionFactory:從監聽中獲取當前的master
redisTemplate:到connectionFactory獲取鏈接
[html] view plaincopy
Long listFormRedis=redisTemplate.opsForList().size("login_record_list");
String hostName=connectionFactory.getSentinelConnection().masters().iterator().next().getHost();
System.out.println(listFormRedis);
System.out.println(hostName);
master切換的時候應用的日誌:
[html] view plaincopy
It seems like server has closed the connection.; nested exception
2014-11-10 16:57:46 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 192.168.14.191:6379