5 微服务实战系列 - SpringBoot redis cluster实战


> **redis3.2+升级cluster功能:**
> 1主从模式:主从分片,为每一个master节点创建replica,主节点宕机,从节点升级为主节点,保证服务延续性
> 2负载均衡:集群根据流量redict to node
> 3读写分离:主节点写操作,从节点备份读操作
> 4ruby脚本简单创建集群部署

1 基础环境预览

机器 111.231.112.x:7000/7001/7002/7003/7004/7005
Redis redis-3.2.11
Tcl tcl.8.6.6

2 环境安装

  • 2.1 必备tcl
cd /usr/local/software/
tar -zxvf tcl8.6.6-src.tar.gz
cd tcl8.6.6 && ./unix/config
cd tcl.8.6.6 && make && make install
  • 2.2 必备redis
tar -zxvf redis-3.2.11.tar.gz 
cd redis-3.2.11 && make
cd src && make test

2.3 集群搭建

  • 2.3.1基础集群
mkdri redis-cluster
cd redis-cluster
mkdir 7000/7001/7002/7003/7004/7005
cp ../redis.conf ./7000/7001/7002/7003/7004/7005
**集群配置**
Vim redis.conf
port 7000 #7001/7002/7003/7004/7005
# bind 127.0.0.1 ::1
protect-mode no
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
appendonly  yes 
**启动集群**
../../src/redis-server redis.conf
  • 2.3.2 集群负载主从搭建
安装ruby
yum -y install ruby ruby-devel rubygems rpm-build
安装gem-redis
gem install redis-3.2.2.gem
启动集群
redis-trib.rb  create  --replicas  1  111.231.112.x:7000 111.231.112.x:7001  111.231.112.x:7002 111.231.112.x:7003  111.231.112.x:7004  111.231.112.x:7005

集群输出关键信息:

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
111.231.112.x:7000
111.231.112.x:7001
111.231.112.x:7002
Adding replica 111.231.112.x:7003 to 111.231.112.x:7000
Adding replica 111.231.112.x:7004 to 111.231.112.x:7001
Adding replica 111.231.112.x:7005 to 111.231.112.x:7002
M: aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26 111.231.112.151:7000
   slots:0-5460 (5461 slots) master
M: d5a4e5e770d8e5aec777724730057ccce65df475 111.231.112.151:7001
   slots:5461-10922 (5462 slots) master
M: 563aaa218a4b1e817c9cf3e34582170026f55406 111.231.112.151:7002
   slots:10923-16383 (5461 slots) master
S: 1f281de8be5959436bb2112549cc4fffb16192b5 111.231.112.151:7003
   replicates aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26
S: 99366e6d950c6d9eb923ff1fa089217b24b489ad 111.231.112.151:7004
   replicates d5a4e5e770d8e5aec777724730057ccce65df475
S: 0c411cd0b12297128c87f7542e67705777218654 111.231.112.151:7005
   replicates 563aaa218a4b1e817c9cf3e34582170026f55406
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 111.231.112.151:7000)
M: aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26 111.231.112.151:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 99366e6d950c6d9eb923ff1fa089217b24b489ad 111.231.112.151:7004
   slots: (0 slots) slave
   replicates d5a4e5e770d8e5aec777724730057ccce65df475
S: 0c411cd0b12297128c87f7542e67705777218654 111.231.112.151:7005
   slots: (0 slots) slave
   replicates 563aaa218a4b1e817c9cf3e34582170026f55406
M: d5a4e5e770d8e5aec777724730057ccce65df475 111.231.112.151:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 563aaa218a4b1e817c9cf3e34582170026f55406 111.231.112.151:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 1f281de8be5959436bb2112549cc4fffb16192b5 111.231.112.151:7003
   slots: (0 slots) slave
   replicates aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群测试:

Master 7000 
[root@VM_0_7_centos ~]# redis-cli -c -p 7000
127.0.0.1:7000> set y dsfsdfsdf
-> Redirected to slot [12222] located at 111.231.112.151:7002

Master 7002
[root@VM_0_7_centos ~]# redis-cli -h 127.0.0.1 -c -p 7002
127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 111.231.112.151:7000
OK
Salve 7004
[root@VM_0_7_centos ~]# redis-cli -c -p 7004
127.0.0.1:7004> set k dsadfadsfa
-> Redirected to slot [7629] located at 111.231.112.151:7001
OK

总结:

  • 1)7000 master->7003 replica,7001 master->7004 replica,7002
    master->7005 slave
  • 2)节点负载均衡,主写操作,从读操作

- 3 springBoot redis cluster

  • 3.1 application.properties
# REDIS (RedisProperties)
spring.redis.cluster.nodes=111.231.112.151:7000,111.231.112.151:7001,111.231.112.151:7002,111.231.112.151:7003,111.231.112.151:7004,111.231.112.151:7005
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
  • 3.2 junit
    @Test
    public void testJedisCluster() {
        redisUtil.set("clusterKey", "fsdfsdfsdfsd");
        System.out.println(redisUtil.get("*"));
    }

猜你喜欢

转载自blog.csdn.net/wolfjson/article/details/78668191