[레디스] 스프링4.0 레투스 풀 설정(XML, JAVA) 샘플코드

    728x90
    반응형

    Gradle 설정

    dependencies {
    	implementation group: 'com.google.guava', name: 'guava', version: '19.0'
    	implementation group: 'io.lettuce', name: 'lettuce-core', version: '4.5.0.RELEASE'
    	implementation group: 'org.latencyutils', name: 'LatencyUtils', version: '2.0.3'
    	implementation group: 'io.netty', name: 'netty-all', version: '4.1.68.Final'
    	implementation group: 'io.reactivex.rxjava3', name: 'rxjava', version: '1.3.4'
    	implementation group: 'org.springframework.data', name: 'spring-data-commons', version: '1.13.23.RELEASE'
    	Implementation group: 'org.springframework.data', name: 'spring-data-keyvalue', version: '1.2.23.RELEASE'
    	implementation group: 'org.springframework.data', name: 'spring-data-redis', version: '1.8.23.RELEASE'
    	implementation group: 'org.springframework.session', name: 'spring-session-data-redis', version: '1.3.1.RELEASE'
    	implementation group: 'org.springframework.session', name: 'spring-session', version: '1.3.1.RELEASE'
    	implementation group: 'com.google.guava', name: 'failureaccess', version: '1.0.1'
    }

     

     

     

    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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <context:annotation-config />
    	<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
    		<!--세션시간설정(초 단위)-->
    		<property name="maxInactiveIntervalInSeconds" value="3600" />
    	</bean>
    
    	<bean id="레투스 설정" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
    		<constructor-arg>
    			<ref bean="레투스 풀 설정" />
    		</constructor-arg>
    
    		<!--데이터베이스 인덱스 설정-->
    		<property name="database" value="0" />
    		<!--레디스 비밀번호-->
    		<property name="password" value="비밀번호" />
    	</bean>
    
    	<bean id="레투스 풀 설정" class="org.springframework.data.redis.connection.lettuce.DefaultLettucePool">
    		<constructor-arg>
    			<ref bean="센티널 설정" />
    		</constructor-arg>
    		<property name="poolConfig" >
    			<ref bean="커넥션 풀 설정" />
    		</property>
    		<!--레디스 비밀번호-->
    		<property name="password" value="비밀번호" />
    	</bean>
    	<bean id="커넥션 풀 설정" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
    		<!--레투스 풀 갯수 설정-->
    		<property name="maxTotal" value="5"/>
    	</bean>
    
    	<bean id="센티널 설정" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
    		<!--레디스 마스터 노드명-->
    		<constructor-arg value="마스터 노드명" />
    		<constructor-arg>
    			<set>
    				<!--레디스 센티널 정보 아이피:포트 ,로 구분-->
    				<value>1.12.123.4:8090,1.12.123.5:8091</value>
    			</set>
    		</constructor-arg>
    	</bean>
    
    	<bean id="레디스 템플릿" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="lettuceFactory"/>
    </beans>

     

     

     

    JAVA

    public class HdcRedisUtil {
    	private RedisTemplate<String, Object> templateInit() {
    		try {
    			// 레디스 센티널 설정
    			RedisSentinelConfiguration sentinelConfig = 
    				new RedisSentinelConfiguration()
    				.master("마스터 노드명")
    				.sentinel("센티널 아이피1", 센티널 포트1)
    				.sentinel("센티널 아이피2", 센티널 포트2)
    
    			// 레투스 풀 설정
    			GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    			genericObjectPoolConfig.setMaxTotal(5);
    			DefaultLettucePool lettucePool = new DefaultLettucePool(sentinelConfig);
    			lettucePool.setPoolConfig(genericObjectPoolConfig);
    			lettucePool.setPassword("레디스 비밀번호");
    			lettucePool.afterPropertiesSet();
    
    			LettuceConnectionFactory connFactory = new LettuceConnectionFactory(lettucePool);
    			connFactory.setPassword("레디스 비밀번호");
    			connFactory.afterPropertiesSet();
    
    			// 레디스 템플릿 설정
    			RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>() ;
    			redisTemplate.setConnectionFactory(connFactory);
    			StringRedisSerializer srz = new StringRedisSerializer();
    			JdkSerializationRedisSerializer jsrz = new JdkSerializationRedisSerializer();
    
    			redisTemplate.setKeySerializer(srz);
    			redisTemplate.setValueSerializer(srz);
    			redisTemplate.setHashKeySerializer(srz);
    			redisTemplate.setHashValueSerializer(jsrz);
    			redisTemplate.afterPropertiesSet();
    
    		} catch (Exception e) {
    			throw new Exception(e);
    		}
    		return redisTemplate;
    	}
    }

     

    728x90
    반응형

    댓글