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("*"));
        }