1.问题
每次修改代码后,重启tomcat,客户就无法使用。
而且项目里面大量使用session存储零时数据,部署多台又没有做session共享。
2.解决办法
使用nginx-backup方式代理两台tomcat,两台tomcat都部署同一套代码,一台为主,一台为辅
1.可以将两台tomcat都启动,高可用。
2.也可以只在需要更新代码时才启动辅tomcat,主tomcat重启完成后停掉辅tomcat,并将辅tomcat代码也更新,节约服务器资源。
3.步骤
安装步骤省略,可参考链接:
https://www.runoob.com/linux/nginx-install-setup.html
3.1.nginx配置
文件:nginx-xxx/conf/nginx.conf
upstream tomcat_server{
#代理的主tomcat
server localhost:8080 weight=1 max_fails=5 fail_timeout=60s;
#代理的辅tomcat,配置backup
server localhost:8082 weight=1 max_fails=5 fail_timeout=60s backup;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
# 4.指定虚拟目录
location /{
root html;
index index.html index.htm;
#404不转到备份机,502/503/504才转到备机
proxy_next_upstream error timeout http_502 http_503 http_504 ;
proxy_pass http://tomcat_server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
}
}
注意:必须给辅tomcat配置backup属性,否则将会默认以轮询方式访问
3.2修改tomcat端口
准备两台tomcat,这里主tomcat默认8080,所以只配置辅tomcat配置为8082
文件位置:apache-tomcat-xxx/conf/server.xml
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8444"/>
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/>
3.3 nginx启动
进入nginx目录
nginx # 启动
启动tomcat,进入浏览器访问.
IP/项目名称/
nginx其他命令
nginx -s reload # 重新载入配置文件
nginx -s reopen # 重启 Nginx
nginx -s stop # 停止 Nginx