1.Nginx+Tomcat集群架构介绍
2.Nginx+Tomcat集群架构实战
[[email protected] conf.d]# cat proxy_zrlog.cheng.com.conf
upstream zrlog {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
}
server {
listen 80;
server_name zrlog.cheng.com;
location / {
proxy_pass http://zrlog;
include proxy_params;
}
}
3.Nginx+Tomcat集群会话共享 redis cluster
session测试代码用例
1.配置虚拟主机【web01,02操作】
[[email protected] conf]# vim /soft/tomcat/conf/server.xml
<!--session站点-->
<Host name="session.cheng.com" appBase="/code/session"
unpackWARs="true" autoDeploy="true">
</Host>
2.准备index.jsp文件(为了区分需要调整输出的web01 web02)
[[email protected] 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>
3.下载TomcatClusterRedisSessionManager (所有web集群都需要操作)
GitHub地址 https://github.com/ran-jit/tomcat-cluster-redis-session-manager
[[email protected] ~]# wget https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/download/3.0.3/tomcat-cluster-redis-session-manager.zip
[[email protected] ~]# unzip tomcat-cluster-redis-session-manager.zip
[[email protected] ~]# cd tomcat-cluster-redis-session-manager
3.1拷贝jar包
[[email protected] tomcat-cluster-redis-session-manager]# cp lib/* /soft/tomcat/lib/
3.2拷贝tomcat连接redis配置文件
[[email protected] tomcat-cluster-redis-session-manager]# cp conf/redis-data-cache.properties /soft/tomcat/conf/
3.3修改redis-data-cache.properties
[[email protected] ~]# vim /soft/tomcat/conf/redis-data-cache.properties
...
redis.hosts=172.16.1.51:6379
redis.password=123456 #有密码就写密码,没有不要写
...
4.添加如下两行至tomcat/conf/context.xml
[[email protected] ~]# 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>
6.安装redis,当然也可以自行搭建redis集群,anyway
[[email protected] ~]# yum install redis -y
[[email protected] ~]# cat /etc/redis.conf
...
bind 172.16.1.51 127.0.0.1
requirepass 123456 #如果不需要密码,则不要配置
...
[[email protected] ~]# systemctl start redis
[[email protected] ~]# systemctl enable redis
7.重启多台机器的Tomcat
[[email protected] ~]# /soft/tomcat/bin/shutdown.sh
[[email protected] ~]# /soft/tomcat/bin/startup.sh
8.接入负载均衡,通过负载均衡轮询调度检查是否正常
[[email protected] conf.d]# cat proxy_session.cheng.com.conf
upstream session {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
server 172.16.1.9:8080;
}
server {
listen 80;
server_name session.cheng.com;
location / {
proxy_pass http://session;
proxy_set_header Host $http_host;
}
}
9.如果session会话不正常:
将域名解析到指定的服务器,通过8080的方式去访问,测试,检查日志.
4.Nginx+Tomcat集群全站Https
1.单台:
1.http接收器修改为 80端口 ---> 443
2.配置443的证书
2.集群:
[[email protected] conf.d]# cat proxy_zrlog.cheng.com.conf
upstream zrlog {
server 172.16.1.7:8080;
server 172.16.1.8:8080;
server 172.16.1.9:8080;
}
server {
listen 443 ssl;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
server_name zrlog.cheng.com;
location / {
proxy_pass http://zrlog;
include proxy_params;
}
}
server {
listen 80;
server_name zrlog.cheng.com;
return 302 https://$http_host$request_uri;
}
5.Tomcat开启JMX监控
背景:Tomcat系统运行过程出现错误,需要打开JMX,添加对JVM的监控。Tomcat运行在CentOS中。
前提:监控端windows系统,安装JDK
步骤如下:
1. 服务器关闭Tomcat
[[email protected] conf]# /soft/tomcat/bin/shutdown.sh
2. 进入Tomcat/bin目录,修改catalina.sh,找到如下内容“#—–Execute The Requested Command”,在其上添加以下配置,此配置不需要用户名、密码
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=#tomcat本机IP地址
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"
ip是你要监控的tomcat所在服务器的ip地址
端口号,是你要开启的监控端口号。
ssl,false表示不使用ssl链接
authenticate,false表示不使用监控,即不需要用户名和密码
3. 服务器启动Tomcat
[[email protected] conf]# /soft/tomcat/bin/startup.sh
5. 做完以上操作后,使用jdk自带工具jvisualvm.exe连接,工具目录如下:JAVA_HOME/bin,连接方式如下:
原文地址:https://www.cnblogs.com/yinwu/p/11616464.html
时间: 2024-11-05 22:04:18