본문 바로가기

TroubleShooting/Redis

Redis Cluster 구성

728x90

Redis cluster 구성


 Redis 파티셔닝(partitioning) 방법론(client side, proxy assisted, query routing) 들이 나왔지만, Redis 3.0.0(https://groups.google.com/forum/#!msg/redis-db/dO0bFyD_THQ/Uoo2GjIx6qgJ) 부터 지원하기 시작한 Redis Cluster 에서 샤딩 및 파티셔닝 구성을 할 수 있게 되었다. 더불어 HA 도 같이 지원되기 때문에 안 쓸 이유가 없을거 같다.

 

Redis Cluster 튜토리얼 을 참고해서 다음과 같이 작업해 보았다.


master 서버 디렉토리 생성

# pwd

/root/redis-test

# ls

redis-cli  redis-master-sample.conf  redis-server

# mkdir 7001 7002 7002

# cp redis-master-sample.conf 7001/redis-master1.conf

# cp redis-master-sample.conf 7002/redis-master2.conf

# cp redis-master-sample.conf 7003/redis-master3.conf


redis-master.conf 변경

7001/redis-master1.conf 의 port 를 7001 로 변경. 7002/ 7003/ 디렉토리도 차례로 변경.


※ redis-master-sample.conf

port 7001

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes


redis master 실행

[root@localhost redis-test]# cd 7001

[root@localhost 7001]# ../redis-server redis-master-1.conf &

[root@localhost redis-test]# cd 7002

[root@localhost 7002]# ../redis-server redis-master-2.conf &

[root@localhost redis-test]# cd 7003

[root@localhost 7003]# ../redis-server redis-master-3.conf &

각 디렉토리에서 nodes.conf 와 appendonly.aof 파일이 생성된다.


클러스터 구성

# redis-cli -p 7001 cluster meet 1.2.3.4 7002


# redis-cli -p 7001 cluster meet 1.2.3.4 7003

※ ip 부분을 localhost 로 하니 오류가 발생했다. 사설 IP 주소라도 입력해야 함.


클러스터 구성 확인

# redis-cli -p 7001 cluster nodes

33f3582b3e764f0ee36ac80825f8688896137463 1.2.3.4:7003 master - 0 1446773581372 2 connected

0a93f8db39e7b459d970e07772c9767d28893b73 1.2.3.4:7001 myself,master - 0 0 1 connected

d9dd870b7ae43c1b5420ad21d3d9ef933df61907 1.2.3.4:7002 master - 0 1446773582464 0 connected


slot 할당

# for slot in {0..5400}; do redis-cli -p 7001 cluster addslots $slot; done;

ok

ok

....


# for slot in {5401..10800}; do redis-cli -p 7002 cluster addslots $slot; done;


# for slot in {10801..16383}; do redis-cli -p 7003 cluster addslots $slot; done;

※ slot 16384 개를 3 서버로 분산한다는 것은 알겠는데, slot 에 대한 설명이 부족함.(추후 다시 정리 필요)


slot 할당 상태 확인

# redis-cli -p 7001 cluster nodes

33f3582b3e764f0ee36ac80825f8688896137463 1.2.3.4:7003 master - 0 1446773979358 2 connected 10801-16383

0a93f8db39e7b459d970e07772c9767d28893b73 1.2.3.4:7001 myself,master - 0 0 1 connected 0-5400

d9dd870b7ae43c1b5420ad21d3d9ef933df61907 1.2.3.4:7002 master - 0 1446773980450 0 connected 5401-10800


java Redis 클라이언트 중에서 Redis Cluster 를 지원하는 라이브러리는 Jedis 를 사용하기로 했다. Spring Data Redis 는 언제쯤이면 샤딩을 지원하게 될지...


poms.xml

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.7.3</version>

</dependency


beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:c="http://www.springframework.org/schema/c"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:util="http://www.springframework.org/schema/util"

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

        <util:properties id="data" location="classpath:app-properties.xml" />


<util:set id="jedisClusterNodes">

<!--   <bean class="redis.clients.jedis.HostAndPort" c:host="1.2.3.4" c:port="7001" /> -->

<bean class="redis.clients.jedis.HostAndPort" c:host="#{data['redis1.host']}" c:port="#{data['redis1.port']}" />

<bean class="redis.clients.jedis.HostAndPort" c:host="#{data['redis2.host']}" c:port="#{data['redis2.port']}" />

<bean class="redis.clients.jedis.HostAndPort" c:host="#{data['redis3.host']}" c:port="#{data['redis3.port']}" />

</util:set>

  <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster" c:nodes-ref="jedisClusterNodes" />

</beans>

: 세 개의 서버로 redis cluster pool 구성.


app-properties.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" >

<properties>

<comment>Redis Setting Properties</comment>

<entry key="redis1.host">1.2.3.4</entry>

<entry key="redis1.port">7001</entry>

<entry key="redis2.host">1.2.3.4</entry>

<entry key="redis2.port">7002</entry>

<entry key="redis3.host">1.2.3.4</entry>

<entry key="redis3.port">7003</entry>

</properties>


Test 클래스

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:/applicationContext.xml" })

public class RedisClusterTest {

@Autowired

JedisCluster jedisCluster;

@Test

public void testRedisClusterAccountHash() {

Account account = new Account();

account.setUserId("testuser1");

account.setName("SuperMan");


ObjectMapper mapper = new ObjectMapper();

String jsonString = "";

try {

jsonString = mapper.writeValueAsString(account);

} catch (JsonProcessingException e) {

e.printStackTrace();

}

jedisCluster.hset(account.getUserId(), "Account", jsonString);

String retString = jedisCluster.hget(account.getUserId(), "Account");

Account account2 = null;

try {

account2 = mapper.readValue(retString, Account.class);

} catch (JsonParseException e) {

e.printStackTrace();

} catch (JsonMappingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

assertEquals(account.getName(), account2.getName());

}

}


'TroubleShooting > Redis' 카테고리의 다른 글

redis 간단 명령어 정리  (0) 2015.08.18
Redis 간편 설치 in Linux  (0) 2015.07.21