본문 바로가기
Spring

Spring 4.3 Redis Sentinel 연동

by 수앙 2018. 6. 16.

1. 주요 라이브러리 버전

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.12.RELEASE</version>

<exclusions>

<!-- Exclude Commons Logging in favor of SLF4j -->

<exclusion>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.3.12.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.3.12.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>4.3.12.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>1.8.8.RELEASE</version>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.9.0</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.2</version>

</dependency>


2. Redis server 3.2.10 설치 및 redis와 sentinel 연동 설정

여기서는 다루지 않음


3. Spring redis xml 설정

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" />


<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">

<constructor-arg value="mymaster" />

<constructor-arg>

<set>

<value>192.168.0.100:26379</value>

</set>

</constructor-arg>

</bean>


<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<constructor-arg ref="redisSentinelConfiguration" />

<constructor-arg ref="jedisPoolConfig" />

        <!-- 비밀번호 설정 시 아래 설정 추가 -->

<property name="password" value="admin" />

</bean>


<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory" />

<property name="keySerializer">

<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />

</property>

        <!-- value 값을 json 형태로 사용 -->

<property name="valueSerializer">

<bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">

<constructor-arg value="java.lang.Object" />

</bean>

</property>

<property name="hashKeySerializer">

<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />

</property>

<property name="hashValueSerializer">

<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />

</property>

</bean>


4. Spring test 코드

import javax.annotation.Resource;


import org.junit.Test;

import org.junit.runner.RunWith;

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

import org.springframework.data.redis.core.ListOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.data.redis.listener.ChannelTopic;

import org.springframework.test.context.ContextConfiguration;

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


@RunWith(SpringJUnit4ClassRunner.class)

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

public class RedisConnectionTest {


@Resource(name = "redisTemplate")

private ValueOperations<String, Object> valueOps;


@Test

public void testGet() {

Object value = valueOps.get("test");

System.out.println(value);

}


}


끝.

'Spring' 카테고리의 다른 글

Spring 4 request logging  (0) 2019.11.07
Spring 4.3 Redis Sentinel Pubsub 설정  (0) 2018.06.16
Spring 4 Atomikos 설정  (0) 2017.10.14
Spring 4 JSR-303 Validator  (0) 2017.06.12
Spring REST Docs 사용법  (0) 2017.04.23

댓글