5 微服务实战系列

来源:互联网 发布:淘宝店铺货源怎么搞定 编辑:程序博客网 时间:2024/05/22 07:57
> **redis3.2+升级cluster功能:**> 1主从模式:主从分片,为每一个master节点创建replica,主节点宕机,从节点升级为主节点,保证服务延续性> 2负载均衡:集群根据流量redict to node> 3读写分离:主节点写操作,从节点备份读操作> 4ruby脚本简单创建集群部署

1 基础环境预览

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

2 环境安装

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

2.3 集群搭建

  • 2.3.1基础集群
mkdri redis-clustercd redis-clustermkdir 7000/7001/7002/7003/7004/7005cp ../redis.conf ./7000/7001/7002/7003/7004/7005
**集群配置**Vim redis.confport 7000 #7001/7002/7003/7004/7005# bind 127.0.0.1 ::1protect-mode nodaemonize yespidfile /var/run/redis_7000.pidcluster-enabled yescluster-config-file nodes-7000.confcluster-node-timeout 15000appendonly  yes 
**启动集群**../../src/redis-server redis.conf
  • 2.3.2 集群负载主从搭建
安装rubyyum -y install ruby ruby-devel rubygems rpm-build安装gem-redisgem 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:7000111.231.112.x:7001111.231.112.x:7002Adding replica 111.231.112.x:7003 to 111.231.112.x:7000Adding replica 111.231.112.x:7004 to 111.231.112.x:7001Adding replica 111.231.112.x:7005 to 111.231.112.x:7002M: aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26 111.231.112.151:7000   slots:0-5460 (5461 slots) masterM: d5a4e5e770d8e5aec777724730057ccce65df475 111.231.112.151:7001   slots:5461-10922 (5462 slots) masterM: 563aaa218a4b1e817c9cf3e34582170026f55406 111.231.112.151:7002   slots:10923-16383 (5461 slots) masterS: 1f281de8be5959436bb2112549cc4fffb16192b5 111.231.112.151:7003   replicates aa9485db1dd8b223ccffc3f3c37cd9ac58f32c26S: 99366e6d950c6d9eb923ff1fa089217b24b489ad 111.231.112.151:7004   replicates d5a4e5e770d8e5aec777724730057ccce65df475S: 0c411cd0b12297128c87f7542e67705777218654 111.231.112.151:7005   replicates 563aaa218a4b1e817c9cf3e34582170026f55406Can 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 clusterWaiting 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 d5a4e5e770d8e5aec777724730057ccce65df475S: 0c411cd0b12297128c87f7542e67705777218654 111.231.112.151:7005   slots: (0 slots) slave   replicates 563aaa218a4b1e817c9cf3e34582170026f55406M: 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 7000127.0.0.1:7000> set y dsfsdfsdf-> Redirected to slot [12222] located at 111.231.112.151:7002Master 7002[root@VM_0_7_centos ~]# redis-cli -h 127.0.0.1 -c -p 7002127.0.0.1:7002> set hello world-> Redirected to slot [866] located at 111.231.112.151:7000OKSalve 7004[root@VM_0_7_centos ~]# redis-cli -c -p 7004127.0.0.1:7004> set k dsadfadsfa-> Redirected to slot [7629] located at 111.231.112.151:7001OK

总结:

  • 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:7005spring.redis.password=spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1
  • 3.2 junit
    @Test    public void testJedisCluster() {        redisUtil.set("clusterKey", "fsdfsdfsdfsd");        System.out.println(redisUtil.get("*"));    }
原创粉丝点击