一、环境准备
1.cenos7服务器一台
2.更换yum源
yum install -y wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
rm -rf /etc/yum.repos.d/epel*
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
3.安装Java
##安装java
mkdir /usr/local/java
cd /usr/local/java
wget http://zjxianlin.com:8082/files/jdk-8u141-linux-x64.tar.gz
tar zxvf jdk-8u141-linux-x64.tar.gz
#编辑配置文件
vi /etc/profile
#填入以下内容
export JAVA_HOME=/usr/local/java/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#刷新环境变量
source /etc/profile
#验证java是否安装成功
java -version
4.安装docker
##安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
##启动并加入开机启动
systemctl start docker
systemctl enable docker
##验证安装是否成功
docker version
5.安装docker-compose
##安装docker-compose
wget -O /usr/local/bin/docker-compose http://zjxianlin.com:8082/files/docker-compose-Linux-x86_64
chmod +x /usr/local/bin/docker-compose
6.其它安装
##安装文件编码转换
yum install -y dos2unix
7.按需安装mysql,redis,mq等开发组件
8.环境搭建完毕,如果是云服务器,请确认服务端口是否在安全组内开放。如果是虚拟机,请确认防火墙是否关闭。否则,无法远程连接。
二、部署规范
1.所有jar包,tar包服务部署在/opt目录下,以目标客户对象分组。如:仙林版本管理服务,仙林文件管理服务部署如下:
/opt/shining/shining-version-starter/shining-version-starter.jar (jar包)
/opt/shining/shining-file-starter/ (tar包)
面向千琪无印系列的的项目部署目录如下:
/opt/wy/wylp/ (tar包)
面向盈狐云系列的项目部署目录如下:
/opt/yh/yh-customer/yh-customer.jar (jar包)
2.所有docker-compose文件放在/usr/local/docker-run/enable/目录下
3.项目目录规范
模块根目录
├─bin 启停脚本文件
├─deploy 部署文件
│ ├─docker-compose
│ └─sh
├─install 打包文件
├─src 源码目录
└─target 打包结果目录
三、服务打包
Java服务打包成可运行程序的形式有很多种,且都依赖于maven或者gradle:
这里介绍maven的三种:
1.springboot jar包
2.依赖配置分离的tar包
3.docker形式
ps:在以下脚本中 SERVICE_NAME为项目启动类所在模块的artifactId
(一).springboot-jar包
1.在服务Application启动类所在模块的根路径建立以下文件:
bin/jar-service.sh (服务启停文件)
deploy/sh/deploy-jar.sh (服务部署脚本)
install/assembly-jar.xml (项目打包配置)
2.jar-service.sh(模板)
#!/bin/sh
# --------------------声明 start--------------------------------------
# jar包部署后专用启动、停止、重启脚本
# 使用方式 sh jar-service.sh start, sh jar-service.sh stop ,sh jar-service.sh restart
# 该shell脚本文件在项目的的存放位置-${project.basedir}/bin
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 在该文件目录下的../install/assembly-jar.xml会扫描该文件并将该文件加入部署包
# --------------------声明 end----------------------------------------
#
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 服务部署目录
DEPLOY_PATH="/opt/wy"
# gc日志文件目录
# 建议项目中logback.xml配置的目录相同
GCLogHome="/var/logs/wy/wy-start-service"
#----------------------- 不同项目需要修改的部分 end---------------------
#
##
echo "服务名称:$SERVICE_NAME"
echo "文件所在目录:$DEPLOY_PATH"
echo "GC日志目录:$GCLogHome"
GCCommand="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+PrintReferenceGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1 -Xloggc:$GCLogHome/gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=14 -XX:GCLogFileSize=100M"
# shellcheck disable=SC1009
# shellcheck disable=SC2112
function startService() {
# shellcheck disable=SC2061
FIND_JAR_CMD="find $DEPLOY_PATH/$SERVICE_NAME/ -maxdepth 1 -name '$SERVICE_NAME*.jar'|grep -v $SERVICE_NAME*sources.jar "
echo "判断$DEPLOY_PATH/$SERVICE_NAME/路径下是否存在执行jar包"
echo "$FIND_JAR_CMD"
service_jar_path=$(eval $FIND_JAR_CMD)
# shellcheck disable=SC2236
if [ ! $service_jar_path ] ;then
echo "$DEPLOY_PATH/$SERVICE_NAME/下没有找到执行jar包"
else
echo "找到jar包,路径为:$service_jar_path"
if [ ! -n "${MY_JAVA_OPTS}" ]; then
START_CMD="nohup java -jar $GCCommand $service_jar_path >/dev/null 2>&1 &"
echo "没有找到JVM参数,无参启动:$START_CMD"
eval $START_CMD
else
START_CMD="nohup java ${MY_JAVA_OPTS} -jar $GCCommand $service_jar_path >/dev/null 2>&1 &"
echo "jvm参数:${MY_JAVA_OPTS},开始启动:$START_CMD"
eval $START_CMD
fi
fi
# shellcheck disable=SC2152
return 1
}
# shellcheck disable=SC2112
function stopService() {
# shellcheck disable=SC2009
service_process=$(ps -ef | grep "$SERVICE_NAME" | grep -v grep | grep .jar | grep java | awk '{print $2}')
# shellcheck disable=SC2236
if [ ! -n "$service_process" ]; then
echo '没有找到进程'
else
echo "进程号为:$service_process"
echo "开始kill..."
kill -9 "$service_process"
fi
return 1
}
# jar包部署专用启动、停止、重启脚本
echo "服务名称:$SERVICE_NAME"
command_line=$1
# shellcheck disable=SC2236
if [ ! -n "$command_line" ]; then
echo "请输入要执行的动作:start stop restart"
elif [ "$command_line" = "start" ]; then
echo "开始启动服务$SERVICE_NAME"
startService
elif [ "$command_line" = "stop" ]; then
echo "开始停止服务$SERVICE_NAME"
stopService
elif [ "$command_line" = "restart" ]; then
echo "开始重启服务$SERVICE_NAME"
stopService
startService
fi
exit 0
3.deploy-jar.sh(模板)
#!/bin/sh
# --------------------声明 start--------------------------------------
# jar包形式部署时专用
# 使用方式 sh deploy-jar.sh
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 该shell脚本文件在项目的的存放位置-${project.basedir}/deploy/sh
# 在该位置下的../../install/assembly-jar.xml会扫描该文件并将该文件加入部署包
# --------------------声明 end----------------------------------------
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 部署目录
# jar包形式默认部署在/opt目录且已目标客户或者业务线区分如/wy,/shining
DEPLOY_PATH="/opt/wy"
#----------------------- 不同项目需要修改的部分 end--------------------
#
##
echo "要部署的服务名称:$SERVICE_NAME"
echo "要部署的目标目录:$DEPLOY_PATH"
currentPath=$(cd ./; pwd)
echo "当前目录:$currentPath"
echo "部署开始..."
serviceBasePath="$DEPLOY_PATH/$SERVICE_NAME"
if [ ! -d "$serviceBasePath" ]; then
mkdir -p $serviceBasePath
fi
echo "固定部署目录为$serviceBasePath"
# shellcheck disable=SC1009
if [[$currentPath -ef $serviceBasePath ]] ;then
echo "当前目录就是部署目录,直接启动"
else
CLEAR_CMD="rm -rf $serviceBasePath/*"
CP_CMD="cp -rf $currentPath/* $serviceBasePath/"
echo "当前目录不是部署目录,开始迁移:"
echo $CLEAR_CMD
echo $CP_CMD
eval $CLEAR_CMD
eval $CP_CMD
fi
DOS2UNIX="dos2unix $DEPLOY_PATH/$SERVICE_NAME/*.sh"
echo "服务部署完毕,开始脚本编码转换:$DOS2UNIX"
eval $DOS2UNIX
START_CMD="nohup sh $DEPLOY_PATH/$SERVICE_NAME/jar-service.sh restart >/dev/null 2>&1 &"
echo "开始启动:$START_CMD"
eval $START_CMD
exit 0
4.assembly-jar.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
<id>jar-deploy</id><!--id 标识符,添加到生成文件名称的后缀符。可以不指定-->
<formats>
<!--打包方式,支持写多个包括(zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war)-->
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--获取启动jar包-->
<fileSet>
<directory>${project.basedir}/target</directory>
<includes>
<include>${project.artifactId}-${project.version}.jar</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
<!--获取启动脚本-->
<fileSet>
<directory>${project.basedir}/bin</directory>
<includes>
<include>jar-service.sh</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
<!--获取部署脚本-->
<fileSet>
<directory>${project.basedir}/deploy/sh</directory>
<includes>
<include>deploy-jar.sh</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
</fileSets>
</assembly>
6.修改Application类所在模块的pom.xml
<properties>
<mainClass>你的Application启动类的全限定名</mainClass>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-dir</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptors>
<descriptor>install/assembly-jar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
7.开始打包
然后运行mvn clean package
之后 在服务Application启动类所在模块的target目录下就可以找到相应的zip包
上传至服务器unzip ***.zip
然后进入解压目录
dos2unix *.sh
sh deploy-jar.sh
项目则会开始自动部署启动
其它命名:
sh jar-service.sh start
sh jar-service.sh stop
sh jar-service.sh restart
(二).依赖配置分离的tar包
1.在服务Application启动类所在模块的根路径建立以下文件:
bin/tar-service.sh (服务启停文件)
deploy/sh/deploy-tar.sh (服务部署脚本)
install/assembly-tar.xml (项目打包配置)
2.tar-service.sh(模板)
#!/bin/sh
# --------------------声明 start--------------------------------------
# tar包部署后专用启动、停止、重启脚本
# 使用方式 sh tar-service.sh start, sh tar-service.sh stop ,sh tar-service.sh restart
# 该shell脚本文件在项目的的存放位置-${project.basedir}/bin
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 在该文件目录下的../install/assembly-tar.xml会扫描该文件并将该文件加入部署包
# --------------------声明 end----------------------------------------
#
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 服务部署目录
DEPLOY_PATH="/opt/wy"
# gc日志文件目录
# 建议项目中logback.xml配置的目录相同
GCLogHome="/var/logs/wy/wy-start-service"
#----------------------- 不同项目需要修改的部分 end---------------------
#
##
echo "服务名称:$SERVICE_NAME"
echo "文件所在目录:$DEPLOY_PATH"
echo "GC日志目录:$GCLogHome"
GCCommand="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+PrintReferenceGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1 -Xloggc:$GCLogHome/gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=14 -XX:GCLogFileSize=100M"
# shellcheck disable=SC1009
# shellcheck disable=SC2112
function startService() {
# shellcheck disable=SC2061
FIND_JAR_CMD="find $DEPLOY_PATH/$SERVICE_NAME/ -name '$SERVICE_NAME*.jar'|grep -v $SERVICE_NAME*sources.jar "
echo "判断$DEPLOY_PATH/$SERVICE_NAME/路径下是否存在执行jar包"
echo "$FIND_JAR_CMD"
service_jar_path=$(eval $FIND_JAR_CMD)
# shellcheck disable=SC2236
if [ ! $service_jar_path ]; then
echo "$DEPLOY_PATH/$SERVICE_NAME/下没有找到执行jar包"
else
echo "找到jar包,路径为:$service_jar_path"
if [ ! -n "${MY_JAVA_OPTS}" ]; then
START_CMD="java -jar $GCCommand $service_jar_path"
echo "没有找到JVM参数,无参启动:$START_CMD"
eval $START_CMD
else
START_CMD="java ${MY_JAVA_OPTS} -jar $GCCommand $service_jar_path"
echo "jvm参数:${MY_JAVA_OPTS},开始启动:$START_CMD"
eval $START_CMD
fi
fi
return 1
}
# shellcheck disable=SC2112
function stopService() {
# shellcheck disable=SC2009
service_process=$(ps -ef | grep $SERVICE_NAME | grep -v grep | grep .jar | grep java | awk '{print $2}')
# shellcheck disable=SC2236
if [ ! -n "$service_process" ]; then
echo '没有找到进程'
else
echo "进程号为:$service_process"
kill -9 "$service_process"
fi
return 1
}
command_line=$1
# shellcheck disable=SC2236
if [ ! -n "$command_line" ]; then
echo "请输入要执行的动作:start stop restart"
elif [ "$command_line" = "start" ]; then
echo "开始启动服务$SERVICE_NAME"
startService
elif [ "$command_line" = "stop" ]; then
echo "开始停止服务$SERVICE_NAME"
stopService
elif [ "$command_line" = "restart" ]; then
echo "开始重启服务$SERVICE_NAME"
stopService
startService
fi
exit 0
3.deploy-tar.sh(模板)
#!/bin/sh
# --------------------声明 start--------------------------------------
# tar包形式部署时专用
# 使用方式 sh deploy-tar.sh
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 该shell脚本文件在项目的的存放位置-${project.basedir}/deploy/sh
# 在该位置下的../../install/assembly-tar.xml会扫描该文件并将该文件加入部署包
# --------------------声明 end----------------------------------------
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 部署目录
# tar包形式默认部署在/opt目录且已目标客户或者业务线区分如/wy,/shining
DEPLOY_PATH="/opt/wy"
#----------------------- 不同项目需要修改的部分 end--------------------
#
##
echo "服务名称:$SERVICE_NAME"
serviceBasePath="$DEPLOY_PATH/$SERVICE_NAME"
currentPath=$(pwd)
# shellcheck disable=SC1009
if [ "$currentPath" = "$serviceBasePath" ] ;then
echo "当前目录就是部署目录,直接启动"
else
if [ ! -d "$DEPLOY_PATH/" ] ; then
mkdir -p "$DEPLOY_PATH/"
fi
if [ ! -d "$serviceBasePath" ] ; then
echo "目录不存在,开始新建:$serviceBasePath"
mkdir -p "$serviceBasePath"
fi
CLEAR_CMD="rm -rf $serviceBasePath/*"
CP_CMD="cp -rf $currentPath/* $serviceBasePath/"
echo "当前目录不是部署目录,开始迁移:"
echo $CLEAR_CMD
echo $CP_CMD
eval $CLEAR_CMD
eval $CP_CMD
fi
DOS2UNIX="dos2unix $DEPLOY_PATH/$SERVICE_NAME/bin/*.sh"
echo "服务部署完毕,开始脚本编码转换:$DOS2UNIX"
eval $DOS2UNIX
START_CMD="nohup sh $DEPLOY_PATH/$SERVICE_NAME/bin/tar-service.sh restart >/dev/null 2>&1 &"
echo "开始启动:$START_CMD"
eval $START_CMD
exit 0
4.assembly-tar.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
<id>${project.version}</id><!--id 标识符,添加到生成文件名称的后缀符。可以不指定-->
<formats>
<!--打包方式,支持写多个包括(zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war)-->
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--将所有resources文件下的文件打包在conf目录下-->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<!--将启动脚本打包在bin目录下-->
<fileSet>
<directory>${project.basedir}/bin</directory>
<includes>
<include>*.sh</include>
</includes>
<outputDirectory>bin</outputDirectory>
</fileSet>
<!--你还可以将初始化sql文件等打包在init目录下-->
<fileSet>
<directory>${project.basedir}/deploy/sh</directory>
<includes>
<include>deploy-tar.sh</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
5.修改pom
<properties>
<mainClass>你的Application启动类的全限定名</mainClass>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>../conf/</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>*</exclude>
<exclude>*/*</exclude>
<exclude>*/static</exclude>
</excludes>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-dir</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptors>
<descriptor>install/assembly-tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
6.开始打包
运行mvn clean package
之后在Application所在模块的target目录下可以找到相关的tar.gz文件
上传到服务器后 tar zxvf ***.tar.gz
进入解压目录:
dos2unix *.sh
dos2unix bin/*.sh
sh deploy-tar.sh
项目则开始自动化部署运行
其它命令:
sh tar-service.sh start
sh tar-service.sh stop
sh tar-service.sh restart
ps:依赖配置分离的好处在于:
1、可以在外部修改配置文件或者替换单个的依赖jar包。
2、在docker部署时可以将配置文件进行外部映射。
3、在其它模块引用时,可以避免jar包体积过大。
(三).DockerFile部署
1.说明
DockerFile部署其实就是在打包的基础上,进行一次docker容器的封装.要求本地具备docker环境,如果本地不具备。则需要一些特殊手段。
1.ubuntu,mac以及部分windows10可以本地安装docker。
如果本地没有安装docker,则需要远程登录其它已安装的docker主机,远程操作。例如:借用本地虚拟机或者云服务器。
远程的docker主机如何开放远程端口可以见教程:https://blog.csdn.net/qq_33517844/article/details/97639230。
ps:docker-2375端口存在未授权漏洞,建议改用其它10000以上的端口,不易被发现。或者采用信任文件的形式进行校验。(虚拟机忽略该条)
操作完之后,本地主机设置环境变量:DOCKER_HOST=tcp://[远程docker服务器ip]:[远程docker开发的端口]
2.在你使用的Docker的那台服务器上登录docker镜像仓库。
首先:
停止docker
然后:
cenos系统:
vi /etc/docker/daemon.json
写入以下内容:
{
"registry-mirrors": ["https://unwchefq.mirror.aliyuncs.com"]
}
systemctl daemon-reload
service docker restart
ubuntu系统:
vi /lib/systemd/system/docker.service
找到“ExecStart”这一行末尾添加:
--insecure-registry zjxianlin.com:41532
sudo systemctl daemon-reload
sudo service docker restart
windows10:
找到:C:/Users/[当前用户]/.docker/config.json
没有则新建一个
写入以下内容:
{
"auths": {
"registry.cn-hangzhou.aliyuncs.com": {}
},
"credsStore": "wincred"
}
重启电脑和docker
最后:
docker login **镜像仓库
输入用户名: admin
密码:***
2.在服务Application启动类所在模块的根路径建立以下文件:
bin/docker-service.sh (服务启停文件)
deploy/sh/deploy-docker.sh (服务部署脚本)
install/assembly-docker.xml (部署文件打包配置)
install/assembly-tar.xml (项目打包配置)
Dockerfile (容器打包文件)
deploy/docker-compose/docker-compose.yml (docker容器配置文件)
3.docker-service.sh
#!/bin/sh
# --------------------声明 start--------------------------------------
# docker部署后专用启动脚本
# 使用方式 docker容器启动时内部调用,服务停启请通过docker命令
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 该shell脚本文件在项目的的存放位置-${project.basedir}/bin
# 在该文件目录下的../install/assembly-docker.xml会扫描该文件并将在docker镜像构建时加入镜像服务
# --------------------声明 end----------------------------------------
#
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 服务部署目录
DEPLOY_PATH="/opt/wy"
# gc日志文件目录
# 建议项目中logback.xml配置的目录相同
GCLogHome="/var/logs/wy/wy-start-service"
#----------------------- 不同项目需要修改的部分 end---------------------
#
##
echo "服务名称:$SERVICE_NAME"
echo "文件所在目录:$DEPLOY_PATH"
echo "GC日志目录:$GCLogHome"
GCCommand="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+PrintReferenceGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1 -Xloggc:$GCLogHome/gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=14 -XX:GCLogFileSize=100M"
# shellcheck disable=SC2061
FIND_JAR_CMD="find $DEPLOY_PATH/$SERVICE_NAME/ -name '$SERVICE_NAME*.jar'|grep -v $SERVICE_NAME*sources.jar "
echo "查找jar包所在目录:$FIND_JAR_CMD"
service_jar_path=$(eval $FIND_JAR_CMD)
echo "jar包目录:$service_jar_path"
# shellcheck disable=SC2236
if [ ! -n "$service_jar_path" ] ;then
echo '没有找到目标路径'
else
echo "目标路径为:$service_jar_path"
if [ ! -n "${MY_JAVA_OPTS}" ] ;then
echo "没有找到JVM参数"
java -jar "$service_jar_path"
else
echo "jvm参数:${MY_JAVA_OPTS}"
java "${MY_JAVA_OPTS}" -jar "$service_jar_path"
fi
fi
4.deploy-docker.sh(模板)
#!/bin/sh
# --------------------声明 start--------------------------------------
# docker形式部署时专用
# 使用方式 sh deploy-docker.sh
# ${project.basedir}为springboot项目Application启动类所在模块的根目录
# 该shell脚本文件在项目的的存放位置-${project.basedir}/deploy/sh
# 在该位置下的../../install/assembly-docker.xml会扫描该文件并将该文件加入部署包
# --------------------声明 end----------------------------------------
#
#----------------------- 不同项目需要修改的部分 start--------------------
# 服务名称
# springboot项目请修改为Application启动类所在模块的artifactId
SERVICE_NAME="wy-start-service"
# 服务版本
# springboot项目请修改为Application启动类所在模块的version
SERVICE_VERSION="1.0-SNAPSHOT"
#----------------------- 不同项目需要修改的部分 end--------------------
#
# docker-compose启动文件的根目录
# 默认所有docker文件统一在/usr/local/docker-run/enable下以服务名作子文件夹名称区分
# 例如:/usr/local/docker-run/enable/mysql /usr/local/docker-run/enable/redis
#
##
DOCKER_COMPOSE_ROOT="/usr/local/docker-run/enable";
echo "服务名称:$SERVICE_NAME"
dockerComposeFilePath="docker-compose.yml"
dockerConfigPath=$(cd ./conf;pwd)
defaultDirName="$DOCKER_COMPOSE_ROOT/$SERVICE_NAME";
defaultDockerConfigPath="$DOCKER_COMPOSE_ROOT/$SERVICE_NAME/conf"
defaultDockerComposeFilePath="$defaultDirName/docker-compose.yml"
echo "开始部署docker-compose..."
echo "判断当前路径是否存在docker-compose.yml,如果存在,则强制复制到目标路径$defaultDirName"
# shellcheck disable=SC2236
if [ ! -f "$dockerComposeFilePath" ] ;then
echo "当前目录未找到docker-compose.yml,使用默认文件$defaultDockerComposeFilePath"
if [ ! -f "$defaultDockerComposeFilePath" ] ;then
echo "默认文件未找到,程序退出,部署失败"
exit
fi
else
echo "文件存在,开始复制"
if [ ! -d "$defaultDirName" ] ;then
mkdir -p "$defaultDirName"
fi
cp -f "$dockerComposeFilePath" "$defaultDirName"
fi
echo "docker-compose部署完毕"
echo "开始部署服务相关配置文件..."
echo "判断当前路径是否存在conf目录,如果存在,则强制复制到目标路径$defaultDirName"
# shellcheck disable=SC2236
if [ ! -d "$dockerConfigPath" ] ;then
echo "当前目录未找到conf目录,使用默认目录$defaultDockerConfigPath"
if [ ! -d "$defaultDockerConfigPath" ] ;then
echo "默认目录未找到,程序退出,部署失败"
exit
fi
else
if [ ! -d "$defaultDirName" ] ;then
mkdir -p "$defaultDirName"
fi
echo "目录存在,开始复制"
cp -rf "$dockerConfigPath" "$defaultDirName"
fi
echo "config目录部署完毕"
echo "开始启动服务"
docker pull zjxianlin.com:41532/library/"$SERVICE_NAME":1.0-SNAPSHOT
cd "$defaultDirName"&&docker-compose up -d
5. assembly-tar.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
<id>${project.version}</id><!--id 标识符,添加到生成文件名称的后缀符。可以不指定-->
<formats>
<!--打包方式,支持写多个包括(zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war)-->
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--将所有resources文件下的文件打包在conf目录下-->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<!--将启动脚本打包在bin目录下-->
<fileSet>
<directory>${project.basedir}/bin</directory>
<includes>
<include>*.sh</include>
</includes>
<outputDirectory>bin</outputDirectory>
</fileSet>
<!--你还可以将初始化sql文件等打包在init目录下-->
<fileSet>
<directory>${project.basedir}/deploy/sh</directory>
<includes>
<include>deploy-tar.sh</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
6. assembly-docker.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
<id>docker-deploy</id><!--id 标识符,添加到生成文件名称的后缀符。可以不指定-->
<formats>
<!--打包方式,支持写多个包括(zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war)-->
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--将所有resources文件下的文件打包在conf目录下-->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<!--将启动脚本打包在bin目录下-->
<fileSet>
<directory>${project.basedir}/deploy/docker-compose</directory>
<outputDirectory>./</outputDirectory>
</fileSet>
<!--将启动脚本打包在bin目录下-->
<fileSet>
<directory>${project.basedir}/deploy/sh</directory>
<includes>
<include>deploy-docker.sh</include>
</includes>
<outputDirectory>./</outputDirectory>
</fileSet>
</fileSets>
</assembly>
7.Dockerfile 注意替换 /opt/wy部署目录
# This Dockerfile
# Version 1.0-SNAPSHOT
# Base images 基础镜像
FROM openjdk:8-jdk-alpine
#MAINTAINER 维护者信息
MAINTAINER fengkuangdejava
EXPOSE 8080
ARG SERVICE_NAME
ARG VERSION_NAME
ENV SERVICE_NAME ${SERVICE_NAME}
ENV VERSION_NAME ${VERSION_NAME}
ENV MY_JAVA_OPTS -Xmx512m
ENV ACTIVEPROFILE dev
RUN mkdir -p /opt/wy/
ADD ./target/${SERVICE_NAME}-${VERSION_NAME}.tar.gz /opt/wy/
RUN rm -f /opt/wy/${SERVICE_NAME}-${VERSION_NAME}.tar.gz
RUN cp -r /opt/wy/${SERVICE_NAME}/bin /opt/wy/
RUN cd /opt/wy/${SERVICE_NAME}&&ls
CMD ["sh","/opt/wy/bin/docker-service.sh"]
ps:SERVICE_NAME,VERSION_NAME两个变量maven打包时会pom传入,如果是手动构建,则需要在构建命令 docker build时自己传入build args.
8.docker-compose.yml 注意替换版本号为模块版本
version: '3'
services:
wuyin:
image: 镜像仓库地址/wy-start-service:1.0-SNAPSHOT
restart: always
container_name: wuyin
volumes:
- ./conf:/opt/wy/wy-start-service/conf
- ./log/:/var/logs/wy/wy-start-service
ports:
- 8084:8080
environment:
- MY_JAVA_OPTS=-Xmx1024m
- ACTIVEPROFILE=dev
- serverPort=8080
ps:
MY_JAVA_OPTS:docker-service.sh用,用于java启动传参
ACTIVEPROFILE:因为为在application.yml的写法为:
spring:
profiles:
active: ${ACTIVEPROFILE:dev}
用于指定容器内部使用哪套application-**.yml配置。
serverPort:因为为在application.yml的写法为:
server:
port: ${serverPort:8082}
用于指定容器内部服务启动时的端口,Docker部署时固定8080,以便和dockerFile对应
9.Application启动类所在模块的pom.xml配置
<properties>
<mainClass>你的Application启动类的全限定名</mainClass>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>../conf/</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>*</exclude>
<exclude>*/*</exclude>
<exclude>*/static</exclude>
</excludes>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptors>
<descriptor>install/assembly-tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>make-deploy</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptors>
<descriptor>install/assembly-docker.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<!--如果package时不想用docker打包,就注释掉这个goal-->
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 上下文路径配置,此处设置为项目根路径 用来读取Dockerfile-->
<contextDirectory>${project.basedir}</contextDirectory>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<!--上传路径/镜像构建名: 私服地址/私服项目名/springboot项目名-->
<repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}
</repository>
<!-- 标记 -->
<tag>${project.version}</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<buildArgs>
<SERVICE_NAME>${project.artifactId}</SERVICE_NAME>
<VERSION_NAME>${project.version}</VERSION_NAME>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
10.开始打包
mvn clean package -DskipTests=true (仅构docker构建镜像不上传镜像到镜像仓库)
mvn clean package deploy -DskipTests=true(构建镜像并上传到镜像仓库)
然后在target目录找到相关tar包
上传至服务器任意目录 tar zxvf ***.tar.gz
然后进入目录:
dos2unix *.sh
sh deploy-docker.sh
然后会正式开始部署docker-compose文件,并启动容器
总结
这里讲了3种部署方式,其实三种都可以配置在项目当中。通过maven的Profile来区分。
如下图:
mvn clean package install -Ptar 来指定使用什么打包方式
-Pjar
-Pdocker