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的每个实例

[root@localhost redis-4.0.11]# pwd
/home/java/redis-4.0.11
[root@localhost redis-4.0.11]# ./src/redis-server redis-cluster/8001/redis.conf 
[root@localhost 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
[root@localhost 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
[root@localhost 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 JedisCluster jedisCluster;

    @RequestMapping("/addKey")
    @ResponseBody
    public String addKey(String key) {
        return jedisCluster.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> 
[root@localhost redis-4.0.11]# ./src/redis-cli -h xxxx -p 8002
xxxx:8002> keys *
1) "key5"
2) "key1"
xxxx:8002> 
[root@localhost 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:8005@18005 slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361388617 5 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:8003@18003 master - 0 1537361388112 3 connected 10923-16383
2ccb471c40836207c29c896ed1818658e7d2c066 xxxx:8002@18002 master - 0 1537361388000 2 connected 5461-10922
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:8001@18001 slave ab3f037516244ee7aafacdcd785c00b622c8e389 0 1537361388718 7 connected
ab3f037516244ee7aafacdcd785c00b622c8e389 xxxx:8004@18004 myself,master - 0 1537361387000 7 connected 0-5460
4536459b852a142dca441971440b1405a32da8a5 xxxx:8006@18006 slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361387000 6 connected
xxxx:8004> 

能看到8002.8003.8004为主,8004的从节点是8001,现在kill-9 8004的进程

[root@localhost 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
[root@localhost redis-4.0.11]# kill -9 11745

再次查看集群状态

[root@localhost 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> 
[root@localhost 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:8004@18004 master,fail - 1537361508018 1537361506000 7 disconnected
4536459b852a142dca441971440b1405a32da8a5 xxxx:8006@18006 slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361601653 6 connected
2ccb471c40836207c29c896ed1818658e7d2c066 xxxx:8002@18002 master - 0 1537361601000 2 connected 5461-10922
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:8001@18001 myself,master - 0 1537361599000 9 connected 0-5460
5adf03f87014b067fddd521375368130b090c967 xxxx:8005@18005 slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361601145 5 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:8003@18003 master - 0 1537361600000 3 connected 10923-16383
xxxx:8001> 

8004已宕,8001已成master,此时启动8004

[root@localhost 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
[root@localhost 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:8002@18002 master - 0 1537361705000 2 connected 5461-10922
ab3f037516244ee7aafacdcd785c00b622c8e389 xxxx:8004@18004 myself,slave f42919ab17239d579c0a998197d4903ca7a77101 0 1537361704000 7 connected
bdf945b89045d249f32df2c53f122e32f375fa24 xxxx:8003@18003 master - 0 1537361705501 3 connected 10923-16383
4536459b852a142dca441971440b1405a32da8a5 xxxx:8006@18006 slave bdf945b89045d249f32df2c53f122e32f375fa24 0 1537361706990 6 connected
5adf03f87014b067fddd521375368130b090c967 xxxx:8005@18005 slave 2ccb471c40836207c29c896ed1818658e7d2c066 0 1537361705501 5 connected
f42919ab17239d579c0a998197d4903ca7a77101 xxxx:8001@18001 master - 0 1537361706568 9 connected 0-5460
xxxx:8004> 

8004成了slave,即主从切换完成,对应的key也完成同步.

猜你喜欢

转载自blog.csdn.net/qq_29312259/article/details/82770442