In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the method tutorial of Spring Boot packaging different environment configuration and Shell script deployment". In the daily operation, I believe many people have doubts about the method tutorial of Spring Boot packaging different environment configuration and Shell script deployment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to package different environment configurations and Shell script deployment by Spring Boot"! Next, please follow the editor to study!
Profiles specifies the configuration of different environments
Usually, a set of programs is divided into many deployment environments: development, testing, uat, online, etc. We can distinguish between configuration files for these environments in two ways:
Specify profile.active=uat by coding in application.yml
Use profiles in mvn to distinguish the configuration folders corresponding to different environments, and manually check the idea to generate packages for different environments (recommended)
What we are going to talk about here is the second, first of all, configure the following in mvn:
12 3 node 4 5 6 node 7 ${scripts_packageName} 8 ${scripts_bootMain} 9 10 11 true 12 13 14 15 node1 16 17 node1 18 ${scripts_packageName} 19 ${scripts_bootMain} 20 21 22 23 node2 24 25 node2 26 ${scripts_packageName} 27 ${scripts_bootMain} 28 29 30
Node rough solution:
Id: used to specify the directory where different environment configuration files are located, as shown below:
Properties: the node in this node can be passed as a parameter to other configuration files. For example, the package-name node value here can be obtained through ${package-name} in another assembly.xml or shell script file, as follows:
ActiveByDefault: specify the default environment configuration folder
Maven-assembly-plugin hits and releases a compressed package.
For springboot program packaging, it can be divided into jar and war. Here is the jar package. In some scenarios, our configuration files or third-party dependent packages do not want to be placed in the project jar, and these files are compressed into a zip package to facilitate uploading to linux; through maven-assembly-plugin and maven-jar-plugin. The configuration of mvn is as follows:
12 org.apache.maven.plugins 3 maven-jar-plugin 4 2.6 5 6 7 false 8 9 true 10 lib/ 11 ${scripts_bootMain} 12 13 14 15 16 * * / * .yml 17 * * / * .properties 18 * / * .xml 19 * * / .sh 20 21 22 23 24 make-a-jar 25 compile 26 27 jar 28 29 30 31 32 33 34 org.apache.maven. Plugins 35 maven-assembly-plugin 36 2.4 37 38 39 40 41 ${project.basedir} / src/main/assembly/assembly.xml 42 43 44 45 46 make-assembly 47 package 48 49 single 50 51 52 53
The noteworthy points are as follows:
MainClass node: used to specify the entry classpath to start the main function, as shown here: com.sm.EurekaServerApplication
Excludes node: exclude some column suffix files such as configuration in the main jar package, because we want to package these configuration files outside the main package
Descriptor node: used to specify the assembly.xml configuration file for the assembly plug-in
With the above mvn configuration, we also need the configuration of assembly.xml. Here we extract the configuration combined with the shell script publisher:
${activeProfile} zip false false ${package-name}-${activeProfile} / lib false ${project.basedir} / src/main/profiles/$ {activeProfile} ${package-name}-${activeProfile} / conf * * / * ${project.basedir} / src/main/scripts * * / * 777 777 true ${project.build.directory} ${package-name}-${activeProfile} / * .jar
Introduction of key nodes:
Formats node: what file format to compress configuration files, jar packages, etc., here can be: zip,tar, etc.
FileMode node: specify that the script file in the scripts directory (here: shenniu_publish.sh) has a file permission of 777 on linux
Filtered node: the parameter variable in the script is the value of properties in the profiles of pom (this configuration is to generate the mapping of attribute values in mvn into a sh file, such as ${package-name})
After the above configuration is completed, we can hit the zip package by selecting the switch between different environments on idea, as shown in the figure:
Share shenniu_publish.sh program startup tools
After the release package in zip format is completed in the above steps, we will share the shell script of the startup program, which has functions such as:
Extract the zip+ startup jar package
Start the jar package
Stop running the corresponding jar
Restart the jar program
At present, there are two ways to start jar commands in this shell:
Java-cp
Java-jar
As shown in the figure, the command format is:
Take a look at all the shell code:
#! / usr/bin/env bash # variable variable languageType= "javac" # support java,javac,netcore release # Parameter values are passed from the pom file baseZipName= "${package-name}-${activeProfile}" # publish packageName= "${package-name}" # command to start the package name xx.jar xx mainclass= "${boot-main}" # java-cp, specify the main entry class; command: java-cp conf Lib\ * .jar;$ {packageName} .jar ${mainclass} # example # baseZipName= "publish-test" # publish # packageName= "publish" # command of the package name publish-test.zip starts the xx # fixed variable basePath=$ (cd `dirname $0` /) of the package name publish.jar Pwd) baseZipPath= "${basePath} / ${baseZipName} .zip" # zip package path baseDirPath= "${basePath}" # decompress deployment disk path pid= # process pid # extract function shenniu_unzip () {echo "decompress -" echo "Compression Package path: ${baseZipPath} "if [! `find ${baseZipPath} `] then echo" No zip package: ${baseZipPath} "else echo" extract disk path: ${baseDirPath} / ${baseZipName} "echo" start decompression. "# extract command unzip-od ${baseDirPath} / ${baseZipName} ${baseZipPath} # set execution permission chmod + x ${baseDirPath} / ${baseZipName} / ${packageName} echo "decompression completed." Fi} # detect pid function getPid () {echo "Detection status -" pid= `ps-ef | grep-n ${packageName} | grep-v grep | awk'{print $2} '`if [${pid}] then echo "run pid : ${pid} "else echo" not running "fi} # launcher function start () {# before startup First stop the previous stop if [${pid}] then echo "stop program failed Unable to start "else echo" launcher-"# Select language type read-p" input program type (java,javac,netcore) Next, press enter (default: ${languageType}): "read_languageType if [${read_languageType}] then languageType=$ {read_languageType} fi echo" Select Program Type: ${languageType} "# enter the running package directory cd ${baseDirPath} / ${baseZipName} # category to launch If ["${languageType}" = "javac"] then if [${mainclass}] then nohup java-cp conf:lib\ * .jar: ${packageName} .jar ${mainclass} > ${baseDirPath} / ${packageName} .out 2 > & 1 & # nohup java-cp conf:lib\ * .jar:$ {packageName} .jar ${mainclass} > / dev/null 2 > & 1 & fi elif ["${languageType}" = "java"] then nohup java-jar ${baseDirPath} / ${baseZipName} / ${packageName} .jar > / dev/null 2 > & 1 & # java-jar ${baseDirPath} / ${baseZipName} / ${packageName} .jar elif ["${languageType}" = "netcore" "] then # nohup dotnet run ${baseDirPath} / ${baseZipName} / ${packageName} > / dev/null 2 > & 1 & nohup ${baseDirPath} / ${baseZipName} / ${packageName} > / dev/null 2 > & 1 & fi # query whether there is a startup process getPid if [${pid}] then Echo "started" # nohup log tail-n 50-f ${baseDirPath} / ${packageName} .out else echo "start failed" fi fi} # stop program function stop () {getPid if [${pid}] then echo "stop program- -- "kill-9 ${pid} getPid if [${pid}] then # stop echo" stop failed "else echo" stop successful " Fi fi} # starts with parameters Execute if [${#}-ge 1] then case ${1} in "start") start according to the parameters ; "restart") start;; "stop") stop;; "unzip") # execute decompress shenniu_unzip # execute start start *) echo "${1} has no operation"; esac else echo "command" command: unzip: extract and start start: start stop: stop the process restart: restart example commands such as:. / shenniu_publish start "fi
As mentioned in the above section, the parameters package-name,activeProfile,boot-main in shell are provided by the properties of profiles in mvn and are variable. The script code itself does not need to be modified manually, but only the parameters of mvn need to be changed. In fact, when we generate the zip package, the parameters in shell are replaced. You can see the contents of the shell file in zip as follows:
Upload the generated zip to linux and decompress it with the command:
1 unzip-od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip
In fact, the shell script contains the decompression command, but I put it in zip when I pack it, so I can only decompress it manually, and of course you can adjust it. Enter the pressurized directory like this:
Note: when the. / shenniu_publish.sh script is executed for the first time, an error message is prompted. It is because I edited the script on windows, and its space and other spaces are different from those on linux, so there will be problems in running. To solve the problem, you can use the vim command to convert the file to linux format in linux, as follows:
1 vim shenniu_publish.sh 2 set ff=unix 3: wq
After execution, run the script. / shenniu_publish.sh with the following prompt:
At the moment, our file is in an unzipped state, so we only need the start command to start the program:
At this point, the use of the shenniu_publish.sh script is completed. As long as the script does not indicate an error, the jar service can be started, and other restart and stop commands can be executed as well:
At this point, on the "Spring Boot packaging different environment configuration and Shell script deployment method tutorial" is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.