Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the service packaging scheme of Springboot based on assembly?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What is the service packaging scheme of Springboot based on assembly? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Before using assembly to package the springboot microservice project, I'd like to talk about several common deployment methods of the springboot project today.

Use the docker container to deploy, build the springboot application into a docker image, and then start the image through the container. This way is very convenient when you need to deploy large-scale applications and application expansion. It belongs to the current industrial deployment scheme, but you need to master the docker ecosystem technology.

Use fatjar to deploy directly, which is a simple way for many beginners or very small-scale applications to deploy.

First, why package springboot services?

Recently I saw a project team, they are using springboot to develop the project build interaction to the operations team is a springboot fatjar. Moreover, this kind of original package is undoubtedly fatal to the operation and maintenance staff in the traditional project development company. After the project is delivered, the entire configuration file is hidden in the typed jar, so it becomes a very difficult thing to modify the configuration file for different environments. Therefore, when we introduce any new technology in the company, we must consider how to do service and engineering. If we only refer to the technical framework, we may only need to add a few dependencies. Take a look at api to write a few lines of code to run.

In view of the above problems, if we want to do service and engineering, we need to solve two problems:

Enable springboot to load configuration files outside of jar.

Provide a service-oriented startup script, which is usually bat under shell or windows. With springboot's application service script, you can easily start and stop springboot applications.

2. Packaged springboot application structure diagram

Let's first take a look at the effect of packaging springboot services using assembly.

3. Important steps of service-oriented packaging

Here are the detailed steps for packaging springboot.

3.1 add the assembly packaging plug-in maven-assembly-plugin 3.0.0 src/main/assembly/assembly.xml make-assembly package single

From the above code, we can see that I put the configuration of assembly in the main directory. This is a habit, or you don't have to put it here. Here is a general structure diagram of assembly in the project:

3.2 assembly.xml configuration

The configuration of assembly is similar to that of the following configuration, just packaging service scripts, jar, configuration files, and so on. From the config configuration in the following code, you can see that assembly typed the configuration file under config.

1.0 tar.gz src/main/assembly/bin bin 0755 src/main/assembly/config config 0644 target lib * .jar Src/main/resources logs 0755 * * / * 3.3.Writing service scripts

Now write a script for the linux environment.

First: start.sh startup script

#! / bin/bashSERVER_NAME='spring-vue'# jar name JAR_NAME='springboot-vue.jar'cd `dirname $0`BIN _ DIR= `pwd`cd.. DEPLOY_DIR= `pwd`CONF _ DIR=$DEPLOY_DIR/config# SERVER_PORT= `sed'/ server.port Tr-d'\ r' `# get the port number of the application SERVER_PORT= `sed-nr'/ port: [0-9] + / s/.*port: + ([0-9] +). * /\ 1p' config/ application. Yml`PIDs = `ps-f | grep java | grep "$CONF_DIR" | awk'{print $2} '`if ["$1" = "status"]; then if [- n "$PIDS"] Then echo "The $SERVER_NAME is running.!" Echo "PID: $PIDS" exit 0 else echo "The $SERVER_NAME is stopped" exit 0 fifiif [- n "$PIDS"]; then echo "ERROR: The $SERVER_NAME already started!" Echo "PID: $PIDS" exit 1fiif [- n "$SERVER_PORT"]; then SERVER_PORT_COUNT= `netstat-tln | grep $SERVER_PORT | wc-l`if [$SERVER_PORT_COUNT-gt 0]; then echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!" Exit 1 fifiLOGS_DIR=$DEPLOY_DIR/logsif [!-d $LOGS_DIR]; then mkdir $LOGS_DIRfiSTDOUT_FILE=$LOGS_DIR/stdout.logJAVA_OPTS= "- Djava.awt.headless=true-Djava.net.preferIPv4Stack=true" JAVA_DEBUG_OPTS= "if [" $1 "=" debug "]; then JAVA_DEBUG_OPTS="-Xdebug-Xnoagent-Djava.compiler=NONE-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "fiJAVA_JMX_OPTS="if [" $1 "=" jmx "] Then JAVA_JMX_OPTS= "- Dcom.sun.management.jmxremote.port=1099-Dcom.sun.management.jmxremote.ssl=false-Dcom.sun.management.jmxremote.authenticate=false" fiJAVA_MEM_OPTS= "BITS= `java-version 2 > & 1 | grep-I 64-bit`if [- n" $BITS "] Then JAVA_MEM_OPTS= "- server-Xmx512m-Xms512m-Xmn256m-XX:PermSize=128m-Xss256k-XX:+DisableExplicitGC-XX:+UseConcMarkSweepGC-XX:+CMSParallelRemarkEnabled-XX:+UseCMSCompactAtFullCollection-XX:LargePageSizeInBytes=128m-XX:+UseFastAccessorMethods-XX:+UseCMSInitiatingOccupancyOnly-XX:CMSInitiatingOccupancyFraction=70" else JAVA_MEM_OPTS= "- server-Xms512m-Xmx512m-XX:PermSize=128m-XX:SurvivorRatio=2-XX:+UseParallelGC" fiCONFIG_FILES= "- Dlogging.path=$LOGS_DIR-Dlogging.config=$CONF_DIR/log4j2.xml-Dspring.config.location=$CONF_DIR/application.properties" echo- E "Starting the $SERVER_NAME..." nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES-jar $DEPLOY_DIR/lib/$JAR_NAME > $STDOUT_FILE 2 > & 1 & COUNT=0while [$COUNT-lt 1] Do echo-e ".\ c" sleep 1 if [- n "$SERVER_PORT"]; then COUNT= `netstat-an | grep $SERVER_PORT | wc-l`netstat `ps-f | grep java | grep "$DEPLOY_DIR" | awk'{print $2}'| wc-l`fi if [$COUNT-gt 0] Then break fidoneecho "OK!" PIDS= `ps-f | grep java | grep "$DEPLOY_DIR" | awk'{print $2} '`echo "PID: $PIDS" echo "STDOUT: $STDOUT_FILE"

Script use case:

# launch the application. / start.sh# starts the task in debug mode. / start debug# starts the task and starts jmx monitoring. / start jmx# to get the current running status. / start status

Stop script: stop.sh

#! / bin/bashcd `BIN $0`BIN _ DIR= `pwd`cd.. DEPLOY_DIR= `pwd`CONF _ DIR=$DEPLOY_DIR/configSERVER_NAME=$DEPLOY_DIRPIDS= `ps-ef | grep java | grep "$CONF_DIR" | awk'{print $2} '`if [- z "$PIDS"]; then echo "ERROR: The $SERVER_NAME does not started!" Exit 1fiif ["$1"! = "skip"]; then $BIN_DIR/dump.shfiecho-e "Stopping the $SERVER_NAME.\ c" for PID in $PIDS; do kill $PID > / dev/null 2 > & 1doneCOUNT=0while [$COUNT-lt 1]; do echo-e ".\ c" sleep 1 COUNT=1 for PID in $PIDS; do PID_EXIST= `ps-f-p $PID | grep java`if [- n "$PID_EXIST"] Then COUNT=0 break fi donedoneecho "OK!" echo "PID: $PIDS"

Startup script for the windows environment:

Echo offset APP_NAME=springboot-vue.jarset CONFIG=-Dlogging.path=../logs-Dlogging.config=../config/log4j2.xml-Dspring.config.location=../config/application.yml set DEBUG_OPTS=if "% 1" = = "" debug "" (set DEBUG_OPTS=-Xloggc:../logs/gc.log-verbose:gc-XX:+PrintGCDetails-XX:+HeapDumpOnOutOfMemoryError-XX:HeapDumpPath=../logs goto debug) set JMX_OPTS=if "% 1" = = "" jmx "" ( Set JMX_OPTS=-Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.port=9888-Dcom.sun.management.jmxremote.ssl=FALSE-Dcom.sun.management.jmxremote.authenticate=FALSE goto jmx) echo "Starting the% APP_NAME%" java-Xms512m-Xmx512m-server% DEBUG_OPTS%% JMX_OPTS%% CONFIG%-jar. / lib/%APP_NAME%goto end:debugecho "debug" java-Xms512m-Xmx512m-server% DEBUG_OPTS%% CONFIG%-jar. / lib/%APP_NAME%goto end:jmxjava-Xms512m-Xmx512m-server% JMX_OPTS%% CONFIG%-jar. / lib/%APP_NAME%goto end:endpause

For different springboot projects, you only need to modify the script appropriately. In order to save space, no other scripts are listed here. Please refer to the demo: https://github.com/Shalousun/springboot-vue.git I submitted. If you have any questions about demo, you can add this group: 170651381.

Ps: the above script is referenced from the official dubbo. In fact, the lightweight construction of dubbo projects is similar.

Important: there are so many scripts mentioned above, in fact, it is troublesome to add a bunch of assembly configuration every time you create a project, and you need to modify the jar name in the script, and the modification is inconsistent and may make mistakes, so you can use your own open source project framework to build scaffolding https://gitee.com/sunyurepository/ApplicationPower to automatically create these configurations and copy them to your project, you just need to write the business code with peace of mind.

Fourth, log path processing of applications after packaging

In the figure in the second section, you can see that the packaged application logs are generally output to the logs directory, but for different system platforms, although the configured log output path is the same, it may not be output to logs in the end. After testing, it is no problem to use the relative log path.. / logs in the windows platform, but for the linux system to use the relative path can not be output to the logs, so it is recommended to write the absolute path in the linux platform. But set the path to the output log in the script I provided

-Dlogging.path=../logs

Therefore, combined with the powerful resolution capability of log4j2, you can set the log path of log42 (you can specify the path manually when developing):

${sys:logging.path}

However, it seems that the access log for springboot applications can only use absolute paths under linux.

# server configserver: port: 8080 undertow: accesslog: enabled: true dir: / usr/xxx/logslogging: path: / usr/xxx/logs

Of course, the students who use the configuration solution can remind and correct it.

The solution itself doesn't bring anything new, and even most of the scripts refer to the official scripts of dubbo, just making some improvements on it. But the important point is how to think about the service and engineering that need to be done to use this technology according to the actual technology application scenario.

The answer to the question about Springboot's assembly-based service package is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report