Tomcat+Nginx+Redis实践

Tomcat-学习目录

1、redis的方式来实现Tomcat的session共享-架构图

2、环境准备

2.1、主机准备

LB-nginx 192.168.10.4
tomcat1  192.168.10.5
tomcat2  192.168.10.7
redis    192.168.10.8

2.2、tomcat1、tomcat2代码准备

2.2.1、tomcat1

# tomcat1
mkdir /opt/session/ROOT -p
cat >/opt/session/ROOT/index.jsp<<'EOF'
<body>
<%
//HttpSession session = request.getSession(true);
System.out.println(session.getCreationTime());
out.println("<br> tomcat1 SESSION ID:" + session.getId() + "<br>");
out.println("Session created time is :" + session.getCreationTime()
+ "<br>");
%>
</body>
EOF

2.2.2、tomcat2

# tomcat2
mkdir /opt/session/ROOT -p
cat >/opt/session/ROOT/index.jsp<<'EOF'
<body>
<%
//HttpSession session = request.getSession(true);
System.out.println(session.getCreationTime());
out.println("<br> tomcat2 SESSION ID:" + session.getId() + "<br>");
out.println("Session created time is :" + session.getCreationTime()
+ "<br>");
%>
</body>
EOF

2.3、此前已经安装好Nginx负载均衡+Tomcat集群,tomcat1、tomcat2新增一个虚拟主机

]# vi /usr/local/tomcat/conf/server.xml 
...
    <Engine>
      <!-- session -- >
      <Host name="session.cyc.com"  appBase="/opt/session" unpackWARs="true" autoDeploy="true"></Host>
    </Engine>
...

2.4、配置nginx

cat >/etc/nginx/conf.d/session.cyc.com.conf <<'EOF'
upstream jsp_session {
  server 192.168.10.5:8080 max_fails=2 fail_timeout=10s;
  server 192.168.10.7:8080 max_fails=2 fail_timeout=10s;
}
server {
  listen 80;
  server_name session.cyc.com;
  location / {
    proxy_pass http://jsp_session;
    include proxy_params;
  }
}
EOF

2.5、配置hosts

192.168.10.4 session.cyc.com

2.6、测试访问

2.6.1、tomcat1

2.6.2、tomcat2

2.6.3、总结

测试访问,通过访问nginx,然后轮询调度到后端web集群,会发现不同主机的session不一致

3、配置tomcat-cluster-redis-session-manager方式实现redis共享会话

部署tomcat1、tomcat2

3.1、安装redis

3.2、下载tomcat-cluster-redis-session-manager软件

# 项目地址
https://github.com/ran-jit/tomcat-cluster-redis-session-manager

# 下载版本
wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/3.0.3/tomcat-cluster-redis-session-manager.zip

3.3、解压软件并且复制jar包至tomcat lib

unzip tomcat-cluster-redis-session-manager.zip
cp tomcat-cluster-redis-session-manager/lib/* /usr/local/tomcat/lib/

3.4、将redis-data-cache.properties文件移动到tomcat/conf目录并且配置

cp tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties /usr/local/tomcat/conf/

]# vi /usr/local/tomcat/conf/redis-data-cache.properties 
...
redis.hosts=192.168.10.8:6379
...

3.5、context.xml配置

]# vi /usr/local/tomcat/conf/context.xml
<Context>
...
    <Valve  className="tomcat.request.session.redis.SessionHandlerValve"    />
    <Manager        className="tomcat.request.session.redis.SessionManager" />
...
</Context>

3.6、修改tomcat/conf/web.xml中的会话过期时间(60分钟)

]# vi /usr/local/tomcat/conf/web.xml
<session-config>
  <session-timeout>60</session-timeout>
</session-config>

3.7、重新tomcat1、tomcat2

systemctl restart tomcat

3.8、访问测试

3.8.1、tomcat1

 

3.8.2、tomcat2

 

3.8.3、查询redis

~]# redis-cli 
127.0.0.1:6379> keys *
1) "F6ED82CFC0F10836FEF9EEC611AA5B42"

3.8.4、总结

通过nginx进行访问,每次刷新SessionID都是一致的,说明基于Redis的session共享方式部署成功.

 

posted @ 2023-05-04 11:13  小粉优化大师  阅读(221)  评论(0编辑  收藏  举报