# Redis 직접설치

sudo apt-get install redis-server

redis-server --version

sudo systemctl restart redis-server.service

netstat -nlpt | grep 6379

redis-cli

set [key] [value]

# Redis 도커설치

docker pull redis

docker network create redis-net

docker run --name myredis -p 6379:6379 --network redis-net -d redis redis-server --appendonly yes

docker run -it --network redis-net --rm redis redis-cli -h myredis

 

# Tomcat 서버에 Redis Session Manager 다운로드 및 tomcat 설정 변경 

sudo wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/2.0.4/tomcat-cluster-redis-session-manager.zip

(sudo apt install unzip)

sudo unzip tomcat-cluster-redis-session-manager.zip

sudo cp ./tomcat-cluster-redis-session-manager/lib/* /usr/local/tomcat9/lib/
sudo cp ./tomcat-cluster-redis-session-manager/conf/* /usr/local/tomcat9/conf/

sudo chown -R ubuntu: tomcat1

 

 

# redis-data-cache.properties 수정

vi /usr/local/tomcat9/conf/redis-data-cache.properties

redis.hosts=127.0.0.1:6379  ## redis endpoint 추가

 

# web.xml 수정

vi /usr/local/tomcat9/conf/web.xml

    <session-config>
        <session-timeout>60</session-timeout> <!-- 60 으로 변경 -->
    </session-config>

    
# context.xml 수정

vi /usr/local/tomcat9/conf/context.xml

<Context>  
...
  <!-- 아래 내용 추가 -->
  <Valve className="tomcat.request.session.redis.SessionHandlerValve" />
  <Manager className="tomcat.request.session.redis.SessionManager" />
...
</Context>

 

# 다른 톰켓서버를 내려도 세션 ID는 유지

<%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%
        String RsessionId = request.getRequestedSessionId();
        String sessionId = session.getId();
        boolean isNew = session.isNew();
        long creationTime = session.getCreationTime();
        long lastAccessedTime = session.getLastAccessedTime();
        int maxInactiveInterval = session.getMaxInactiveInterval();
        Enumeration e = session.getAttributeNames();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Session Test</title>
</head>
<body>
<table border=1 bordercolor="gray" cellspacing=1 cellpadding=0
        width="100%">
        <tr bgcolor="gray">
                <td colspan=2 align="center"><font color="white"><b>Session
                Info</b></font></td>
        </tr>
        <tr>
                <td>Server HostName</td>
                <td><%=java.net.InetAddress.getLocalHost().getHostName()%></td>
        </tr>
        <tr>
                <td>Server IP</td>
                <td><%=java.net.InetAddress.getLocalHost()
                                                                        .getHostAddress()%></td>
        </tr>
        <tr>
                <td>Request SessionID</td>
                <td><%=RsessionId%></td>
        </tr>
        <tr>
                <td>SessionID</td>
                <td><%=sessionId%></td>
        </tr>
        <tr>
                <td>isNew</td>
                <td><%=isNew%></td>
        </tr>
        <tr>
                <td>Creation Time</td>
                <td><%=new Date(creationTime)%></td>
        </tr>
        <tr>
                <td>Last Accessed Time</td>
                <td><%=new Date(lastAccessedTime)%></td>
        </tr>
        <tr>
                <td>Max Inactive Interval (second)</td>
                <td><%=maxInactiveInterval%></td>
        </tr>
     <tr bgcolor="cyan">
                <td colspan=2 align="center"><b>Session Value List</b></td>
        </tr>
        <tr>
                <td align="center">NAME</td>
                <td align="center">VAULE</td>
        </tr>
        <%
                String name = null;
                while (e.hasMoreElements()) {
                        name = (String) e.nextElement();
        %>
        <tr>
                <td align="left"><%=name%></td>
                <td align="left"><%=session.getAttribute(name)%></td>
        </tr>
        <%
                }
        %>

</table>

        <%
                int count = 0;

                if(session.getAttribute("count") != null)
                        count = (Integer) session.getAttribute("count");

                count += 1;

                session.setAttribute("count", count);

                out.println(session.getId() + "     :     " + count);
        %>
</body>
</html>

 

[참고]

https://cleanupthedesk.tistory.com/42

https://hongddo.tistory.com/89

 

 

+ Recent posts