redis cluter叢集搭建+springboot整合
Redis Sentinel提供高可用,通過Redis Cluster提供自動分割槽.記錄一下redis 叢集搭建及與springboot整合
Redis Cluster 將所有資料劃分為 16384 的 slots,Cluster預設會對key值使用crc16演算法進行hash得到一個整數值,然後用這個整數值對 16384 進行取模來得到具體槽位. 注:文中的xxxx對應的是IP地址 一.安裝redis 1、yum -y install wget 2、wget http://download.redis.io/releases/redis-4.0.11.tar.gz 3、cd redis-4.0.11 4、make 二.單機多例項 在/redis-4.0.8/目錄下建立資料夾redis-cluster,在redis-cluster資料夾中分別建立6個資料夾 8001-8006,作為三主三從叢集使用,把redis.conf拷貝到8001資料夾中,修改相關配置
port 8001 bind #對應的IP地址 dir /home/java/redis-4.0.11/redis-cluster/8001/ #指定工作目錄,rdb,aof持久化檔案將會放在該目錄下,不同例項一定要配置不同的工作目錄 cluster-enabled yes #啟用叢集模式 cluster-config-file nodes-8001.conf #生成的叢集配置檔名稱,叢集搭建成功後會自動生成,在工作目錄下 cluster-node-timeout 5000 #節點宕機發現時間,可以理解為主節點宕機後從節點升級為主節點時間 appendonly yes #開啟AOF模式 pidfile /var/run/redis_8001.pid #pid file所在目錄
修改好8001的配置檔案,分別拷貝到8002-8006中,對應修改配置檔案中的8001->800*
sed -i 's/8001/8002/g' 8002/redis.conf #正則修改
redis-trib作為搭建叢集使用,及其相關的依賴
yum install ruby
yum install rubygems
gem install redis --version 3.3.3
分別啟動8001-8006的每個例項
[[email protected] redis-4.0.11]# pwd
/home/java/redis-4.0.11
[[email protected] redis-4.0.11]# ./src/redis-server redis-cluster/8001/redis.conf
[ [email protected] redis-4.0.11]# ./src/redis-server redis-cluster/8006/redis.conf
建立叢集: 把xxxx替換成redis-conf中的配置的IP
./src/redis-trib.rb create --replicas 1 xxxx:8001 xxxx:8002 xxxx:8003 xxxx:8004 xxxx:8005 xxxx:8006
[[email protected] redis-4.0.11]# ps -ef|grep redis
root 10013 1132 0 9月18 pts/0 00:03:53 ./redis-server *:6379
root 11735 1 0 03:57 ? 00:01:36 ./src/redis-server xxxx:8002 [cluster]
root 11740 1 0 03:57 ? 00:01:36 ./src/redis-server xxxx:8003 [cluster]
root 11745 1 0 03:57 ? 00:01:37 ./src/redis-server xxxx:8004 [cluster]
root 11750 1 0 03:57 ? 00:01:36 ./src/redis-server xxxx:8005 [cluster]
root 11766 1 0 03:58 ? 00:01:37 ./src/redis-server xxxx:8006 [cluster]
root 12370 1 0 19:44 ? 00:00:07 ./src/redis-server xxxx:8001 [cluster]
root 12417 1132 0 20:30 pts/0 00:00:00 grep --color=auto redis
[[email protected] redis-4.0.11]#
圖中箭頭是主從對應關係,到此為止叢集搭建完畢. set一個值試一下
xxxx:8002> set aa ethan
(error) MOVED 1180 xxxx:8004
xxxx:8002>
MOVED :表示set失敗,ethan經過crc16 對應的槽位不屬於8002,並返回正確的地址xxxx:8004
三.與springboot整合測試 maven配置檔案: 引入redis及web相關包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml
server:
port: 8082
spring:
application:
name: redisCluster
redis:
database: 0
timeout: 10000
pool:
maxIdle: 300
minIdle: 50
maxActive: 1000
cluster:
nodes: xxxx:8001,xxxx:8002,xxxx:8003,xxxx:8004,xxxx:8005,xxxx:8006
配置類
@Configuration
public class ClusterConfigurationProperties {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisCluster getJedisCluster() {
List<String> serverArray = redisProperties.getCluster().getNodes();
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes, redisProperties.getTimeout(),redisProperties.getTimeout());
}
}
測試使用的controller,或者用junit,testng一類的都可以
@Controller
public class TestController {
@Autowired
private ClusterConfigurationProperties clusterConfigurationProperties;
@RequestMapping("/addKey")
@ResponseBody
public String addKey(String key) {
return clusterConfigurationProperties.getJedisCluster().set(key, new Date() + "");
}
}
啟動專案,瀏覽器呼叫,如果調不通,嘗試關閉防火牆
http://localhost:8082/addKey?key=key1
http://localhost:8082/addKey?key=key2
http://localhost:8082/addKey?key=key3
http://localhost:8082/addKey?key=key4
http://localhost:8082/addKey?key=key5
http://localhost:8082/addKey?key=key6
即插入6個值,現在看下值的分佈狀態,檢視master節點的 key值
xxxx:8003> keys *
1) "key4"
xxxx:8003>
[[email protected] redis-4.0.11]# ./src/redis-cli -h xxxx -p 8002
xxxx:8002> keys *
1) "key5"
2) "key1"
xxxx:8002>
[[email protected] redis-4.0.11]# ./src/redis-cli -h xxxx -p 8004
xxxx:8004> keys *
1) "key3"
2) "key2"
3) "key6"
xxxx:8004> cluster nodes
5adf03f87014b067fddd521375368130b090c967 xxxx:[email protected] slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361388617 5 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:[email protected] master - 0 1537361388112 3 connected 10923-16383
2ccb471c40836207c29c896ed1818658e7d2c066 xxxx:[email protected] master - 0 1537361388000 2 connected 5461-10922
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:[email protected] slave ab3f037516244ee7aafacdcd785c00b622c8e389 0 1537361388718 7 connected
ab3f037516244ee7aafacdcd785c00b622c8e389 xxxx:[email protected] myself,master - 0 1537361387000 7 connected 0-5460
4536459b852a142dca441971440b1405a32da8a5 xxxx:[email protected] slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361387000 6 connected
xxxx:8004>
能看到8002.8003.8004為主,8004的從節點是8001,現在kill-9 8004的程序
[[email protected] redis-4.0.11]# ps -ef|grep redis
root 10013 1132 0 9月18 pts/0 00:03:55 ./redis-server *:6379
root 11735 1 0 03:57 ? 00:01:40 ./src/redis-server xxxx:8002 [cluster]
root 11740 1 0 03:57 ? 00:01:40 ./src/redis-server xxxx:8003 [cluster]
root 11745 1 0 03:57 ? 00:01:41 ./src/redis-server xxxx:8004 [cluster]
root 11750 1 0 03:57 ? 00:01:40 ./src/redis-server xxxx:8005 [cluster]
root 11766 1 0 03:58 ? 00:01:41 ./src/redis-server xxxx:8006 [cluster]
root 12370 1 0 19:44 ? 00:00:11 ./src/redis-server xxxx:8001 [cluster]
root 12435 1132 0 20:51 pts/0 00:00:00 grep --color=auto redis
[[email protected] redis-4.0.11]# kill -9 11745
再次檢視叢集狀態
[[email protected] redis-4.0.11]# ./src/redis-cli -h xxxx -p 8004
Could not connect to Redis at xxxx:8004: Connection refused
Could not connect to Redis at xxxx:8004: Connection refused
not connected>
[[email protected] redis-4.0.11]# ./src/redis-cli -h xxxx -p 8001
xxxx:8001> keys *
1) "key3"
2) "key2"
3) "key6"
xxxx:8001> cluster nodes
ab3f037516244ee7aafacdcd785c00b622c8e389 xxxx:[email protected] master,fail - 1537361508018 1537361506000 7 disconnected
4536459b852a142dca441971440b1405a32da8a5 xxxx:[email protected] slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361601653 6 connected
2ccb471c40836207c29c896ed1818658e7d2c066 xxxx:[email protected] master - 0 1537361601000 2 connected 5461-10922
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:[email protected] myself,master - 0 1537361599000 9 connected 0-5460
5adf03f87014b067fddd521375368130b090c967 xxxx:[email protected] slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361601145 5 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:[email protected] master - 0 1537361600000 3 connected 10923-16383
xxxx:8001>
8004已宕,8001已成master,此時啟動8004
[[email protected] redis-4.0.11]# ./src/redis-server redis-cluster/8004/redis.conf
12439:C 19 Sep 20:54:53.391 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12439:C 19 Sep 20:54:53.391 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=12439, just started
12439:C 19 Sep 20:54:53.391 # Configuration loaded
[[email protected] redis-4.0.11]# ./src/redis-cli -h xxxx -p 8004
xxxx:8004> keys *
1) "key2"
2) "key6"
3) "key3"
xxxx:8004> cluster nodes
2ccb471c40836207c29c896ed1818658e7d2c066 xxxx:[email protected] master - 0 1537361705000 2 connected 5461-10922
ab3f037516244ee7aafacdcd785c00b622c8e389 xxxx:[email protected] myself,slave f42919ab17239d579c0a998197d4903ca7a77101 0 1537361704000 7 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:[email protected] master - 0 1537361705501 3 connected 10923-16383
4536459b852a142dca441971440b1405a32da8a5 xxxx:[email protected] slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361706990 6 connected
5adf03f87014b067fddd521375368130b090c967 xxxx:[email protected] slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361705501 5 connected
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:[email protected] master - 0 1537361706568 9 connected 0-5460
xxxx:8004>
8004成了slave,即主從切換完成,對應的key也完成同步.