참고
https://github.com/ran-jit/tomcat-cluster-redis-session-manager
https://eun2jong.com/227
사전 준비사항
- ElastiCache Redis 준비
ElastiCache가 아니어도 상관 없으나 테스트는 ElastiCache Redis로 진행하였음 - ALB + 2대의 Tomcat 서버 준비 (정상동작 확인 후 진행)
- 작업 전 반드시 스냅샷 생성
- 아래 작업을 두개의 Tomcat 서버 모두에 동일하게 진행
버전 확인
[root@ip-172-31-86-130 ~]# rpm -qa | grep release
system-release-2-12.amzn2.x86_64
[root@ip-172-31-86-130 ~]# java -version
openjdk version "11.0.8" 2020-07-14 LTS
OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.8.10.1 (build 11.0.8+10-LTS, mixed mode)
[root@ip-172-31-86-130 ~]# tomcat version
Server version: Apache Tomcat/7.0.76
Server built: Jun 22 2020 23:34:41 UTC
Server number: 7.0.76.0
OS Name: Linux
OS Version: 4.14.186-146.268.amzn2.x86_64
Architecture: amd64
JVM Version: 11.0.8+10-LTS
JVM Vendor: Amazon.com Inc.
레디스 세션 매니저 다운로드 및 설정
[root@ip-172-31-86-130 ~]# cd /usr/share/tomcat/
[root@ip-172-31-86-130 tomcat]# mkdir download
[root@ip-172-31-86-130 tomcat]# cd download/
[root@ip-172-31-86-130 download]# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/2.0.4/tomcat-cluster-redis-session-manager.zip
--2020-07-31 14:23:04-- https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/2.0.4/tomcat-cluster-redis-session-manager.zip
Resolving github.com (github.com)... 52.78.231.108
Connecting to github.com (github.com)|52.78.231.108|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/54272419/c2323ca8-5dd4-11e8-9e90-052fe48c96dd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200731T142305Z&X-Amz-Expires=300&X-Amz-Signature=8aee34e9247501fcfe00a14d07aee0fce04ae4be2a3f0d69226f3b5b9d3990c3&X-Amz-SignedHeaders=host&actor_id=0&repo_id=54272419&response-content-disposition=attachment%3B%20filename%3Dtomcat-cluster-redis-session-manager.zip&response-content-type=application%2Foctet-stream [following]
--2020-07-31 14:23:05-- https://github-production-release-asset-2e65be.s3.amazonaws.com/54272419/c2323ca8-5dd4-11e8-9e90-052fe48c96dd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200731%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200731T142305Z&X-Amz-Expires=300&X-Amz-Signature=8aee34e9247501fcfe00a14d07aee0fce04ae4be2a3f0d69226f3b5b9d3990c3&X-Amz-SignedHeaders=host&actor_id=0&repo_id=54272419&response-content-disposition=attachment%3B%20filename%3Dtomcat-cluster-redis-session-manager.zip&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.107.236
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.107.236|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 740661 (723K) [application/octet-stream]
Saving to: ‘tomcat-cluster-redis-session-manager.zip’
100%[======================================================================================================>] 740,661 742KB/s in 1.0s
2020-07-31 14:23:06 (742 KB/s) - ‘tomcat-cluster-redis-session-manager.zip’ saved [740661/740661]
[root@ip-172-31-86-130 download]# unzip tomcat-cluster-redis-session-manager.zip
Archive: tomcat-cluster-redis-session-manager.zip
creating: tomcat-cluster-redis-session-manager/conf/
inflating: tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties
creating: tomcat-cluster-redis-session-manager/lib/
inflating: tomcat-cluster-redis-session-manager/lib/commons-logging-1.2.jar
inflating: tomcat-cluster-redis-session-manager/lib/commons-pool2-2.4.2.jar
inflating: tomcat-cluster-redis-session-manager/lib/jedis-2.9.0.jar
inflating: tomcat-cluster-redis-session-manager/lib/tomcat-cluster-redis-session-manager-2.0.4.jar
inflating: tomcat-cluster-redis-session-manager/readMe.txt
[root@ip-172-31-86-130 download]# cp tomcat-cluster-redis-session-manager/lib/* ../lib/
[root@ip-172-31-86-130 download]# cp tomcat-cluster-redis-session-manager/conf/* ../conf/
톰캣 설정 변경
[root@ip-172-31-86-130 download]# vim /usr/share/tomcat/conf/redis-data-cache.properties
—주의사항: 레디스 엔드포인트로 변경하고 설정에 맞게 비밀번호등 변경 필요, 아무설정도 하지 않았다면 엔드포인트만 변경
============== 파일내용 ==============
#-- Redis data-cache configuration
#- redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....
redis.hosts= <ElastiCache-Endpoint.cache.amazonaws.com> :6379
#- redis password (for stand-alone mode)
#redis.password=
#- set true to enable redis cluster mode
redis.cluster.enabled=false
#- redis database (default 0)
#redis.database=0
#- redis connection timeout (default 2000)
#redis.timeout=2000
=================================
[root@ip-172-31-86-130 download]# vim /usr/share/tomcat/conf/web.xml
============== 파일내용 변경==============
<session-config>
<session-timeout>60</session-timeout>
</session-config>
=====================================
[root@ip-172-31-86-130 download]# cat /usr/share/tomcat/conf/context.xml
— 주의사항: <Context> </Context> 사이에 <Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />넣어주면 됨
============== 파일내용 변경==============
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
</Context>
=====================================
테스트 페이지 준비
[root@ip-172-31-86-130 download]# cd /var/lib/tomcat/webapps/sample/
[root@ip-172-31-86-130 sample]# wget https://s3.ap-northeast-2.amazonaws.com/youngwoo.lab/download/tomcat_session_test_jsp/tomcat_session_test.jsp
--2020-07-31 14:40:34-- https://s3.ap-northeast-2.amazonaws.com/youngwoo.lab/download/tomcat_session_test_jsp/tomcat_session_test.jsp
Resolving s3.ap-northeast-2.amazonaws.com (s3.ap-northeast-2.amazonaws.com)... 52.219.58.117
Connecting to s3.ap-northeast-2.amazonaws.com (s3.ap-northeast-2.amazonaws.com)|52.219.58.117|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2922 (2.9K) [binary/octet-stream]
Saving to: ‘tomcat_session_test.jsp’
100%[======================================================================================================>] 2,922 --.-K/s in 0s
2020-07-31 14:40:34 (172 MB/s) - ‘tomcat_session_test.jsp’ saved [2922/2922]
[root@ip-172-31-86-130 sample]# ls
META-INF WEB-INF hello.jsp images index.html session_check.jsp tomcat_session_test.jsp
서비스 재시작
[root@ip-172-31-86-130 download]# systemctl restart tomcat
여기까지 모든 톰캣 서버에 동일하게 진행했나요?!
진행 했다면 세션유지 테스트로 넘어가고, 아니라면 모든 톰캣서버에 동일하게 진행합니다.!
세션 유지 확인테스트
재시작되는데 시간이 소요되므로 약 5분~10분후 확인 권장
크롬 시크릿창에서 ELB경로/sample/tomcat_session_test.jsp 확인
아래 사진처럼 Server IP는 바뀌지만 SessionID가 바뀌면 정상입니다! 😁
'AWS :D' 카테고리의 다른 글
[AWS EventBridge] Root 유저 로그인시 e-mail(알림) 보내기! (0) | 2020.09.04 |
---|---|
[AWS IAM] 특정 리전에서만 사용 가능정책, MFA force 정책 적용하기 (0) | 2020.09.04 |
[AWS AppStream] Stack & Fleet 구성하기 (0) | 2020.07.13 |
[AWS EC2-Linux] monitoring Linux metric and logs with Amazon CloudWatch (0) | 2020.07.03 |
[AWS EC2 - Disk] EBS IOPS 구하기! (0) | 2020.06.17 |