수요일, 5월 22, 2024
HomeLanguageRedis Cluster 설치 및 구성 java Source

Redis Cluster 설치 및 구성 java Source

Redis Cluster 설치 및 구성 java Source

운영체제 : ubuntu 12.04

1. 패키지 설치

apt-get install -y git curl build-essential ruby

2.redis 최신소스 다운로드 및 컴파일

git clone https://github.com/antirez/redis.git /opt/redis
cd /opt/redis
make

3.redis 바이너리 설치

ln -s /opt/redis/src/redis-cli /usr/sbin/redis-cli
ln -s /opt/redis/src/redis-trib.rb /usr/sbin/redis-trib.rb
ln -s /opt/redis/src/redis-server /usr/sbin/redis-server
ln -s /opt/redis/src/redis-sentinel /usr/sbin/redis-sentinel
ln -s /opt/redis/src/redis-check-aof /usr/sbin/redis-check-aof
ln -s /opt/redis/redis-benchmark /usr/sbin/redis-benchmark
ln -s /opt/redis/src/redis-check-dump /usr/sbin/redis-check-dump

4.gem redis 설치

gem install redis

참고 : centos gem 설치방법

yum install ruby ruby-devel ruby-irb ruby-rdoc ruby-ri
yum install rubygems
gem update
gem update --system
gem install redis

5.redis conf 생성 및 설정


mkdir 7379 8379 9379

cp /opt/redis/redis.conf /opt/redis/7379/7379.conf

echo 'cluster-enabled yes' >> /opt/redis/7379/7379.conf
echo 'cluster-config-file nodes.conf' >> /opt/redis/7379/7379.conf
echo 'cluster-node-timeout 5000' >> /opt/redis/7379/7379.conf
sed -i '/^appendonly no/ s:.*:appendonly yes:' /opt/redis/7379/7379.conf
sed -i 's/port 6379/port 7379/g' /opt/redis/7379/7379.conf
sed -i 's/dir .\//dir \/opt\/redis\/7379\//g' /opt/redis/7379/7379.conf
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /opt/redis/7379/7379.conf
sed -i 's/pidfile \/var\/run\/redis.pid/pidfile \/opt\/redis\/7379\/redis.pid/g' /opt/redis/7379/7379.conf

cp /opt/redis/redis.conf /opt/redis/8379/8379.conf
cp /opt/redis/redis.conf /opt/redis/9379/9379.conf

sed -i 's/port 7379/port 8379/g' /opt/redis/8379/8379.conf
sed -i 's/dir .\//dir \/opt\/redis\/8379\//g' /opt/redis/8379/8379.conf
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /opt/redis/8379/8379.conf
sed -i 's/pidfile \/var\/run\/redis.pid/pidfile \/opt\/redis\/8379\/redis.pid/g' /opt/redis/8379/8379.conf

sed -i 's/port 7379/port 9379/g' /opt/redis/9379/9379.conf
sed -i 's/dir .\//dir \/opt\/redis\/9379\//g' /opt/redis/9379/9379.conf
sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /opt/redis/9379/9379.conf
sed -i 's/pidfile \/var\/run\/redis.pid/pidfile \/opt\/redis\/9379\/redis.pid/g' /opt/redis/9379/9379.conf

6. redis 실행

/opt/redis/src/redis-server /opt/redis/7379/7379.conf &
/opt/redis/src/redis-server /opt/redis/8379/8379.conf &
/opt/redis/src/redis-server /opt/redis/9379/9379.conf &

7.redis cluster 설정(master 3개)
rediscluster001

src/redis-trib.rb  create --replicas 0 127.0.0.1:7379 127.0.0.1:8379 127.0.0.1:9379

위의 명령실행 결과

>>> Creating cluster
Connecting to node 127.0.0.1:7379: OK
Connecting to node 127.0.0.1:8379: OK
Connecting to node 127.0.0.1:9379: OK
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:7379
127.0.0.1:8379
127.0.0.1:9379
M: c923041b0bd608f2626ccc2de3d5cefd58d10869 127.0.0.1:7379
   slots:0-5460 (5461 slots) master
M: f3464abdacaf24e002897fd7985890b04b603f63 127.0.0.1:8379
   slots:5461-10922 (5462 slots) master
M: 31339c56231f01ef2f9f844e891d48bf72b20c44 127.0.0.1:9379
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
11254:M 16 Sep 11:45:24.740 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
11257:M 16 Sep 11:45:24.741 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
11265:M 16 Sep 11:45:24.741 # configEpoch set to 3 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster
11254:M 16 Sep 11:45:24.814 # IP address for this node updated to 127.0.0.1
11257:M 16 Sep 11:45:24.914 # IP address for this node updated to 127.0.0.1
11265:M 16 Sep 11:45:24.915 # IP address for this node updated to 127.0.0.1
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 127.0.0.1:7379)
M: c923041b0bd608f2626ccc2de3d5cefd58d10869 127.0.0.1:7379
   slots:0-5460 (5461 slots) master
M: f3464abdacaf24e002897fd7985890b04b603f63 127.0.0.1:8379
   slots:5461-10922 (5462 slots) master
M: 31339c56231f01ef2f9f844e891d48bf72b20c44 127.0.0.1:9379
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

8.redis cluster 설정(master 3개, slave 3개 1:1)
rediscluster002

src/redis-trib.rb  create --replicas 1 127.0.0.1:7379 127.0.0.1:8379 127.0.0.1:9379 127.0.0.1:7380 127.0.0.1:8380 127.0.0.1:9380

9.redis client 실행

src/redis-cli -p 7379 -c
127.0.0.1:7379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:109
cluster_stats_messages_received:109
127.0.0.1:7379> cluster nodes
c923041b0bd608f2626ccc2de3d5cefd58d10869 127.0.0.1:7379 myself,master - 0 0 1 connected 0-5460
f3464abdacaf24e002897fd7985890b04b603f63 127.0.0.1:8379 master - 0 1442371582367 2 connected 5461-10922
31339c56231f01ef2f9f844e891d48bf72b20c44 127.0.0.1:9379 master - 0 1442371581363 3 connected 10923-16383
127.0.0.1:7379> flushall
OK
127.0.0.1:7379> keys *
(empty list or set)
127.0.0.1:7379> get 5
-> Redirected to slot [9974] located at 127.0.0.1:8379
"5"
127.0.0.1:8379> 

10 java maven pom.xml 추가


    redis.clients
    jedis
    2.7.2
    jar
    compile

11.Java jUnit jedisClusterTest Source

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

public class jedisClusterTest {

	@SuppressWarnings("deprecation")
	@Test
	public void test() {
		Set clusterNodes = new HashSet();
        clusterNodes.add(new HostAndPort("127.0.0.1", 7379));
        JedisCluster cluster = new JedisCluster(clusterNodes);


		List clusterHostAndPortList = new ArrayList();
		clusterHostAndPortList.add(new HostAndPort("127.0.0.1", 7379));
		clusterHostAndPortList.add(new HostAndPort("127.0.0.1", 8379));
		clusterHostAndPortList.add(new HostAndPort("127.0.0.1", 9379));
		HostAndPort nodeInfo1 = clusterHostAndPortList.get(0);
		HostAndPort nodeInfo2 = clusterHostAndPortList.get(1);
		HostAndPort nodeInfo3 = clusterHostAndPortList.get(2);
		
		Jedis  node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
	    node1.connect();
	    node1.flushAll();

		Jedis  node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
	    node2.connect();
	    node2.flushAll();

		Jedis  node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
	    node3.connect();
	    node3.flushAll();
		
        for(int i=0 ; i < 100; i++){
        	cluster.set(Integer.toString(i), Integer.toString(i));
        }
        for(int i=0 ; i < 100; i++){
        		System.out.println("["+i+"]------- get --------" + cluster.get(Integer.toString(i)));
        }
		
        // get cluster nodes
        System.out.println("------- cluster nodes --------"+cluster.getClusterNodes().values().size());
	}
}

ps: cluster node 변경시

rm -f 7379/nodes.conf 7379/appendonly.aof 
rm -f 8379/nodes.conf 8379/appendonly.aof 
rm -f 9379/nodes.conf 9379/appendonly.aof 
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Time limit exceeded. Please complete the captcha once again.

Most Popular