Docker容器之SpringBoot多环境部署

13 篇文章 0 订阅
2 篇文章 0 订阅

一.  项目结构:

说明: 其中1为启动脚本,2为maven构建配置 3 为将项目构建成公司统一管理风格 4 为pom依赖

app_control.bash:

#!/bin/bash
set -o pipefail
set -u
EXTERNAL_LOADER_PATH="../conf"
SERVER_PORT=8080
SHUTDOWN_HOST=127.0.0.1
EXEC_STD_OUT=/dev/null
LOG_ROOT_PATH="./logs"
LOG_FILE_NAME="em.log"
#source env.bash
#evironment set
WEB_BIN=`pwd`
CONF_DIR="${WEB_BIN}/../conf"
CONFIG_LOCATION=/data1/www/rz_entrance/config/java_config/rz_em_etl/application.properties
JAVA_OPTS=" -server -Xmx1g -Xms1g  -Xmn256m -XX:MetaspaceSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70"
JAVA_OPTS="${JAVA_OPTS} -Dserver.port=${SERVER_PORT}"
#JAVA_OPTS="${JAVA_OPTS} -Dloader.path=${WEB_BIN}/../conf,${EXTERNAL_LOADER_PATH}"
#JAVA_OPTS="${JAVA_OPTS} -Dlogging.path=${LOG_ROOT_PATH} -Dendpoints.logfile.external-file=${LOG_ROOT_PATH}/${LOG_FILE_NAME}"
START_UP_EXEC="java -Dfile.encoding=utf-8 -Dspring.config.location=${CONFIG_LOCATION} ${JAVA_OPTS} -jar $WEB_BIN/../lib/rz-em-etl.jar"
#Time Config
START_WAIT=5
SHUT_WAIT=10
KILL_INTERVAL=5
KILL_TIMES=5
#RETURN CODES
RET_SUCCESS=0
RET_INSTANCE_DEAD=1
RET_ERROR_SHUT=2
RET_ERROR_START=3
RET_STATUS_ALIVE=0
RET_STATUS_NOT_ALIVE=1
INSTANCE_PID=rz-em-etl.pid

function get_Pid(){
    if [ ! -z ${INSTANCE_PID} ];then
        if [ -f ${INSTANCE_PID} ];then
            cat "${INSTANCE_PID}"
        fi
    else
        ps -ef | grep -vE "grep $WEB_BIN|$0" | grep ${WEB_BIN} | awk '{print $2}'
    fi
}
#get pid
PID=`get_Pid`
function usage(){
        cat <<EOM
    Purpose  :    This script encapsulates the spring boot jar and just acts like a controller.
    Usage    :    bash ${0} start|shutdown|kill|force|restart|status
    Date     :    2017.03
EOM
}
#test instance alived or not
function is_Instance_Alive(){
    #kill -0 : test process alived or not
    if `kill -0 ${PID} 2>/dev/null` ; then
    #0 stands for success in shell
        return ${RET_SUCCESS}
    else
        return ${RET_INSTANCE_DEAD}
    fi
}
## remove pid file
function remove_Pid(){
   if [ ! -z ${INSTANCE_PID} ];then
       if [ -f ${INSTANCE_PID} ];then
           rm -f ${INSTANCE_PID}
       fi
   fi
}
## shutdown Instance
function shutdown_Instance(){
    if ! is_Instance_Alive ;then
        echo "no need to stop, not found PID"
        return ${RET_SUCCESS}
    else
        echo -n "shutdown instance"
        for((i=0;i<=$KILL_TIMES;i++ ));do   #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
            curl -d "" "http://${SHUTDOWN_HOST}:${SERVER_PORT}/shutdown"
            sleep_Wait ${KILL_INTERVAL}
            #check if kill of this round success
            if is_Instance_Alive ; then
                kill_Instance
                return ${RET_SUCCESS}
            fi
            if ! is_Instance_Alive ; then
                remove_Pid
                echo shutdown successfully
                return ${RET_SUCCESS}
            fi
        done
        return ${RET_ERROR_SHUT}
    fi
}
#normal kill
function kill_Instance(){
    if ! is_Instance_Alive ;then
        echo "no need to stop, not found PID"
        return ${RET_SUCCESS}
    else
        echo -n "killing $PID "
    for((i=0;i<=$KILL_TIMES;i++ ));do    #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
        kill -15 ${PID}
        sleep_Wait ${KILL_INTERVAL}
        #check if kill of this round success
        if ! is_Instance_Alive ; then
            remove_Pid
            echo kill successfully
            return ${RET_SUCCESS}
        fi
    done
    return ${RET_ERROR_SHUT}
    fi
}
#-9 kill
function force_Instance(){
    if ! is_Instance_Alive ;then
        echo "no need to force kill, not found PID"
        return ${RET_SUCCESS}
    else
    echo -n "force killing $PID "
    for((i=0;i<=$KILL_TIMES;i++ ));do
        #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
        kill -9 ${PID}
        sleep_Wait ${KILL_INTERVAL}
        if ! is_Instance_Alive ; then
            remove_Pid
            echo force kill successfully    #check if force kill of this round success
            return ${RET_SUCCESS}
        fi
    done
    echo shutdown failed, please manually check
    return ${RET_ERROR_SHUT}
    fi
}
#wait
function sleep_Wait(){
    local sec=$1
    for((i=1;i<=$sec;i++ ));do
        sleep 1
        echo -n "."
    done
    echo
}
#start up
function startup_Instance(){
##This function startup the instance
    if is_Instance_Alive; then
        echo no need to start, instance pid : ${PID}, exit
    else
        echo exec ${START_UP_EXEC}
        echo -n "starting "
        nohup ${START_UP_EXEC} > ${EXEC_STD_OUT} 2>&1 &
        if [ ! -z ${INSTANCE_PID} ];then
            echo $! > "${INSTANCE_PID}"
        fi
        sleep_Wait ${START_WAIT}
        if ! is_Instance_Alive ; then
            echo start successfully : `get_Pid`
            return ${RET_SUCCESS}
        else
            echo start failed
            return ${RET_ERROR_START}
        fi
    fi
}
#start up
function startup_Instance_Without_Nohup(){
##This function startup the instance
    if is_Instance_Alive; then
        echo no need to start, instance pid : ${PID}, exit
    else
        echo exec ${START_UP_EXEC}
        echo -n "starting "
        ${START_UP_EXEC}
        if [ ! -z ${INSTANCE_PID} ];then
            echo $! > "${INSTANCE_PID}"
        fi
        sleep_Wait ${START_WAIT}
        if ! is_Instance_Alive ; then
            echo start successfully : `get_Pid`
            return ${RET_SUCCESS}
        else
            echo start failed
            return ${RET_ERROR_START}
        fi
    fi
}
#check instance status
function status_Instance(){
    if is_Instance_Alive;then
        echo "Instance is Alived, pid:${PID}"
        return ${RET_STATUS_ALIVE}
    else
        echo "Instance is not Alived"
        return ${RET_STATUS_NOT_ALIVE}
    fi
}
#dispatcher
function dispatcher(){
    if [ $# -lt 1 ] ;then
        usage
        exit -1
    fi
#    if [ -z ${PROFILES} ];then
#        echo "Please specific profiles before excute this script, example: export PROFILES=dev"
#        exit -1
#    fi
    local args=$1
    case "$args" in
    kill)
        kill_Instance
        ;;
    force)
        force_Instance
        ;;
    start)
        startup_Instance
        ;;
    fstart)
        startup_Instance_Without_Nohup
        ;;
    shutdown)
        shutdown_Instance
        ;;
    restart)
        shutdown_Instance && startup_Instance
        ;;
    status)
        status_Instance
        ;;
    *)
        usage
        ;;
    esac
}
dispatcher "$@"
#END

assembly.xml:

<assembly>
	<id>release</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
    	<fileSet>
    		<directory>${project.build.directory}</directory>
    		<outputDirectory>lib</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0755</fileMode>
    		<includes>
    			<include>*.jar</include>
    		</includes>
    	</fileSet>
        <fileSet>
            <directory>src/main/resources/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    
</assembly>

build.sh:

#!/bin/bash
HOME_DIR=`cd $(dirname $0);pwd`
echo $HOME_DIR
BUILD_DIR=$HOME_DIR/output_scm

# rz-em-etl.tar.gz
RZ_EM_ETL_FILE_NAME=rz-em-etl.tar.gz
RZ_EM_ETL=$HOME_DIR/target/$RZ_EM_ETL_FILE_NAME

if [[ -z $2 ]]; then
    MODULE=all
else
    MODULE=$2
fi
echo "Begin building..."
# re-create BUILD_DIR
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
# building
mvn clean package -DskipTests -P ${1}
# checking mvn buile status
if [ $? -ne 0 ]
then
	echo "Project build faild. Mvn build faild."
	exit 1
fi
if [[ ${MODULE} == 'all' ]] || [[ ${MODULE} == 'rz-em-etl' ]]; then
    if [ -f $RZ_EM_ETL ];then
        cp -f $RZ_EM_ETL .
#        tar czf rz-em-etl.tar.gz rz-em-etl.jar
	    cp -f rz-em-etl.tar.gz $BUILD_DIR
	    md5sum rz-em-etl.tar.gz|cut -f 1 -d " " > $BUILD_DIR/rz-em-etl.tar.gz.md5
    else
	    echo "No $RZ_EM_ETL found."
	    exit 1
    fi
fi

echo "Project has built..."

pom.xml(部分):

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>ga</id>
            <properties>
                <env>ga</env>
            </properties>
        </profile>
        <profile>
            <id>docker</id>
            <properties>
                <env>docker</env>
            </properties>
        </profile>   
    </profiles>
    
	<build>
	    <defaultGoal>package</defaultGoal>
	    <finalName>rz-em-etl</finalName>
	    <outputDirectory>target/classes</outputDirectory>
	    <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
        
		<plugins>
			<plugin>
			    <groupId>org.springframework.boot</groupId>
			    <artifactId>spring-boot-maven-plugin</artifactId>
			    <configuration>
			        <fork>true</fork>
			        <mainClass>com.em.Application</mainClass>
			        <layout>ZIP</layout>
			    </configuration>
           </plugin>
           
           <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<!--<version>3.3</version>-->
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<!--<version>2.18.1</version>-->
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<!--<version>2.4</version>-->
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
            	<groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                	<!-- not append assembly id in release file name -->
                	<appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/resources/build/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                    	<id>make-targz</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<!--<version>3.0.2</version>-->
			</plugin>
           
           
		</plugins>
		
		
	</build>

 

二. Dockerfile和其它

Dockerfile:

FROM registry.docker-cn.com/library/centos-oracle-jdk8:v2
MAINTAINER BY sys
ADD output_scm/rz-em-etl.tar.gz /data1/www/rzpaf_release
ENTRYPOINT cd /data1/www/rzpaf_release/rz-em-etl/bin && bash app_control.bash fstart
EXPOSE 8080

打包命令:

sh build.sh docker

打包到服务器相对包路径:

output_scm/rz-em-etl.tar.gz

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值