민서네집

Tomcat Clustering 본문

Java

Tomcat Clustering

브라이언7 2015. 2. 4. 07:52

< Implementation of tomcat clustering >

http://www.easywayserver.com/implementation-tomcat-clustering.htm


위 문서에 정말 잘 나와 있다.

나는 하나의 서버 안에 Apache Server와 TomcatA Server와 TomcatB Server 가 같이 있는, Vertical Clustering 을 구현했다.


위 페이지에 나와 있는 대로 Tomcat 서버의 server.xml 파일을 설정했다.

httpd-jk.conf 파일과 workers.properties 파일의 설정은 이 웹페이지의 아래에 적어 두었다.


이렇게 Clustering을 하는 이유는 Fail Over 시에 대비하고, 웹 어플리케이션 변경 시에도 중단 없는 서비스를 제공하기 위함이다. 이렇게 하기 위해서는 Tomcat Server 앞 단에 Apache Server 를 두어야 한다.


설치일: 2015-02-05

OS: Ubuntu 12.04


http://polaris.vartist.org/entry/Ubuntu-Diary-Apach2-Tomcat6-%EC%84%A4%EC%B9%98-%EB%B0%8F-%EC%97%B0%EB%8F%99


위 페이지에 있는 대로 


b) Apache2 설치

#sudo apt-get install apache2


설치 완료 후, 'http://서버IP' 입력시 It works 페이지 정상출력

< 실제 경로 : /var/www (web root directory) >


d) Mod_jk 설치

#sudo apt-get install libapache2-mod-jk


http://www.easywayserver.com/implementation-tomcat-clustering.htm 문서에 보면

httpd.conf 를 편집하라고 되어 있는데,


/etc/libapache2-mod-jk/httpd-jk.conf

이 파일을 편집하면 된다.


/etc/libapache2-mod-jk/workers.properties

이 파일을 열어서 위 웹페이지에 있는대로 수정함.


테스트 해 보니 Apache Web Server에서 404 에러가 나서

# vi /etc/libapache2-mod-jk/httpd-jk.conf
 
JkLogLevel info 를 JkLogLevel debug 로 수정해서 에러 메시지를 보다 더 자세히 출력함. (에러 확인이 끝난 후 다시 info로 복귀시킴)

[Wed Feb 04 18:31:49.919 2015] [18549:140330666026752] [debug] jk_translate::mod_jk.c (3488): missing uri map for ***.cafe24.com:/examples/test.jsp
[Wed Feb 04 18:31:49.919 2015] [18549:140330666026752] [debug] jk_map_to_storage::mod_jk.c (3647): missing uri map for ***.cafe24.com:/examples/test.jsp

위와 같은 로그.


[문제 해결]
JkMount 명령을 <VirtualHost *:80> 안에 집어 넣고 apache 서버를 restart 시켰더니 404 에러가 안 나고 잘됨.

/etc/libapache2-mod-jk/httpd-jk.conf

    <VirtualHost *:80>

        JkMount /* loadbalancer

        JkMount /jk-manager/* jk-manager

        JkMount /jk-status/* jk-status

    </VirtualHost>


test.jsp 파일을 각각 tomcatA 와 tomcatB 에 넣고, 실행해 봤더니

Fail Over 시 다른 tomcat 서버로 연결은 잘 되는데, 세션 ID가 달라짐. 

Serializable 을 상속받은 객체를 세션에 저장해 봤는데, 세션 복제가 안됨.


[문제 해결]

web.xml 파일에 <distributable /> 을 넣지 않았었음. 넣으니까 Session ID가 동일하게 생기고, 세션이 복제됨.


이렇게 하지 않으면 웹브라우저에서 tomcatA 에 접근하고 나서 tomcatB에 접근하면 Sessino ID가 바뀜. 그 반대의 경우도 마찬가지.


web.xml 파일에 <distributable /> 을 넣으면 


tomcatA로 접근할 때: 6A3BC293E869AE55712BA00D86FCD255.tomcatA


tomcatB로 접근할 때: 6A3BC293E869AE55712BA00D86FCD255.tomcatB


세션에 저장한 값이 tomcatA, tomcatB 서버에서 서로 공유됨.





JK Status Manager 셋팅



[참고]

http://www.celinio.net/techblog/?p=1095

http://www.gesea.de/techdocs.htm?id=67616


/etc/libapache2-mod-jk/httpd-jk.conf


<IfModule jk_module>

    # We need a workers file exactly once
    # and in the global server
    JkWorkersFile /etc/libapache2-mod-jk/workers.properties

    # Our JK error log
    # You can (and should) use rotatelogs here
    JkLogFile /var/log/apache2/mod_jk.log

    # Our JK log level (trace,debug,info,warn,error)
    JkLogLevel info

    # Our JK shared memory file
    JkShmFile /var/log/apache2/jk-runtime-status

    # Start a separate thread for internal tasks like
    # idle connection probing, connection pool resizing
    # and load value decay.
    # Run these tasks every JkWatchdogInterval seconds.
    # Since: 1.2.27
    JkWatchdogInterval 60

    # Configure access to jk-status and jk-manager
    # If you want to make this available in a virtual host,
    # either move this block into the virtual host
    # or copy it logically there by including "JkMountCopy On"
    # in the virtual host.
    # Add an appropriate authentication method here!
    <Location /jk-status>
        # Inside Location we can omit the URL in JkMount
        JkMount jk-status
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </Location>
    <Location /jk-manager>
        # Inside Location we can omit the URL in JkMount
        JkMount jk-manager
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
		Allow from 61.**.208.***
    </Location>

    # Example for Mounting a context to the worker "balancer"
    # The URL syntax "a|b" instantiates two mounts at once,
    # the first one is "a", the second one is "ab".
    # JkMount /myapp|/* balancer
    <VirtualHost *:80>
        JkMount /* loadbalancer
		JkMount /jk-manager|/* jk-manager
		JkMount /jk-status|/* jk-status
    </VirtualHost>

</IfModule>



/etc/libapache2-mod-jk/workers.properties



# workers.properties -

workers.tomcat_home=/home/test-user/tomcatA

workers.java_home=/usr/lib/jvm/default-java

ps=/

worker.list=loadbalancer,jk-status,jk-manager

worker.jk-status.type=status
worker.jk-manager.type=status

worker.tomcatA.port=8109
worker.tomcatA.host=localhost
worker.tomcatA.type=ajp13

worker.tomcatB.port=8209
worker.tomcatB.host=localhost
worker.tomcatB.type=ajp13

#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
worker.tomcatA.lbfactor=1
worker.tomcatB.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcatA,tomcatB
worker.loadbalancer.sticky_session=1


test.jsp


<%
  String user = request.getParameter("user");
  String savedUser = (String)session.getAttribute("user");
  
  if( user != null && !user.isEmpty() ) {
	session.setAttribute("user",user);
  }
%>
<html>
<head>
<title>Test JSP</title>
</head>

<body>
<form action="/examples/test.jsp" name="frm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="#CCCCCC">
    <td width="20%">TomcatB Machine</td>
    <td width="80%"> </td>
  </tr>
  <tr>
    <td>Session ID :</td>
    <td><%=session.getId()%></td>
  </tr>
  <tr>
	<td>Saved User :</td>
	<td><input type="text" value="<%=savedUser%>" /></td>
  </tr>
  <tr>
	<td>New User :</td>
	<td><input type="text" name="user" value="" /></td>
  </tr>
  <tr>
	<td colspan="2"><input type="button" value="SEND" onclick="frm.submit()" /></td>
  </tr>
</table>
</form>
</body>
</html>





< 테스트 방법 >


1) Apache Web Server 로 접속 (80포트)



2) 세션에 저장할 User 이름을 입력하고 SEND 버튼을 누른다.



3) 주소줄에서 test.jsp 뒤의 파라미터를 지우고 엔터를 누른다.



4) 세션에 저장된 User 값이 보인다.



5) Tomcat B 서버를 죽이고, 새로고침 했을 때, 세션에 저장된 User 이름이 동일하게 보이고, Session ID도 동일하고, Session ID 뒤의 jvmRoute 값만 tomcatA 로 변경됨을 확인한다. ( TomcatA 서버로 연결되고, 세션에 저장된 값도 그대로 복제되었음을 확인할 수 있다.) jvmRoute 값은 tomcat 의 server.xml 안에서 설정한 값이다.










Tomcat Clustering Series Part 1 : Simple Load Balancer


http://www.javacodegeeks.com/2012/11/tomcat-clustering-series-part-1-simple-load-balancer.html


Running Multiple Tomcat Instances on Single Machine


http://www.ramkitech.com/2011/07/running-multiple-tomcat-instances-on.html

[Apache - Tomcat] 로드 밸런싱, 세션 클러스터링


http://bongbong.net/entry/Apache-Tomcat-%EB%A1%9C%EB%93%9C-%EB%B0%B8%EB%9F%B0%EC%8B%B1-%EC%84%B8%EC%85%98-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0%EB%A7%81


Apache 2.2 로드 밸런싱 Tomcat 6.0 세션 클러스터링

http://blog.jqdom.com/?p=363


[ tomcat ] loadbalacing 과 session clustering


http://shonm.tistory.com/entry/tomcat-loadbalacing-%EA%B3%BC-session-clustering


An Introduction To Tomcat Cluster Configuration

http://www.mulesoft.com/tcat/tomcat-cluster


Tomcat Clustering - A Step By Step Guide


Apache-Tomcat 로드밸런싱 및 세션 클러스터링 설정하기




Tomcat Clustering - Fail Over


Tomcat Clustering 으로 묶으면 Fail Over 가 적용될까?


http://stackoverflow.com/questions/11465714/tomcat-webapp-failover


http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html

* failover 로 검색해 볼 것.


http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html#Bind_session_after_crash_to_failover_node


Comments