当前位置:   article > 正文

Docker容器之SpringBoot多环境部署_docker 环境下srpingboot的多环境配置怎么做

docker 环境下srpingboot的多环境配置怎么做

一.  项目结构:

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

app_control.bash:

  1. #!/bin/bash
  2. set -o pipefail
  3. set -u
  4. EXTERNAL_LOADER_PATH="../conf"
  5. SERVER_PORT=8080
  6. SHUTDOWN_HOST=127.0.0.1
  7. EXEC_STD_OUT=/dev/null
  8. LOG_ROOT_PATH="./logs"
  9. LOG_FILE_NAME="em.log"
  10. #source env.bash
  11. #evironment set
  12. WEB_BIN=`pwd`
  13. CONF_DIR="${WEB_BIN}/../conf"
  14. CONFIG_LOCATION=/data1/www/rz_entrance/config/java_config/rz_em_etl/application.properties
  15. 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"
  16. JAVA_OPTS="${JAVA_OPTS} -Dserver.port=${SERVER_PORT}"
  17. #JAVA_OPTS="${JAVA_OPTS} -Dloader.path=${WEB_BIN}/../conf,${EXTERNAL_LOADER_PATH}"
  18. #JAVA_OPTS="${JAVA_OPTS} -Dlogging.path=${LOG_ROOT_PATH} -Dendpoints.logfile.external-file=${LOG_ROOT_PATH}/${LOG_FILE_NAME}"
  19. START_UP_EXEC="java -Dfile.encoding=utf-8 -Dspring.config.location=${CONFIG_LOCATION} ${JAVA_OPTS} -jar $WEB_BIN/../lib/rz-em-etl.jar"
  20. #Time Config
  21. START_WAIT=5
  22. SHUT_WAIT=10
  23. KILL_INTERVAL=5
  24. KILL_TIMES=5
  25. #RETURN CODES
  26. RET_SUCCESS=0
  27. RET_INSTANCE_DEAD=1
  28. RET_ERROR_SHUT=2
  29. RET_ERROR_START=3
  30. RET_STATUS_ALIVE=0
  31. RET_STATUS_NOT_ALIVE=1
  32. INSTANCE_PID=rz-em-etl.pid
  33. function get_Pid(){
  34. if [ ! -z ${INSTANCE_PID} ];then
  35. if [ -f ${INSTANCE_PID} ];then
  36. cat "${INSTANCE_PID}"
  37. fi
  38. else
  39. ps -ef | grep -vE "grep $WEB_BIN|$0" | grep ${WEB_BIN} | awk '{print $2}'
  40. fi
  41. }
  42. #get pid
  43. PID=`get_Pid`
  44. function usage(){
  45. cat <<EOM
  46. Purpose : This script encapsulates the spring boot jar and just acts like a controller.
  47. Usage : bash ${0} start|shutdown|kill|force|restart|status
  48. Date : 2017.03
  49. EOM
  50. }
  51. #test instance alived or not
  52. function is_Instance_Alive(){
  53. #kill -0 : test process alived or not
  54. if `kill -0 ${PID} 2>/dev/null` ; then
  55. #0 stands for success in shell
  56. return ${RET_SUCCESS}
  57. else
  58. return ${RET_INSTANCE_DEAD}
  59. fi
  60. }
  61. ## remove pid file
  62. function remove_Pid(){
  63. if [ ! -z ${INSTANCE_PID} ];then
  64. if [ -f ${INSTANCE_PID} ];then
  65. rm -f ${INSTANCE_PID}
  66. fi
  67. fi
  68. }
  69. ## shutdown Instance
  70. function shutdown_Instance(){
  71. if ! is_Instance_Alive ;then
  72. echo "no need to stop, not found PID"
  73. return ${RET_SUCCESS}
  74. else
  75. echo -n "shutdown instance"
  76. for((i=0;i<=$KILL_TIMES;i++ ));do #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
  77. curl -d "" "http://${SHUTDOWN_HOST}:${SERVER_PORT}/shutdown"
  78. sleep_Wait ${KILL_INTERVAL}
  79. #check if kill of this round success
  80. if is_Instance_Alive ; then
  81. kill_Instance
  82. return ${RET_SUCCESS}
  83. fi
  84. if ! is_Instance_Alive ; then
  85. remove_Pid
  86. echo shutdown successfully
  87. return ${RET_SUCCESS}
  88. fi
  89. done
  90. return ${RET_ERROR_SHUT}
  91. fi
  92. }
  93. #normal kill
  94. function kill_Instance(){
  95. if ! is_Instance_Alive ;then
  96. echo "no need to stop, not found PID"
  97. return ${RET_SUCCESS}
  98. else
  99. echo -n "killing $PID "
  100. for((i=0;i<=$KILL_TIMES;i++ ));do #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
  101. kill -15 ${PID}
  102. sleep_Wait ${KILL_INTERVAL}
  103. #check if kill of this round success
  104. if ! is_Instance_Alive ; then
  105. remove_Pid
  106. echo kill successfully
  107. return ${RET_SUCCESS}
  108. fi
  109. done
  110. return ${RET_ERROR_SHUT}
  111. fi
  112. }
  113. #-9 kill
  114. function force_Instance(){
  115. if ! is_Instance_Alive ;then
  116. echo "no need to force kill, not found PID"
  117. return ${RET_SUCCESS}
  118. else
  119. echo -n "force killing $PID "
  120. for((i=0;i<=$KILL_TIMES;i++ ));do
  121. #kill instance for KILL_TIMES, each with KILL_INTERVAL secs
  122. kill -9 ${PID}
  123. sleep_Wait ${KILL_INTERVAL}
  124. if ! is_Instance_Alive ; then
  125. remove_Pid
  126. echo force kill successfully #check if force kill of this round success
  127. return ${RET_SUCCESS}
  128. fi
  129. done
  130. echo shutdown failed, please manually check
  131. return ${RET_ERROR_SHUT}
  132. fi
  133. }
  134. #wait
  135. function sleep_Wait(){
  136. local sec=$1
  137. for((i=1;i<=$sec;i++ ));do
  138. sleep 1
  139. echo -n "."
  140. done
  141. echo
  142. }
  143. #start up
  144. function startup_Instance(){
  145. ##This function startup the instance
  146. if is_Instance_Alive; then
  147. echo no need to start, instance pid : ${PID}, exit
  148. else
  149. echo exec ${START_UP_EXEC}
  150. echo -n "starting "
  151. nohup ${START_UP_EXEC} > ${EXEC_STD_OUT} 2>&1 &
  152. if [ ! -z ${INSTANCE_PID} ];then
  153. echo $! > "${INSTANCE_PID}"
  154. fi
  155. sleep_Wait ${START_WAIT}
  156. if ! is_Instance_Alive ; then
  157. echo start successfully : `get_Pid`
  158. return ${RET_SUCCESS}
  159. else
  160. echo start failed
  161. return ${RET_ERROR_START}
  162. fi
  163. fi
  164. }
  165. #start up
  166. function startup_Instance_Without_Nohup(){
  167. ##This function startup the instance
  168. if is_Instance_Alive; then
  169. echo no need to start, instance pid : ${PID}, exit
  170. else
  171. echo exec ${START_UP_EXEC}
  172. echo -n "starting "
  173. ${START_UP_EXEC}
  174. if [ ! -z ${INSTANCE_PID} ];then
  175. echo $! > "${INSTANCE_PID}"
  176. fi
  177. sleep_Wait ${START_WAIT}
  178. if ! is_Instance_Alive ; then
  179. echo start successfully : `get_Pid`
  180. return ${RET_SUCCESS}
  181. else
  182. echo start failed
  183. return ${RET_ERROR_START}
  184. fi
  185. fi
  186. }
  187. #check instance status
  188. function status_Instance(){
  189. if is_Instance_Alive;then
  190. echo "Instance is Alived, pid:${PID}"
  191. return ${RET_STATUS_ALIVE}
  192. else
  193. echo "Instance is not Alived"
  194. return ${RET_STATUS_NOT_ALIVE}
  195. fi
  196. }
  197. #dispatcher
  198. function dispatcher(){
  199. if [ $# -lt 1 ] ;then
  200. usage
  201. exit -1
  202. fi
  203. # if [ -z ${PROFILES} ];then
  204. # echo "Please specific profiles before excute this script, example: export PROFILES=dev"
  205. # exit -1
  206. # fi
  207. local args=$1
  208. case "$args" in
  209. kill)
  210. kill_Instance
  211. ;;
  212. force)
  213. force_Instance
  214. ;;
  215. start)
  216. startup_Instance
  217. ;;
  218. fstart)
  219. startup_Instance_Without_Nohup
  220. ;;
  221. shutdown)
  222. shutdown_Instance
  223. ;;
  224. restart)
  225. shutdown_Instance && startup_Instance
  226. ;;
  227. status)
  228. status_Instance
  229. ;;
  230. *)
  231. usage
  232. ;;
  233. esac
  234. }
  235. dispatcher "$@"
  236. #END

assembly.xml:

  1. <assembly>
  2. <id>release</id>
  3. <formats>
  4. <format>tar.gz</format>
  5. </formats>
  6. <fileSets>
  7. <fileSet>
  8. <directory>${project.build.directory}</directory>
  9. <outputDirectory>lib</outputDirectory>
  10. <directoryMode>0755</directoryMode>
  11. <fileMode>0755</fileMode>
  12. <includes>
  13. <include>*.jar</include>
  14. </includes>
  15. </fileSet>
  16. <fileSet>
  17. <directory>src/main/resources/conf</directory>
  18. <outputDirectory>conf</outputDirectory>
  19. <directoryMode>0755</directoryMode>
  20. <fileMode>0644</fileMode>
  21. </fileSet>
  22. <fileSet>
  23. <directory>src/main/resources/bin</directory>
  24. <outputDirectory>bin</outputDirectory>
  25. <directoryMode>0755</directoryMode>
  26. <fileMode>0755</fileMode>
  27. </fileSet>
  28. </fileSets>
  29. </assembly>

build.sh:

  1. #!/bin/bash
  2. HOME_DIR=`cd $(dirname $0);pwd`
  3. echo $HOME_DIR
  4. BUILD_DIR=$HOME_DIR/output_scm
  5. # rz-em-etl.tar.gz
  6. RZ_EM_ETL_FILE_NAME=rz-em-etl.tar.gz
  7. RZ_EM_ETL=$HOME_DIR/target/$RZ_EM_ETL_FILE_NAME
  8. if [[ -z $2 ]]; then
  9. MODULE=all
  10. else
  11. MODULE=$2
  12. fi
  13. echo "Begin building..."
  14. # re-create BUILD_DIR
  15. rm -rf $BUILD_DIR
  16. mkdir -p $BUILD_DIR
  17. # building
  18. mvn clean package -DskipTests -P ${1}
  19. # checking mvn buile status
  20. if [ $? -ne 0 ]
  21. then
  22. echo "Project build faild. Mvn build faild."
  23. exit 1
  24. fi
  25. if [[ ${MODULE} == 'all' ]] || [[ ${MODULE} == 'rz-em-etl' ]]; then
  26. if [ -f $RZ_EM_ETL ];then
  27. cp -f $RZ_EM_ETL .
  28. # tar czf rz-em-etl.tar.gz rz-em-etl.jar
  29. cp -f rz-em-etl.tar.gz $BUILD_DIR
  30. md5sum rz-em-etl.tar.gz|cut -f 1 -d " " > $BUILD_DIR/rz-em-etl.tar.gz.md5
  31. else
  32. echo "No $RZ_EM_ETL found."
  33. exit 1
  34. fi
  35. fi
  36. echo "Project has built..."

pom.xml(部分):

  1. <profiles>
  2. <profile>
  3. <id>dev</id>
  4. <properties>
  5. <env>dev</env>
  6. </properties>
  7. <activation>
  8. <activeByDefault>true</activeByDefault>
  9. </activation>
  10. </profile>
  11. <profile>
  12. <id>ga</id>
  13. <properties>
  14. <env>ga</env>
  15. </properties>
  16. </profile>
  17. <profile>
  18. <id>docker</id>
  19. <properties>
  20. <env>docker</env>
  21. </properties>
  22. </profile>
  23. </profiles>
  24. <build>
  25. <defaultGoal>package</defaultGoal>
  26. <finalName>rz-em-etl</finalName>
  27. <outputDirectory>target/classes</outputDirectory>
  28. <resources>
  29. <resource>
  30. <directory>src/main/resources</directory>
  31. <filtering>false</filtering>
  32. </resource>
  33. </resources>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. <configuration>
  39. <fork>true</fork>
  40. <mainClass>com.em.Application</mainClass>
  41. <layout>ZIP</layout>
  42. </configuration>
  43. </plugin>
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-compiler-plugin</artifactId>
  47. <!--<version>3.3</version>-->
  48. <configuration>
  49. <source>1.8</source>
  50. <target>1.8</target>
  51. <encoding>UTF-8</encoding>
  52. </configuration>
  53. </plugin>
  54. <plugin>
  55. <groupId>org.apache.maven.plugins</groupId>
  56. <artifactId>maven-surefire-plugin</artifactId>
  57. <!--<version>2.18.1</version>-->
  58. <configuration>
  59. <skip>true</skip>
  60. </configuration>
  61. </plugin>
  62. <plugin>
  63. <groupId>org.apache.maven.plugins</groupId>
  64. <artifactId>maven-source-plugin</artifactId>
  65. <!--<version>2.4</version>-->
  66. <executions>
  67. <execution>
  68. <id>attach-sources</id>
  69. <goals>
  70. <goal>jar</goal>
  71. </goals>
  72. </execution>
  73. </executions>
  74. </plugin>
  75. <plugin>
  76. <groupId>org.apache.maven.plugins</groupId>
  77. <artifactId>maven-assembly-plugin</artifactId>
  78. <configuration>
  79. <!-- not append assembly id in release file name -->
  80. <appendAssemblyId>false</appendAssemblyId>
  81. <descriptors>
  82. <descriptor>src/main/resources/build/assembly.xml</descriptor>
  83. </descriptors>
  84. </configuration>
  85. <executions>
  86. <execution>
  87. <id>make-targz</id>
  88. <phase>package</phase>
  89. <goals>
  90. <goal>single</goal>
  91. </goals>
  92. </execution>
  93. </executions>
  94. </plugin>
  95. <plugin>
  96. <artifactId>maven-resources-plugin</artifactId>
  97. <!--<version>3.0.2</version>-->
  98. </plugin>
  99. </plugins>
  100. </build>

 

二. Dockerfile和其它

Dockerfile:

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

打包命令:

sh build.sh docker

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

output_scm/rz-em-etl.tar.gz

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/995818
推荐阅读
相关标签
  

闽ICP备14008679号