前言
小编刚完成了一个部署Java服务的任务,大概有三个点可以介绍,Tomcat服务安装修改配置文件、Nginx路由配置、编写shell脚本部署服务以及遇到的几个相关坑,记录下来,分享给大家!技术要点
一、安装Tomcat服务,修改配置文件
1.安装Tomcat服务 // 解压tomcat的压缩包
tar -zxvf apache-tomcat-8.5.54.tar.gz
// 重命名 Tomcat服务名称:方便标识,一个服务器中的多个Tomcat服务
mv apache-tomcat-8.5.54 apache-tomcat-8.5.54_8040
2.修改配置文件server.xml
(1)使用vim 命令打开配置文件,按键 i 进行编辑
vim apache-tomcat-8.5.54_8040/conf/server.xml
(2)修改http请求的端口号
<Connector port="8040" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
二、Nginx配置,添加路由
server{
listen 80;
server_name 域名;
access_log /var/log/nginx/oapi-access.log main;
root /home/appserver/apache-tomcat-8.5.54_8040/webapps/ROOT/;
index index.html index.htm;
location /oapi {
charset utf-8;
proxy_pass http://paas-oapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream paas-oapi{
#ip_hash;
server 10.0.0.66:8040;
server 10.0.0.67:8040;
}
三、Shell脚本
#!/bin/bash
today=`date '+%Y%m%d'`
mkdir -p /tmp/$today/new
mkdir -p /tmp/$today/old
mkdir -p /tmp/$today/rbbak
newpath=/tmp/$today/new/
oldpath=/tmp/$today/old/
project=oapi
tomcat=/home/appserver/apache-tomcat-8.5.54_8040
cd $newpath
echo "downloading new war file......"
wget http://jk.hecom.cn/jobs-download/workspace/project-Install/paas/paas-oapi/paas-oapi-gateway/target/paas-oapi-gateway-1.0.war -O ${project}.war
newwar=`ls /tmp/$today/new/${project}.war 2>/dev/null | head -n 1`
if [[ -f "$newwar" ]];then
echo "new war file downloaded OK!!!!!!"
fi
echo "deploying new war file......"
backwar=`ls /tmp/$tody/rbbak/${project}.war 2>/dev/null | head -n 1`
if [[ -f "$backwar" ]];then
echo "Backup file exists"
else
cd ${tomcat}/webapps
cp -r ${project}* /tmp/$today/rbbak
fi
javaid0=`ps -ef|grep java|grep ${tomcat}|grep -v grep|awk '{print $2}'`
echo "tomcat process id: ${javaid0}"
kill -9 $javaid0
cd ${tomcat}/webapps
rm -rf ${project}*
cp -r /tmp/$today/new/${project}* .
cd ${tomcat}/work
rm -rf Catalina
cd ${tomcat}/logs
echo ' ' > catalina.out
cd ${tomcat}/bin
./startup.sh
tail -200f ${tomcat}/logs/catalina.out
跳坑经历
一、脚本文件没有执行权限
1.无执行权限的文件-rw-r--r-- 1 root root 29 8月 27 15:51 deploy_paas_oapi.sh
2.chmod 命令添加 x 权限
chmod +x deploy_paas_oapi.sh
3.成功添加执行权限
-rwxr-xr-x 1 root root 29 8月 27 15:51 deploy_paas_oapi.sh
二、Tomcat重名解压后,替换原有文件夹
Tomcat的压缩包与当前已安装Tomcat服务名称重复,导致解压后,替换了原有的Tomcat,解压前要注意这点,将当前服务重新命名后解压三、服务部署启动后,请求404
1.查看Nginx分发请求的日志//查看Tomcat接口请求日志(nginx配置文件中,配置的路径)
vim /var/log/nginx/oapi-access.log
2.定位接口具体调用哪个服务
在 /etc/nginx/nginx.conf中配置 请求的格式,带上详细的请求路由参数
log_format main '$http_x_forwarded_for [$time_local] "$request" '
'$status $bytes_sent $request_length $request_time "$http_referer" $http_uid $http_clientTag $http_version $http_app $http_entCode $http_obj $upstream_addr $upstream_response_time $http_act';
3.明确后,发现服务正常启动,但是没有初始化类,Java服务部署时缺少包,重新打包即可。