redis

redis 4부 - jedis를 이용한 client sample ...

drscg 2019. 4. 8. 14:01

이제 마지막이다.
redis server를 다 설치했으니, 그곳에 data를 write하고 또 read해 보자.
Java를 이용해, 간단한 sample을 만들어 보겠다.

가장 많이 쓰이는 redis java library는 jedis 라고 한다.
글을 쓰고 있는 시점에서 가장 최신 버전인 3.0.1 을 첨부한다.
또한, 관련 library인 Common Pool 2 library를 첨부한다.
그리고, 입력한 data를 확인하기 위한, RedisDesktopManager for Windows를 첨부한다.

jedis의 Java document는 http://xetorthio.github.io/jedis/에서 볼수 있다.

우리는 Sentinel을 이용한 replication을 사용하여, redis cluster를 구성하였다.
그리고, Redis는 Master에만 data를 write할 수 있다고 언급한 바 있다.
Sentinel은 Master가 down되면, Slave 중의 하나를 Master로 선발한다.

따라서, 우리는 redis cluster의 어느 것이 Master인지 알지 못한다.
왜냐하면, redis master가 down되어, 다른 slave가 master가 되어 있을 가능성이 있기 때문이다.
그렇다면 어디에서 알 수 있을까? 그렇다. Sentinel이다.

redis의 ip나 port를 알 필요가 없다. Sentinel의 ip, port만 알고 있으면 된다.
간단하게 shadow code를 작성해 보면 아래와 같다.

1. Sentinel의 ip/port를 이용하여, JedisSentinelPool 을 구성한다.
2. Master Redis 객체를 얻어온다 ( JedisSentinelPool.getResource() )
3. redis에 입/출력한다.
4. JedisSentinelPool 을 제거한다.

real code는 다음과 같다.

import java.net.Socket;
import java.util.HashSet;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.exceptions.JedisException;

public class Redis
{
	private	static	final	String	MASTER_NAME	= "drscg.master";
	public	static	final	String	PASSWORD	= "passw0rd";
	public	static	final	int		DATABASE	= 0;

	public static void main( String[] args )
	{
		HashSet&ltString&gt			oHashSetSetSentinel			= null;
		GenericObjectPoolConfig	oGenericObjectPoolConfig	= null;
		JedisSentinelPool		oJedisSentinelPool			= null;
		Jedis					oJedis						= null;
		Socket					oSocket						= null;
		String					strValue					= "";

		// Sentinel의 ip와 port setting
		oHashSetSetSentinel = new HashSet&ltString&gt();
		oHashSetSetSentinel.add("10.0.0.1:17000");
		oHashSetSetSentinel.add("10.0.0.2:17000");
		oHashSetSetSentinel.add("10.0.0.4:17000");

		// Sentinel Pool에 연결한다.
		oGenericObjectPoolConfig = new GenericObjectPoolConfig();
		oJedisSentinelPool = new JedisSentinelPool(MASTER_NAME, oHashSetSetSentinel,
									oGenericObjectPoolConfig, 1000, PASSWORD, DATABASE);

		// set
		for ( int i = 0; i &lt 100; i++ )
		{
			try
			{
				// pool에서 jedis master를 가져온다.
				oJedis = oJedisSentinelPool.getResource();
				
				// master가 어떤 server인지 확인하기 위한 code
				oSocket = oJedis.getClient().getSocket();
				System.out.println("Connected to " + oSocket.getRemoteSocketAddress());
				
				// set
				oJedis.set("foo_" + i, "bar_" + i);
				System.out.println("set foo_" + i);
			}
			catch ( JedisException ex)
			{
				ex.printStackTrace();
				break;
			}
			finally
			{
				// jedis 객체 반환
				oJedis.close();
			}
			
		}
		
		// get
		for ( int i = 0; i &lt 100; i++ )
		{
			try
			{
				// pool에서 jedis master를 가져온다.
				oJedis = oJedisSentinelPool.getResource();
				
				// master가 어떤 server인지 확인하기 위한 code
				oSocket = oJedis.getClient().getSocket();
				System.out.println("Connected to " + oSocket.getRemoteSocketAddress());
				
				// get
				strValue = oJedis.get("foo_" + i);
				System.out.println("get foo_" + i + ": [" + strValue + "]");
			}
			catch ( JedisException ex)
			{
				ex.printStackTrace();
				break;
			}
			finally
			{
				// jedis 객체 반환
				oJedis.close();
			}
			
		}
		
		// Sentinel Pool 반환
		oJedisSentinelPool.destroy();
	}
}

 

commons-pool2-2.4.3.zip
0.12MB
jedis-3.0.1.zip
0.56MB
redis-desktop-manager-0.9.3.817.z01
5.00MB
redis-desktop-manager-0.9.3.817.z02
5.00MB
redis-desktop-manager-0.9.3.817.zip
0.59MB

'redis' 카테고리의 다른 글

redis 3부 - Warning 제거 ...  (0) 2019.04.08
redis 2부 - replication ...  (0) 2019.04.08
redis 1부 - 설치 ...  (0) 2019.04.08