目录
1、创建虚拟主机(01-02)相同【192.168.200.121、192.168.200.122】
2、LB配置文件【192.168.200.120】
3、tomcat节点配置TomcatClusterRedisSessionManager方式实现redis共享(01-02)相同
4、安装redis、配置【192.168.200.123】
5、重启tomcat
6、测试访问,查看session不会再发生改变
创建虚拟主机(01-02)相同【192.168.200.121、192.168.200.122】
[root@nginx zrlog]# vim /soft/tomcat/conf/server.xml
<Host name="www.test-session.org" appBase="/code/session"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="session_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %{X-Forwarded-For}i" />
</Host>
- 创建目录、代码
[root@nginx zrlog]# mkdir /code/session/ROOT -p
[root@nginx zrlog]# vim /code/session/ROOT/index.jsp
<body>
<%
//HttpSession session = request.getSession(true);
System.out.println(session.getCreationTime());
out.println("<br> web01 SESSION ID:" + session.getId() + "<br>");
out.println("Session created is:" + session.getCreationTime() + "<br>");
%>
</body>
LB配置文件【192.168.200.120】
[root@nginx nginx]# vim /etc/nginx/conf.d/session.conf
upstream session {
server 192.168.200.121:8080;
server 192.168.200.122:8080;
}
server {
listen 80;
server_name www.test-session.org;
location / {
proxy_pass http://session;
include proxy_params;
}
}
[root@nginx nginx]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_connect_timeout 60s; # nginx连接后端的超时时间
proxy_read_timeout 60s; # 响应头部超时时间
proxy_send_timeout 60s; # 响应数据主体的超时时间
proxy_buffering on; # 开启缓冲区
proxy_buffer_size 8k; # 缓冲区Header大小
proxy_buffers 4 64k; # 缓冲区数量 * 大小 = 最大接收
tomcat节点配置TomcatClusterRedisSessionManager方式实现redis共享(01-02)相同
[root@nginx ~]# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/4.0/tomcat-cluster-redis-session-manager.zip
## 解压
[root@nginx ~]# unzip tomcat-cluster-redis-session-manager.zip
## 拷贝jars到tomcat/lib目录中
[root@nginx ~]# cp tomcat-cluster-redis-session-manager/lib/* /soft/tomcat/lib/
## 拷贝配置文件到/conf中
[root@nginx ~]# cp tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties /soft/tomcat/conf/
## 修改配置文件
[root@nginx ~]# grep "^[a-z]" /soft/tomcat/conf/redis-data-cache.properties
redis.hosts=192.168.200.123:6379
redis.cluster.enabled=false
redis.sentinel.enabled=false
redis.sentinel.master=mymaster
redis.timeout=2000
lb.sticky-session.enabled=false
session.persistent.policies=DEFAULT
redis.sso.timeout=0
## 修改context.conf中添加以下两行
[root@nginx ~]# vim /soft/tomcat/conf/context.xml
<Context>
.....
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
.....
</Context>
## 调整session的过期时间到60s,默认时30分钟,可以不用修改
[root@nginx ~]# vim /soft/tomcat/conf/web.xml
<session-config>
<session-timeout>30</session-timeout>
</session-config>
安装redis、配置【192.168.200.123】
[root@redis ~]# yum -y install redis
[root@redis ~]# sed -i '/^bind/c bind 127.0.0.1 192.168.200.123' /etc/redis.conf
[root@redis ~]# systemctl restart redis
[root@redis ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.200.123:6379 0.0.0.0:* LISTEN 3336/redis-server 1
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 3336/redis-server 1
重启tomcat
[root@nginx ~]# systemctl stop tomcat.service
[root@nginx ~]# systemctl start tomcat