linux部署(一)组件部署

一、部署war包项目:

一般是spring项目,当然也有springboot项目打成war包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.xxx</groupId>
	<artifactId>xxx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>xxx</name>

	<profiles>
		<!-- 测试环境 -->
		<profile>
			<id>test</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<spring.profiles.active>test</spring.profiles.active>
			</properties>
		</profile>
		<!-- 开发环境 -->
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<spring.profiles.active>dev</spring.profiles.active>
			</properties>
		</profile>
		<!-- 生产环境 -->
		<profile>
			<id>prod</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<spring.profiles.active>prod</spring.profiles.active>
			</properties>
		</profile>
	</profiles>

	<build>
		<finalName>xxx</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<nonFilteredFileExtensions>
						<nonFilteredFileExtension>xls</nonFilteredFileExtension>
						<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
					</nonFilteredFileExtensions>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
1、部署方式:

使用tomcat服务器来部署war包。

1.1、 可以临时上传一个新的tomcat;

1.2、也可以复制已有的tomcat:

(1)停止old下项目的进程(防止产生两个进程);

(2)复制old为new,并删除new下temp、work、logs和webapps目录下面的全部内容;

(3)再启动old。

2、部署流程
2.1、配置

主要是tomcat的端口号配置。修改tomcat下sever.xml的几个端口号(tomcat7有两个,tomcat8则有三个端口号,都需要改为不同的);

2.2、上传

上传war包到tomcat的webapps目录下;

2.3、启动

启动文件在tomcat的bin目录下,一般目命名为startup.sh(.sh为linux使用,.bat为windows使用),进入bin目录,执行./startup.sh   或sh startup.sh start即可。

./startup.sh

常见问题:

(1)从其他地方复制过来的startup.sh文件有时候可能不是可执行文件,可以chmod 777 startup.sh来赋权限就可以了;

   (2)执行启动命令后报错-bash: ./startup.sh: /bin/sh^M: bad interpreter: No such file or directory,Windows环境下dos格式文件传输到unix系统时,会在每行的结尾多一个^M,所以在执行的时候出现了这种现象,但是你在unix或者Linux环境下使用vi编辑的时候,会在下面显示此文件的格式,比如”sky8g.sh” [dos] 2L,20C字样,表示这是一个【dos】的格式文件,如果是MAC的系统则会出现【MAC】的字样,因为文件格式的原因,有时候我们是unix程序或shell程序,则就要把dos文件转化为unix的文件格式。解决方法:执行sed -i -e 's/\r$//'  startup.sh。

2.4、验证

下面jar包的验证方式同此。

(1)可以通过查看进程的方式:

① 查看进程:

ps aux | grep 包名关键字

② 同查看日志的方式查找进程

ps -ef|grep 关键字

  bin目录下执行

ps -ef|grep `pwd`

为查看当前目录下的进程,如是在bin目录下,则是查看该项目的进程。

③ 查看端口占用命令:

 netstat -anop|grep port,如:netstat -anop|grep 8090

(2)也可以通过查看日志验证。tomcat自带一个日志目录atalina.out,如果项目中指定了logback日志文件,也可以去指定的日志目录查看。

2.5、关闭

下面jar包的关闭方式同此。

可以查看进程后,直接kill -9 杀死进程。

二、部署jar包项目:

一般是springboot项目。

1、部署方式:
1.1、使用tomcat部署

比较少见。jar包可以使用java -jar命令启动,springboot内置了tomcat,所以一般不需要依赖tomcat服务器。

1.2、java -jar启动

具体也有两种方式:

(1)手动执行命令启动;

(2)像tomcat一样定义一个启动脚本,封装启动命令,使用脚本启动。

下面看下具体的的部署流程:

2、命令启动方式部署jar包
2.1、代码

首先pom需要加入打包依赖

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
2.2、打包上传

执行mvn clean package打包后把生成的jar包上传到服务器

2.3、执行启动命令

(1)最简单的jar包运行命令:

java -jar xxx.jar

缺点是:当前ssh窗口被锁定,可按CTRL + C打断程序运行或直接关闭窗口,这时程序会退出 

(2)窗口不被锁定

java -jar xxx.jar &

在上面的基础上后面加个&,代表当前ssh窗口不被锁定,但是当窗口关闭后,程序会被中止 

(3)账户退出或终端关闭时,程序仍然运行:

java -jar xxx.jar >/dev/null 2>&1 &

还可以指定输出目录

java -jar xxx.jar  > log.file  2>&1 &

 数字1、2的意思是:

0    标准输入(一般是键盘)
1    标准输出(一般是显示屏,是用户终端控制台)
2    标准错误(错误信息输出)
 
将运行的jar 错误日志信息输出到log.file文件中,然后(>&1)就是继续输出到标准输出
(前面加的&,是为了让系统识别是标准输出),最后一个&,表示在后台运行。
2.4、验证

 启动后显示的数字就表示运行的pid,netstat -anp可以查看到。

3、脚本启动方式部署jar包
3.1、代码改造

(1)自定义脚本

  src/main下创建文件夹assemble和bin

assemble下创建文件package.xml,

<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>

   <fileSets>
        <fileSet>
            <filtered>true</filtered>
            <directory>src/main/bin</directory>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
        <fileSet>
            <filtered>false</filtered>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**</include>
            </includes>
        </fileSet>
        <fileSet>
            <filtered>true</filtered>
            <directory>src/main</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>readme.txt</include>
            </includes>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
            	<exclude>org.springframework.boot:spring-boot-devtools</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

bin下创建启动脚本startup.sh:

#!/bin/sh

#if [ -z "$1" ]; then
#	echo "请在参数中指定进程Id文件的名称!"
#	exit 1
#fi

CURRENT_DIR=$(pwd)
PROJECT_DIR=$CURRENT_DIR"/.."

# echo $PROJECT_DIR

CLASSPATH=
CLASSPATH=$CLASSPATH:$PROJECT_DIR

CLASSPATH=$CLASSPATH:$CURRENT_DIR"/../lib/*"

# echo $CLASSPATH

APPNAME=com.demo.FileUploadApplication

java -Xms256m -Xmx256m -classpath $CLASSPATH $APPNAME start >/dev/null 2>&1 & 

echo $! > "./FileUploadApplication.pid"

echo "started"

  把start后面的>/dev/null 2>&1 &去掉,日志会打印到控制台上,不过ctrl+c或者断开连接后这个服务会自动停止。

java -Xms2048M -Xmx2048M -classpath $CLASSPATH $APPNAME start >/usr/logs/app.log 2>&1 &

则可以把日志输出到指定目录(/usr/logs/app.log),而ctrl+c或者断开连接也不会停止服务。 

java -jar xxx >&1 &表示断开连接不终止服务。如

nohup java -jar my-service.jar >/dev/null 2>&1 &

(2)pom文件加入打包命令:

 <build>
    <finalName>file-upload-job</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-release-plugin</artifactId>
       
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assemble/package.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.html</exclude>
            <exclude>config</exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
 3.2、项目打包上传

mvn clean package打包,把target下生成的zip包上传到指定目录;

3.3、启动

解压zip,进入bin目录启动项目

3.4、验证:

可以通过查看进程的方式验证,也可以通过查看日志验证。

三、部署vue项目

1、部署方式

vue项目打包后是一个名为dist的静态文件,因为服务器一般限制资源随意访问,所以dist的访问需要依赖其他服务器,一般有两种方式:

1.1、nginx代理

使用Nginx代理静态文件。

1.2、tomcat

这里的tomcat可以是真实的tomcat,也可以是springboot内置的tomcat。一般的做法是:

将dist放到后端项目(war包、jar包)的静态目录下(/src/main/resources下),部署war包或jar包后,通过后端项目的ip:端口/静态目录地址来访问。这种方式同war、jar的部署。

2、nginx代理部署流程
2.1、打包

本地执行npm run build打包命令后生成dist包。

2.2、上传

将该dist包下的所有内容上传到某一目录,如上传到/usr/html/mydemo下。

2.3、代理

nginx代理该静态目录,如配置http://mydemo指向/usr/html/mydemo,则访问http://mydemo#login即可。

2.4、重新部署

即把mydemo目录下内容替换成新的即可,只要ngix配置没有做修改,则不需要执行其他命令。

四、引用本地自定义的jar包

如本地自定义api,现在远程部署service,依赖了api,一般方式是把api部署到私服上,service项目拉取私服依赖。如果没有私服可以这样做:

1、api的src/main下新建assemble文件夹,assemble下新建package.xml(具体如上springboot的打包方式);

2、service:为了打包能够引入本地api jar包,在与pom同级目录创建thirdlibs目录,将步骤一中生成的api jar包拷贝进去,同时pom文件中依赖api的配置需要修改下:

  <!-- api -->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>mysercurity-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>system</scope>
            <!--      <systemPath>E:\workspace8\mysercurity-api\target\mysercurity-api.jar</systemPath>-->
            <systemPath>${project.basedir}/thirdlibs/mysercurity-api.jar</systemPath>
        </dependency>

再打包service即可。注意这样只是为了serivce打包过程中不会报错,服务部署后还是需要手动复制下api的jar包到service的lib目录下。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值