[NCP - Cloud DB for Redis & Tomcat 연동 (session clustering)]

Naver Cloud에서 제공하는 Cloud for DB Redis 서비스를 이용해서 was(tomcat)과 연동하여 session clustering을 진행하도록 하겠습니다.


테스트 환경

was1(tomcat) , was2(tomcat)
redis 서버(고가용성)

 

 

 

 테스트 시작

 

Redis 생성

버전과 라이센스 선택 불가..고가용성 선택 시 자동 리다이렉션 됩니다.


■  생성 후 아래와 같이 확인 가능하며 도메인 확인은 관리->Redis 설정 에서 가능합니다.

 



■  Redis에 접속할 서버를 redis DB ACG에 추가해줍니다.



 

■  생성 후 redis 접속을 위해 redis client 설치

root@was001:~# mkdir -p /etc/redis-client

root@was001:~# cd /etc/redis-client/


root@was001:/etc/redis-client# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2022-01-21 14:38:25-- http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/octet-stream]
Saving to: ‘redis-4.0.6.tar.gz’

redis-4.0.6.tar.gz 100%[====================================================================================================>] 1.64M --.-KB/s in 0.06s

2022-01-21 14:38:26 (25.6 MB/s) - ‘redis-4.0.6.tar.gz’ saved [1723533/1723533]




root@was001:/etc/redis-client# tar -zxvf redis-4.0.6.tar.gz



root@was001:/etc/redis-client# cd redis-4.0.6/



root@was001:/etc/redis-client/redis-4.0.6# make
...생략
siphash.c:211:5: note: here
case 1: b |= ((uint64_t)siptlw(in[0])); break;
^~~~
CC rax.o
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory '/etc/redis-client/redis-4.0.6/src'


## Redis dns 및 접속 포트 확인 후 접속
root@was001:/etc/redis-client/redis-4.0.6# cd src/


## 접속 및 접속 확인
root@was001:/etc/redis-client/redis-4.0.6/src# ./redis-cli -h redisc-9cikc.vpc-cdb.ntruss.com -p 6379
redisc-9cikc.vpc-cdb.ntruss.com:6379>
 
 

Redis & Tomcat 연동

Tomcat 서버 ACG에 redis 6379 포트 추가

 

■ redis-cluster-session 파일 download

root@was001:~# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/3.0.3/tomcat-cluster-redis-session-manager.zip
...생략
edential=AKIAIWNJYAX4CSVEH53A%2F20220121%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220121T054525Z&X-Amz-Expires=300&X-Amz-Signature=7109d6856d0440f54813983163c7201d8cc2e6d477dfa6587f92a761d856e352&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=54272419&response-content-disposition=attachment%3B%20filename%3Dtomcat-cluster-redis-session-manager.zip&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 906134 (885K) [application/octet-stream]
Saving to: ‘tomcat-cluster-redis-session-manager.zip’

100%[===================================================================================================================================================>] 906,134     --.-K/s   in 0.008s  

2022-01-21 14:45:26 (110 MB/s) - ‘tomcat-cluster-redis-session-manager.zip’ saved [906134/906134]



■  해당 파일에 있는 lib폴더와 conf 폴더의 파일들을 tomcat의 lib, conf 디렉토리에 옮겨줍니다.


root@was001:~# unzip tomcat-cluster-redis-session-manager.zip
root@was001:~# cd tomcat-cluster-redis-session-manager/


root@was001:~# cp -R lib/* /var/lib/tomcat9/lib/
root@was001:~# cp -R conf/* /var/lib/tomcat9/conf


root@was001:~# cd /var/lib/tomcat9/lib/

root@was001:/var/lib/tomcat9/lib# ll
total 820
drwxr-xr-x 2 tomcat tomcat 4096 Jan 21 13:48 ./
drwxr-xr-x 5 root root 4096 Jan 21 13:46 ../
-rw-r--r-- 1 root root 61829 Jul 21 2017 commons-logging-1.2.jar
-rw-r--r-- 1 root root 111969 Jul 20 2017 commons-pool2-2.4.2.jar
-rw-r--r-- 1 root root 553762 Jul 20 2017 jedis-2.9.0.jar
-rw-r--r-- 1 root root 93625 Jan 21 13:48 tomcat-cluster-redis-session-manager-2.0.4.jar
 
 

■  redis-data-cache.properties 파일 수정

Cloud DB for Redis  유저  관리  부분에서  유저명을 default 로  지정  해줘야지  사용가능합니다.

root@was001:~# vi /var/lib/tomcat9/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=redisc-9cikc.vpc-cdb.ntruss.com:6379 //redis ip와 port를 넣어줍니다.

#- 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

 


■  session.jsp 파일 tomcat의 webapp 경로에 생성

root@was001:~# vi  /var/lib/tomcat9/webapps/ROOT/session.jsp

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import = "java.net.InetAddress" %>
<%@ page import = "java.net.UnknownHostException" %>
<HTML>

<HEAD>

    <TITLE>Session Clustering Test WAS1</TITLE>

</HEAD>

<BODY>

<h1>Session Clustering Test  <%= request.getServerName() %></h1>
<%=InetAddress.getLocalHost().getHostName()%>
<%=InetAddress.getLocalHost().getHostAddress()%>
<%

    Integer ival = (Integer)session.getAttribute("_session_counter");
    if(ival==null) {

        ival = new Integer(1);

    }

    else {

        ival = new Integer(ival.intValue() + 1);

    }

    session.setAttribute("_session_counter", ival);

    System.out.println("here~~~~"+ival);
%>

Session Counter = [<b> <%= ival %> </b>]<p>

<a href="./session.jsp">[Reload]</a>

<p>

Current Session ID : <%= request.getRequestedSessionId() %><br />

</BODY>



</HTML>
 
 
 
 
 

■  tomcat 재부팅

root@was001:/var/lib/tomcat9/webapps/ROOT# systemctl restart tomcat9.service
 
 
 
 

 세션 확인 테스트

 

■  브라우저에서 tomcat 접속

http://<LB도메인>/session.jsp

ex)http://vpc-lb-9833762-ae4ee6d3937f.kr.lb.naverncp.com/session.jsp

■  새로고침하면 session 카운트가 올라가면서 동일한 session ID값을 갖고 있는것을 확인

 

■  tomcat1 번 stop

root@was001:~# systemctl stop tomcat9.service

 

 

■  was002로 바뀌며 sessionID는 동일 한것 확인