简单springboot项目部署

配置文件外置,用shell脚本启动停止项目,拿xxl-job-admin项目作为例子(不知道这个项目的,百度一下就可以找到了),部署好的目录结构如下图。

bin目录存放startup.sh和shutdown.sh,启动/停止项目脚本

conf目录存放配置文件,application.properties、logback.xml等。

logs目录是启动项目时生成的

1. 打包

maven直接打包项目,将打包好的jar包放置某个目录下。打成jar包时项目中的配置文件可有可无,反正用不到。

2. 修改配置文件

application.properties文件,其中的port以及数据源连接信息根据项目部署的环境进行修改

logback.xml文件,可以不修改直接使用,可以修改对应的log.path,使生成的日志文件和项目在同一个目录下,改成 logs/xxl-jog-admin.log

 将以上两个配置文件修改好后放入conf目录中

3. 编写启动/停止脚本

startup.sh(根据实际情况修改即可)

#!/bin/sh
#JAVA_DEBUG='-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000'
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
root_dir=$(cd $script_dir/../;pwd)

echo "root dir is $root_dir"
logs_dir=$root_dir/logs
echo "logs dir is $logs_dir"
if [ ! -d "$logs_dir" ]; then
        echo "logs dir not exist,create logs dir $logs_dir"
        mkdir $logs_dir
fi
cd $root_dir

# 部署项目环境下的jdk地址下的/bin/java,根据实际情况修改
JAVA_EXE='/home/jdk/bin/java'
echo $JAVA_EXE
if [ -z $JAVA_EXE ]
then
    echo 'can not found java'
    exit 1
fi
cd $root_dir
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${root_dir}/logs/java_heapdump.hprof -Dfile.encoding=UTF-8"
# 对应的jar包名称,根据实际情况修改
JAVA_OPT="${JAVA_OPT} -jar ${root_dir}/xxl-job-admin-2.3.1.jar"
# 启动时使用的配置文件位置
JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${root_dir}/conf/application.properties"
JAVA_OPT="${JAVA_OPT} --logging.config=${root_dir}/conf/logback.xml"
nohup $JAVA_EXE $JAVA_OPT >>$logs_dir/app.log 2>&1 & echo $! >$script_dir/pidfile.txt

shutdown.sh(直接复制粘贴就好,startup.sh启动时会生成一个pidfile.txt文件,文件里存的是项目启动之后的pid,shutdown.sh直接kill这个pid就会停止项目)

#!/bin/sh
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
cd $script_dir
PID=`cat pidfile.txt`
kill $PID
while true
do
  ls /proc/$PID/cmdline >/dev/null 2>&1
  if [ $? == 0 ]
  then
    sleep 1    
  else
   break
  fi
  echo "closing......"
done
rm -f pidfile.txt

 编写好sh文件之后直接放入bin目录下,基本上这样就完成了。

进入bin目录,输入 ./startup.sh  ./shutdown.sh 这两个命令分别是启动和停止项目。(不懂的话可以去学习shell)

项目依赖外置的打包方式以及部署

1. 配置

<build>
        <!--最终jar包的名称不包含版本号-->
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 旧的打包方式jar里面包含依赖 -->
            <!--<plugin>-->
            <!--    <groupId>org.springframework.boot</groupId>-->
            <!--    <artifactId>spring-boot-maven-plugin</artifactId>-->
            <!--</plugin>-->

            <!-- 新的打包方式jar里面不包含依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!--jar包中不含以下文件-->
                    <excludes>
                        <exclude>application.properties</exclude>
                        <exclude>application-bjlt.properties</exclude>
                        <exclude>application-bjltcs.properties</exclude>
                        <exclude>application-dev.properties</exclude>
                        <exclude>application-pro.properties</exclude>
                        <exclude>logback.xml</exclude>
                        <exclude>notegw.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-config-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <includeScope>compile</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
	<id>distribution</id>
	<formats>
		<format>tar.gz</format>
	</formats>
	<baseDirectory>notegw</baseDirectory>
	<fileSets>
		<fileSet>
			<directory>${project.build.directory}</directory>
			<includes>
				<include>*.jar</include>
				<include>lib/*.jar</include>
			</includes>
			<outputDirectory></outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.build.directory}/conf</directory>
			<outputDirectory>conf</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${basedir}/bin</directory>
			<fileMode>0777</fileMode>
			<outputDirectory>bin</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${basedir}</directory>
			<includes>
				<include>README.txt</include>
				<include>releasenotes.txt</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

2. 启动脚本

startup.sh文件启动脚本

#! /bin/bash
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
root_dir=$(cd $script_dir/../;pwd)

logs_dir=$root_dir/logs
echo "logs dir is $logs_dir"
if [ ! -d "$logs_dir" ]; then
  	echo "logs dir not exist,create logs dir $logs_dir"
	mkdir $logs_dir
fi

cd $root_dir
JAVA=/home/ccpbx2/jdk-17.0.2+8/bin/java 
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${root_dir}/logs/java_heapdump.hprof -Dfastjson.parser.safeMode=true"
JAVA_OPT="${JAVA_OPT} -Dapp.home=${root_dir}"
APP_ARGS="--spring.config.location=${root_dir}/conf/"
APP_ARGS="${APP_ARGS} --logging.config=${root_dir}/conf/logback.xml"
nohup $JAVA $JAVA_OPT \
-Dfile.encoding=UTF-8 -cp lib/*:notegw.jar com.suntek.web.notegw.NotegwSpringbootApplication $APP_ARGS >>$logs_dir/app.log 2>&1 & echo $! >$script_dir/pidfile.txt

停止项目脚本

#!/bin/sh
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
cd $script_dir
PID=`cat pidfile.txt`
kill $PID
while true
do
  ls /proc/$PID/cmdline >/dev/null 2>&1
  if [ $? == 0 ]
  then
    sleep 1    
  else
   break
  fi
  echo "closing......"
done
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个非常流行的Java框架,它提供了一种简单、快速的方式来构建和部署Web应用程序。下面是一个简单Spring Boot项目部署过程。 1. 编写Spring Boot应用程序 首先,你需要编写一个Spring Boot应用程序。这可能需要一些时间和经验,但这里只是简单地提到一些要点: - 确保你的应用程序具有清晰的结构,易于维护和扩展。 - 使用Spring Boot的自动配置功能,尽量减少手动配置。 - 使用Spring Boot的嵌入式Web服务器,如Tomcat或Jetty。 2. 打包应用程序 一旦你编写了Spring Boot应用程序,你需要打包它,以便可以在服务器上运行。你可以使用Maven或Gradle等构建工具来完成此操作。 对于Maven,你只需运行以下命令: ``` mvn clean package ``` 这将生成一个可执行的JAR文件。 3. 部署应用程序 现在你已经有了一个可执行的JAR文件,接下来就是将它部署到服务器上。 你可以选择使用任何云平台(如AWS、Azure、Google Cloud等)或自己的服务器。不过,在这里,我们将专注于如何使用Docker容器来部署应用程序。 以下是一些基本步骤: - 安装Docker并启动Docker服务。 - 创建一个Dockerfile,其中包含有关如何构建Docker镜像的信息。例如: ``` FROM openjdk:8-jdk-alpine VOLUME /tmp ADD target/myapp.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ``` - 在Dockerfile所在的目录中运行以下命令,以构建Docker镜像: ``` docker build -t myapp . ``` - 运行以下命令,以在Docker容器中启动应用程序: ``` docker run -p 8080:8080 myapp ``` 现在你的Spring Boot应用程序已经在Docker容器中运行,并且可以通过浏览器访问它了。 以上就是一个简单Spring Boot项目部署过程。当然,实际情况可能更加复杂,但是如果你遵循这些基本步骤,应该可以轻松地将你的应用程序部署到服务器上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值