原文鏈接:http://www.codeceo.com/article/redis-cluster-config.html
一、單機版redeis
1.安裝包下載
http://download.redis.io/releases/ 下載redis的壓縮包,並放在/usr/soft文件夾下
2.解壓壓縮包:
tar -zxf redis-3.0.7.tar.gz
3.安裝
這裏安裝redis在/usr/local/redis文件夾中
進入安裝包:cd /usr/soft/redis-3.0.7,執行命令
make PREFIX=/usr/local/redis/ install
安裝成功後
redis.conf是redis的配置文件,redis.conf在redis源碼目錄。注意修改port作爲redis進程的端口,port默認6379。
4.將redis源安裝文件中的redis.conf拷貝到redis的安裝目錄中
cp /usr/soft/redis-3.0.7/redis.conf /usr/local/redis/bin/
5.redis啓動
直接運行./redis-server 是前臺啓動,在關閉運行的窗口後redis也將關閉
爲了關閉窗口後不關閉redis,需要使用後臺啓動
5.1修改redis.conf的daemonize的no爲yes
使用以下命令啓動
./redis-server redis.conf
6.檢測redis是否運行正常
6.1使用 ps -ef|grep redis 查看進程
6.2使用redis的客戶端查看
當輸入ping命令時,返回PONG就表示連接正常
7.通過jedis連接redis單機
需要的依賴
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>
可以使用以下代碼測試
需要注意的是:如果出現遠程客戶端連接不上linux的問題,需要在redis.conf配置文件中註釋 bind=127.0.0.1,protected-mode=no
/**
* jedis測試 單機版
*/
@Test
public void testJedisSingle(){
Jedis jedis = new Jedis("192.168.198.130", 6379);
jedis.set("test", "this i a test");
String str = jedis.get("test");
System.out.println("---:"+str);
//關閉jedis的鏈接
jedis.close();
}
/**
* 使用連接池
*/
@Test
public void testJedisPool(){
JedisPool jedisPool = new JedisPool("192.168.198.130", 6379);
Jedis jedis = jedisPool.getResource();
String str = jedis.get("test");
System.out.println("---:"+str);
jedis.close();
}
jedis與spring整合
添加配置文件applicationContext-jedis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 讀取jedis配置文件; 這裏可以不用配置,-dao已經配置了掃描配置文件 -->
<!-- <context:property-placeholder location="classpath:/properties/*.properties"/> -->
<!-- 連接池配置 -->
<bean id="jedisPoolConfig">
<!-- 最大連接數 -->
<property name="maxTotal" value="30" />
<!-- 最大空閒連接數 -->
<property name="maxIdle" value="10" />
<!-- 每次釋放連接的最大數目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 釋放連接的掃描間隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 連接最小空閒時間 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 連接空閒多久後釋放, 當空閒時間>該值 且 空閒連接>最大空閒連接數 時直接釋放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 獲取連接時的最大等待毫秒數,小於零:阻塞不確定的時間,默認-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在獲取連接的時候檢查有效性, 默認false -->
<property name="testOnBorrow" value="true" />
<!-- 在空閒時檢查有效性, 默認false -->
<property name="testWhileIdle" value="true" />
<!-- 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!--jedis客戶端單機版 -->
<bean id="redisClient">
<constructor-arg name="host" value="${JEDIS_HOST}"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="redisClientDao"></bean> <!--定義自己定義的jedis工具類-->
</beans>
測試
/**
* 結合spring的redis單機版測試
*/
@Test
public void testSpringSingle(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
JedisPool jedisPool = (JedisPool)context.getBean("redisClient");
Jedis jedis = jedisPool.getResource();
jedis.set("key1", "1111");
String str = jedis.get("key1");
System.out.println("--:"+str);
jedis.close();
jedisPool.close();
}
這裏可是自己封裝一個工具類
package com.taotao.rest.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.taotao.rest.dao.JedisClient;
/**
* 在配置文件中註解
*
* @author Administrator
*
*/
public class JedisClientSingle implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String str = jedis.get(key);
jedis.close();
return str;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String str = jedis.set(key, value);
jedis.close();
return str;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String str = jedis.hget(hkey, key);
jedis.close();
return str;
}
@Override
public Long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
return result;
}
@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
return result;
}
@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
return result;
}
}
二、集羣版redis搭建
1.1. 集羣原理
1.1.1. redis-cluster架構圖
架構細節:
(1)所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬.
(2)節點的fail是通過集羣中超過半數的節點檢測失效時才生效.
(3)客戶端與redis節點直連,不需要中間proxy層.客戶端不需要連接集羣所有節點,連接集羣中任何一個可用節點即可
(4)redis-cluster把所有的物理節點映射到[0-16383]slot上,cluster 負責維護node<->slot<->value
Redis 集羣中內置了 16384 個哈希槽,當需要在 Redis 集羣中放置一個 key-value 時,redis 先對 key 使用 crc16 算法算出一個結果,然後把結果對 16384 求餘數,這樣每個 key 都會對應一個編號在 0-16383 之間的哈希槽,redis 會根據節點數量大致均等的將哈希槽映射到不同的節點
1.1.2. redis-cluster投票:容錯
(1)領着投票過程是集羣中所有master參與,如果半數以上master節點與master節點通信超過(cluster-node-timeout),認爲當前master節點掛掉.
(2):什麼時候整個集羣不可用(cluster_state:fail)?
a:如果集羣任意master掛掉,且當前master沒有slave.集羣進入fail狀態,也可以理解成集羣的slot映射[0-16383]不完成時進入fail狀態. ps : redis-3.0.0.rc1加入cluster-require-full-coverage參數,默認關閉,打開集羣兼容部分失敗.
b:如果集羣超過半數以上master掛掉,無論是否有slave集羣進入fail狀態.
ps:當集羣不可用時,所有對集羣的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)錯誤
2.安裝ruby環境
redis集羣管理工具redis-trib.rb依賴ruby環境,首先需要安裝ruby環境:
安裝ruby
yum install ruby
yum install rubygems
安裝ruby和redis的接口程序(需要安裝文件redis-3.0.0.gem)
拷貝redis-3.0.0.gem至/usr/local下
執行:
gem install /usr/local/redis-3.0.0.gem
3.集羣節點的規劃
這裏在同一臺服務器用不同的端口表示不同的redis服務器,如下:
主節點:192.168.198.130:7001 192.168.198.130:7002 192.168.198.130:7003
從節點:192.168.198.130:7004 192.168.198.130:7005 192.168.198.130:7006
在/usr/local下創建redis-cluster目錄,其下創建7001、7002。。7006目錄,如下
將redis安裝目錄bin下的文件拷貝到每個700X目錄內,同時修改每個的redis.conf中的端口爲7001-7006,同時釋放出redis.conf中的註釋的
同時將redis源碼目錄src下的redis-trib.rb拷貝到redis-cluster目錄下
[root@localhost redis-cluster]# cp /usr/soft/redis-3.0.7/src/redis-trib.rb /usr/local/redis-cluster/
使用後臺啓動的方式啓動每個redis
執行redis-trib.rb,此腳本是ruby腳本,它依賴ruby環境
./redis-trib.rb create –replicas 1 192.168.198.130:7001 192.168.198.130:7002 192.168.198.130:7003 192.168.198.130:7004 192.168.198.130:7005 192.168.198.130:7006 //集羣中redis的ip和腳本
啓動成功後如下圖
如果執行時報如下錯誤:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解決方法是刪除生成的配置文件nodes.conf,如果不行則說明現在創建的結點包括了舊集羣的結點信息,需要刪除redis的持久化文件後再重啓redis,比如:appendonly.aof、dump.rdb
使用客戶端進行鏈接測試
其中-c表示要鏈接到集羣 必須使用這個參數
查看集羣狀態信息
4.添加主節點
下面添加7007爲master主節點
修改redis.conf中的端口爲7007,並啓動7007
運行命令
./redis-trib.rb add-node 192.168.198.130:7007 192.168.198.130:7001
運行成功結果
查看情況
5.hash槽重新分配 從上圖我們可以看到7007還沒有分配槽
5.1連接上集羣
./redis-trib.rb reshard 192.168.198.130:7001(連接集羣中任意一個可用結點都行)
5.2第二步:輸入要分配的槽數量
輸入 500表示要分配500個槽
5.3第三步:輸入接收槽的結點id
這裏準備給7007分配槽,通過cluster nodes查看7007結點id爲15b809eadae88955e36bcdbb8144f61bbbaf38fb
輸入:15b809eadae88955e36bcdbb8144f61bbbaf38fb
5.4第四步:輸入源結點id
這裏輸入all
5.5第五步:輸入yes開始移動槽到目標結點id
分配成功後可以查看對應的信息
6.添加從節點
集羣創建成功後可以向集羣中添加節點,下面是添加一個slave從節點。添加7008從結點,將7008作爲7007的從結點。
./redis-trib.rb add-node –slave –master-id 主節點id 添加節點的ip和端口 集羣中已存在節點ip和端口
執行命令(7007的id可以通過連接客戶端使用cluster nodes查看)
./redis-trib.rb add-node –slave –master-id db786026ed607786d6db1aaab94fd73d1ac3c8f4 192.168.198.130:7008 192.168.198.130:7001
注意:如果原來該結點在集羣中的配置信息已經生成cluster-config-file指定的配置文件中(如果cluster-config-file沒有指定則默認爲nodes.conf),這時可能會報錯:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解決方法是刪除生成的配置文件nodes.conf,刪除後再執行./redis-trib.rb add-node指令
查看分配情況
7.刪除節點
./redis-trib.rb del-node 節點的ip:端口 節點id
這是刪除會報錯,需要將該結點佔用的hash槽分配出去(參考hash槽重新分配章節)
8.java測試
@Test
public void testJedisCluster(){
HashSet nodes = new HashSet();
nodes.add(new HostAndPort(“192.168.198.130”, 7001));
nodes.add(new HostAndPort(“192.168.198.130”, 7002));
nodes.add(new HostAndPort(“192.168.198.130”, 7003));
nodes.add(new HostAndPort(“192.168.198.130”, 7004));
nodes.add(new HostAndPort(“192.168.198.130”, 7005));
nodes.add(new HostAndPort(“192.168.198.130”, 7006));
JedisCluster cluster = new JedisCluster(nodes);
cluster.set(“jedisClusterKey”, “hello_world”);
String str = cluster.get(“jedisClusterKey”);
System.out.println(“—:”+str);
//關閉連接
cluster.close();
}
使用spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 讀取jedis配置文件; 這裏可以不用配置,-dao已經配置了掃描配置文件 -->
<!-- <context:property-placeholder location="classpath:/properties/*.properties"/> -->
<!-- 連接池配置 -->
<bean id="jedisPoolConfig">
<!-- 最大連接數 -->
<property name="maxTotal" value="30" />
<!-- 最大空閒連接數 -->
<property name="maxIdle" value="10" />
<!-- 每次釋放連接的最大數目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 釋放連接的掃描間隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 連接最小空閒時間 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 連接空閒多久後釋放, 當空閒時間>該值 且 空閒連接>最大空閒連接數 時直接釋放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 獲取連接時的最大等待毫秒數,小於零:阻塞不確定的時間,默認-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在獲取連接的時候檢查有效性, 默認false -->
<property name="testOnBorrow" value="true" />
<!-- 在空閒時檢查有效性, 默認false -->
<property name="testWhileIdle" value="true" />
<!-- 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<bean id="redisClientClusterDao"></bean>
<!-- redis集羣 -->
<bean id="redisCluster">
<constructor-arg name="nodes">
<set>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean>
<constructor-arg name="host" value="192.168.198.130"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
</beans>
測試代碼
/**
* 結合spring的jedis集羣版
*/
@Test
public void testSpringJedisCluster(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
JedisCluster jedisCluster = (JedisCluster)context.getBean(JedisCluster.class);
jedisCluster.set("key2", "3333");
String str = jedisCluster.get("key2");
System.out.println("--:"+str);
jedisCluster.close();
}
封裝的工具類
package com.taotao.rest.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisCluster;
import com.taotao.rest.dao.JedisClient;
/**
* 集羣不用jedisCluster.close(); 否則下次不能連接
* @author Administrator
*
*/
public class JedisClientCluster implements JedisClient{
@Autowired
private JedisCluster jedisCluster;
@Override
public String get(String key) {
String str = jedisCluster.get(key);
return str;
}
@Override
public String set(String key, String value) {
String str = jedisCluster.set(key, value);
return str;
}
@Override
public String hget(String hkey, String key) {
return jedisCluster.hget(hkey, key);
}
@Override
public Long hset(String hkey, String key, String value) {
return jedisCluster.hset(hkey, key, value);
}
@Override
public long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
}
@Override
public long ttl(String key) {
return jedisCluster.ttl(key);
}
@Override
public long del(String key) {
return jedisCluster.del(key);
}
@Override
public long hdel(String hkey, String key) {
return jedisCluster.hdel(hkey, key);
}
}