Language/Java

redis cluster client java

아르비스 2016. 2. 25. 20:51

위 글 중 redis cluster를 통해서 redis cluster 구성함.


Java project에서 Redis Cluster 접속을 위해서 라이브러리는 jedis를 사용함.

pom.xml 에 jedis library dependency 추가


<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.8.0</version>

</dependency>

 


bean.xml

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

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

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

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

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

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

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

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

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

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

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


<!-- spring java config -->

<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="#{resources['resource.redis1.host']}" c:port="#{resources['resource.redis1.port']}" />

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

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

</util:set>

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

</beans> 


properties에

#**** REDIS Cluster SETTING VALUE ************

resource.redis1.host=1.2.3.4

resource.redis1.port=7000

resource.redis2.host=1.2.3.4

resource.redis2.port=7001

resource.redis3.host=1.2.3.4

resource.redis3.port=7002


test.java

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import redis.clients.jedis.JedisCluster;


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:/WEB-INF/spring/applicationContext-root.xml" })

public class RedisClusterTest {

@Autowired

JedisCluster jedisCluster;

@Test

public void testRedisCluster() {

long start;

for (int i = 1; i <= 100; i++) {

            start = System.currentTimeMillis();

//            jedisCluster.hset("k:" + i, "f" + i, "v:" +start);

            jedisCluster.set("k:" + i, "f" + i);

            System.out.print("set " + i +"th value in " + (System.currentTimeMillis() - start) + " ms");

            start = System.currentTimeMillis();

//            jedisCluster.hget("k:" + i, "f" + i);

            jedisCluster.get("k:" + i);

            System.out.println(", get " + i +"th value in " + (System.currentTimeMillis() - start) + " ms");

        }

}

}



만약 테스트 중.. 

 redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException:Too many Cluster redirections?


이런 오류가 발생한다면.... 다른거 고민하지 말고,

redis_cluster 를 확인해보자..


# ./redis-cli -p 7000 cluster nodes

78a3883d94c515e461b6d4660a87eb43ee18b558 127.0.0.1:7002 master - 0 1456401538707 3 connected 10923-16383

ec673a9f8f3e8b2d595f63a68bf9915933092df6 127.0.0.1:7001 master - 0 1456401537704 2 connected 5461-10922

47fe5069027b24994467ec3368b15bbc1b581398 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460

#

 

이렇게 127.0.0.1로 보여진다면... 이미 당신은 루저...ㅠㅠ 나와 같은 실수한 사람이다..ㅎㅎㅎ


cluster 생성시 127이 아닌 실ㅈ ip를 넣어주면 잘 동작한다..

!!