아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

Tomcat 서버 다중화 시 Session Clustering을 통해 한 서버의 장애 시에도 지속적인 서비스가 가능하도록 구성한다.


Tomcat의 Session Clustering은 Multicast를 이용하여 서버간에 Session을 동기화 하는데

AWS와 같은 Cloud platform에서는 Muticast가 지원되지 않는다.


이 때 사용할 수 있는 방법이 Redis를 이용하여 Session 동기화가 필요한다.


Spring Framework에서는 Redis를 통해 Session Clustering을 지원하고 있다.


■ Dependency


spring-aop-4.3.1.RELEASE.jar

spring-beans-4.3.1.RELEASE.jar

spring-context-4.1.2.RELEASE.jar

spring-context-support-4.1.2.RELEASE.jar

spring-core-4.3.1.RELEASE.jar

spring-data-commons-1.12.10.RELEASE.jar

spring-data-keyvalue-1.1.10.RELEASE.jar

spring-data-redis-1.7.10.RELEASE.jar

spring-expression-4.3.1.RELEASE.jar

spring-jdbc-4.1.2.RELEASE.jar

spring-modules-validation-0.9.jar

spring-orm-4.1.2.RELEASE.jar

spring-security-config-4.1.2.RELEASE.jar

spring-security-core-4.1.2.RELEASE.jar

spring-security-web-4.1.2.RELEASE.jar

spring-session-1.3.1.RELEASE.jar

spring-session-data-redis-1.3.1.RELEASE.jar

spring-tx-4.1.2.RELEASE.jar

spring-web-4.1.2.RELEASE.jar

spring-webmvc-4.1.2.RELEASE.jar

spring-ws-core-2.4.0.RELEASE.jar

 



먼저 Session Clustering을 이용하기 위해 XML을 이용하여 설정한다.


 ■ context-redis.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"

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

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

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

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

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


<!-- This should not be removed -->

<context:annotation-config/>

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

<bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" destroy-method="destroy">

<property name="hostName" value="#{project['session.redis.url']}" />

<property name="port" value="#{project['session.redis.port']}" />

</bean>

<!-- Do not remove -->

<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>

<!-- bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<property name="hostName" value="int-bus-mgmt-eh-dev.hez7xm.ng.0001.euw1.cache.amazonaws.com" />

<property name="port" value="6379" />

<property name="usePool" value="true" />

</bean-->

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

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

</bean> 

</beans>

 


<context:annotation-config>를 통해 Context내의 Annotation을 활성화 한다.


RedisHttpSessionConfiguration을 bean으로 설정한다.


LettuceConnectionFactory에 Redis hostname과 port를 정의한다.


AWS에서 제공하는 Redis를 사용할 경우에는 ConfigureREsisAction.NO_OP를 적용해야 오류가 발생하지 않는다.


설정이 완료되었으면 web.xml에 springSessionFilter를 추가해야 한다.


■ web.xml Filter 추가


   <filter>

    <filter-name>springSessionRepositoryFilter</filter-name>

    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>springSessionRepositoryFilter</filter-name>

    <url-pattern>/*</url-pattern>

    <dispatcher>REQUEST</dispatcher>

    <dispatcher>ERROR</dispatcher>

  </filter-mapping>



이상으로 Spring Framework와 Redis의 연동은 완료되었다.


Redis에 Session이 저장되어 있는지 확인하기 위해 Redis Client를 설치하여 확인이 가능하다.


Redis client에 접속하기 위해 redis-cli -h "hostname" -p "port"로 접속이 가능하다.


keys * command를 이용하여 모든 키 정보 출력이 가능하다.

AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

ARCHIVE

LINK