项目场景:
系统:Windows 7
Tomcat:apache-tomcat-8.0.5
JDK:1.8
问题描述
最近项目的Tomcat隔一段时间就假死,最后想到的解决方式就是:每天凌晨1点重启tomact。
解决方案:
使用Windows系统的计划任务程序,可以在这里设置定时执行的.bat批处理文件(将你要定时执行的cmd命令放在这里),这样就可以实现让电脑在某个时刻做你想让它干的事。
实现步骤:
一、创建tomcat重启的脚本
创建txt文件restart.txt,编辑内容,把下面的内容复制进去,然后把缀改为.bat,最后文件名为:restart.bat
echo 正在关闭Tomcat服务,请稍等......@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.rem ---------------------------------------------------------------------------
rem Stop script for the CATALINA Server
rem ---------------------------------------------------------------------------setlocalrem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHomeset "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExecrem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgscall "%EXECUTABLE%" stop %CMD_LINE_ARGS%:endecho 关闭Tomcat服务完成ping 127.0.0.1 -n 20
echo ******************************************echo 正在启动Tomcat服务,请稍等......@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------setlocalrem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHomeset "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExecrem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgscall "%EXECUTABLE%" start %CMD_LINE_ARGS%:endecho 启动Tomcat服务完成
把restart.bat 文件复制到apache-tomcat-8.0.50\bin目录下 ,然后可以直接双击运行,启动会报个错,是因为tomcat没启动,是正常的。
二、创建任务计划程序
1、开始菜单搜索“任务计划程序”
或者到控制面板里面找到“计划任务”
2、弹出任务计划程序界面,在右侧点击创建任务
3、输入 名称和描述
4、点击触发器选项卡,点击左下角新建,弹出新建操作,新建一个触发器,每天凌晨一点触发
5、点击操作选项卡,点击左下角新建,弹出新建操作,点击浏览。选择要执行的脚本restart.bat ,点击打开确定。
附加:
这里说下nginx配置集群和tomcat实现session共享
首先要启动两个tomcat,然后端口不一样,比如8889和8899
1、nginx.conf关键配置
upstream serverlist {#最少连接数,根据连接数多少自动选择后端服务器,连接数相等时根据权重选择least_conn;#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题#ip_hash; #若120秒内出现2次错误,则下个120秒内不再访问此服务器,weight权重越大,请求机会越多server 127.0.0.1:8889 max_fails=2 fail_timeout=120s weight=1;#主服务server 127.0.0.1:8899 max_fails=2 fail_timeout=120s weight=1 backup;#备用tomcat服务,主服务挂了会调用备用服务
}
2、tomcat实现session共享
server.xml在Engine标签内加上配置
<Engine name="Catalina" defaultHost="localhost"><!-- tomcat集群session共享 --><Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
</Engine>
修改web项目,工程WEB-INF下的web.xml,添加如下一句配置
<distributable />