今天经理提了一个要求,要求将原本的拖拽式项目更新,
改为直接直接打成整个jar包进行更新
而且要求这个jar包是包含的内置tomcat的,不再部署到外部tomcat,要怎么做呢?
一、设置项目端口
由于springboot内置tomcat,直接启动springboot项目也是要指定端口号的。我们先看一下之前的tomcat配置端口是怎么配置的
<?xml version="1.0" encoding="UTF-8"?>
<Server port="7051" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8083" protocol="HTTP/1.1"
maxHttpHeaderSize="81920" useBodyEncodingForURI="true"
maxThreads="2000" redirectPort="8443" enableLookups="false"
compression="on" compressionMinSize="2048" URIEncoding="utf-8" EncodingForURI="true"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="300000" disableUploadTimeout="true"/>
<!--
<Connector port="7041" protocol="AJP/1.3" redirectPort="8443" />
-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" debug="0"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="server" docBase="/opt/AppData/ipas" debug="0" reloadable="false" >
<!-- <Resources allowLinking="true"/> -->
</Context>
<!-- <Context path="/bms" docBase="/opt/AppData/ipas-client" reloadable="false" /> -->
</Host>
</Engine>
</Service>
</Server>
我们可以观察到端口是8083,Context path=“server” docBase="/opt/AppData/ipas"就配置了我们原来项目的位置
接着我们需要在我们自己的项目里引入端口号,就用自己的项目内置tomcat启动了。
我们找到application.yml
将端口号改掉
# 测试环境
server:
port: 8083
main: http://地址:8083/
二、将分立的lib包形式改成打成一整个jar包的形式
首先要在根目录下新建一个pom-test文件,命令行到时后会执行这个pom-test文件里的命令,
此处可以用这种方式在根目录下指定java项目也用下面的新的pom文件的形式,其次要在java项目下要引入一个新的pom文件,用新的形势打包,
要改的pom文件是你要打包的java项目的的pom文件,不是整个项目的根pom文件
对比内容改动的是如下部分
此为java项目下的pom-test文件(而不是根目录下的)
其中IPasApplication是启动类的的位置
@EnableScheduling
@EnableConfigurationProperties
@SpringBootApplication(scanBasePackages = "com.haoyu")
@MapperScans({
@MapperScan(value = "com.test", markerInterface = com.haoyu.framework.core.base.BaseMapper.class)
})
@Import({cn.hutool.extra.spring.SpringUtil.class})
@ImportResource("classpath:tx.xml")
public class IpasApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(IpasApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(IpasApplication.class);
}
}
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- main 入口 -->
<mainClass>com.test.ipas.IpasApplication</mainClass>
<!-- 设置为ZIP,此模式下spring-boot-maven-plugin会将Manifest.MF文件中的Main-Class设置为org.springframework.boot.loader.PropertiesLauncher -->
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<!-- 或者是-->
<!-- <configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
</configuration>-->
</plugin>
然后命名为pom-test,
最后在根目录pom-test文件中指定java项目使用哪个pom文件打包,这里写的是java项目用java项目下的pom-test文件
两个pom-test文件是不一样的
运行pom-test该怎么运行呢?
点击idea右上角m字样
输入mvn clean package -f pom-test.xml
其中pom-test是我们的pom文件名
完成后在target文件夹下就打包成一个jar包了
三、使用shell脚本启动jar包
由于这个jar包内置了一个tomcat的,这里是linux环境,所以使用shell脚本启动
#!/bin/bash
JENKINS_NODE_COOKIE=dontKillMe
BUILD_ID=jiujiu
echo "--------staring $1-------------"
cd /opt/AppData/ipas-server
echo "--------nohup java -Xms512m -Xmx1024m -jar $1.jar --spring.profiles.active=test > log.out 2>&1 &-------------"
nohup java -Xms512m -Xmx1024m -jar $1.jar --spring.profiles.active=test > log.out 2>&1 &
echo "started $1"
echo "exit"
这里的cd位置是你的项目所在的文件夹,这里我让jar包和命令行处于同一文件夹下。命名为start.sh
下面的spring.profiles.active=test是指的使用后缀为test的application配置文件
有下面这行脚本,无论在application.yml里有没有设置使用当前配置文件都会生效
# 有了那行脚本就不用指定active了
spring:
profiles:
active: test
接下来就到了启动,启动命令
./start.sh ipas-server
需要start.sh里加jar包的名字。
过一会检查端口,发现已经启动了
检查端口命令:netstat -nlp|grep 端口号
关闭项目的命令如下
#!/bin/bash
echo "`jps -l | grep $1 | awk '{print $1}'`"
pid=`jps -l | grep $1 | awk '{print $1}'`
if [[ -n $pid ]]; then
echo "[$1] is running, killing................"
kill -9 $pid
sleep 1
else
echo "[$1] is not running..."
fi
关闭的话也是一样的,注意同样输入时要加上jar包的名字
./shutdown.sh ipas-server
四、配置nginx备用服务
线上环境要求nginx的一个域名配置两台服务器,这里不是均衡负载,仅仅是一台主用服务器一台备用服务器。在主服务器停掉的时候会启动备用服务器
这有什么好处呢?最大的一个好处就是可以不停服更新,想什么时候更就什么时候更,不用再找现场对接云云
找到原来的配置,位置位于nginx安装目录下 nginx/conf/nginx.conf
upstream ipas_server {
server 地址:服务器1端口号;
}
我们修改要怎么修改呢?加另外一个备用服务器的就可以了,后面加上backup
upstream ipas_server {
server 地址:服务器1端口号;
server 地址:服务器2端口号 backup;
}
完事后保存,先检查一下配置是否写正确
命令行进入nginx安装目录下的sbin文件夹,输入
/nginx/sbin/nginx -t
看到返回ok,successful就是正确无误的,
下面我在backup后面故意打多了一个1,就failed了
生效后输入nginx -s reload
进行配置重读,
很多人倾向先关nginx再开,但是生产环境的话一个nginx可能会配很多个项目,一个更新影响到别的就不好了。所以这里推荐直接重读配置
验证:
把“地址:服务器1端口号”的这个项目关掉,
在地址栏里重新输入,还可以访问,备用服务已经生效了