Nginx+Tomcat集群架构概述
tomcat+Nginx集群架构实战与共享
tomcat+Nginx集群会话
一、 Nginx +Tomcat集群架构概述
Tomcat集群能带来什么
- 提高服务的性能
- 提高项目架构的扩展能力
Tomcat集群实现原理 - 通过Nginx负载均衡进行请求转发
Tomcat集群架构图
Tomcat集群架构图
二、Tomcat+Nginx集群架构实战与共享
-
redis的方式来实现Tomcat的session共享,架构图:
会话共享 - ① 配置虚拟主机
[root@web01 conf]# vim /soft/tomcat/conf/server.xml
<!--session站点-->
<Host name="session.oldxu.com" appBase="/code/session"
unpackWARs="true" autoDeploy="true">
</Host>
- ②准备index.jsp文件(为了区分需要调整输出的web01 web02.使用vi编辑,因为vim带有格式)
[root@web01 conf]# cat /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 time is :" + session.getCreationTime()
+ "<br>");
%>
</body>
- ③下载Tomcat-Cluster-Redis-Session-Manager (所有web集群都需要操作)
GitHub地址 https://github.com/ran-jit/tomcat-cluster-redis-session-manager
[root@tomcat ~]# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/3.0.3/tomcat-cluster-redis-session-manager.zip
[root@tomcat ~]# unzip tomcat-cluster-redis-session-manager.zip
[root@web01 ~]# cd tomcat-cluster-redis-session-manager
1.拷贝jar包
[root@web01 tomcat-cluster-redis-session-manager]# cp lib/* /soft/tomcat/lib/
2.拷贝tomcat连接redis配置文件
[root@web01 tomcat-cluster-redis-session-manager]# cp conf/redis-data-cache.properties /soft/tomcat/conf/
3.修改redis-data-cache.properties
[root@web01 ~]# vim /soft/tomcat/conf/redis-data-cache.properties
...
redis.hosts=172.16.1.51:6379
redis.password=123456 #有密码就写密码,没有不要写
...
4.添加如下两行至tomcat/conf/context.xml
[root@web01 ~]# vim /soft/tomcat/conf/context.xml
<Context>
.....
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
....
</Context>
5.修改tomcat/conf/web.xml 配置文件session的超时时间 ,单位是分钟
<session-config>
<session-timeout>60</session-timeout> #根据情况调整
</session-config>
- ③ 安装redis,当然也可以自行搭建redis集群,anyway
[root@redis ~]# yum install redis -y
[root@redis ~]# cat /etc/redis.conf
...
bind 172.16.1.51 127.0.0.1
requirepass 123456 #如果不需要密码,则不要配置
...
[root@redis ~]# systemctl start redis
[root@redis ~]# systemctl enable redis
- ④ 重启多台机器的Tomcat(web01和web02b)
# pkill java
# /soft/tomcat/bin/startup.sh
- ⑤ 接入负载均衡,通过负载均衡轮询调度检查是否正常
[root@lb01 conf.d]# cat proxy_session.oldxu.com.conf
upstream session {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name session.oldxu.com;
location / {
proxy_pass http://session;
include proxy_params;
}
}
- ⑥ 域名解析并进行访问
- ⑦ Nginx+Tomcat集群全站Https
[root@lb01 conf.d]# cat proxy_session.oldxu.com.conf
upstream zrlog {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 443 ssl;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
server_name session.oldxu.com;
location / {
proxy_pass http://zrlog;
include proxy_params;
}
}
server {
listen 80;
server_name session.oldxu.com;
return 302 https://$http_host$request_uri;
}
网友评论