Spring Boot jar Setting Environment Parameters at Startup

  • 2021-09-11 20:09:36
  • OfStack

Spring Boot jar Setting Environment Parameters at Startup

1 Summary

Usually, when using Spring Boot to develop projects, it is necessary to set up multiple environments (test environment and production environment, etc.), but the project packaging can only specify one environment. Is there a way to only play one jar package, but specify the project environment when starting? After consulting the data and testing on the Internet, the author found that this function can be realized, which greatly facilitates the deployment of the project (automatic deployment of multiple environments can be realized).

2 core code

2.1 spring Boot Multiple Environment Configuration


../demo-web/src/main/resources/application.yml

## spring config
spring:
  # environment: dev|test|pro
  profiles:
    active: dev

2.2 spring Boot Project Start Command

Linux Command Line Background Start spring boot jar:


nohup java -jar xxx.jar --spring.profiles.active=test > /dev/null 2>&1 &

Modify according to different deployment environments-spring. profiles. active values can be used

3 Spring boot Easy Start and Stop shell Scripts

3.1 Startup script


../doc/script/start-springboot.sh

#!/bin/sh
# 
#  Start  jar  Run 

#  Project deployment directory 
projectDir=/opt/springboot/
#  Project operation  jar  Name 
jarName="springbootdemo.jar"
#  Script log directory 
logDir=/var/log/springbootdemo/
#  Project deployment environment 
profileActive=dev

#  Here's -x  Parameter judgment ${logDir} Does it exist and has executable permission  
if [ ! -x "${logDir}" ]; then 
  mkdir -p "${logDir}" 
fi 

#  Judgment item SpringBoot Does the program run 
count=$(ps -ef |grep ${jarName} |grep -v "grep" |wc -l)
if [ ${count} -lt 1 ]; then
    cd ${projectDir}
    nohup java -jar ${jarName} --spring.profiles.active=${profileActive} > /dev/null 2>&1 &
    echo "$(date '+%Y-%m-%d %H:%M:%S')  Start  ${jarName}  Program  ... ..." >> ${logDir}$(date "+%Y-%m-%d").log    
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') ${jarName}  The program is running normally  !!! !!!" >> ${logDir}$(date "+%Y-%m-%d").log
fi

3.2 Stop Scripts


../doc/script/stop-springboot.sh

#!/bin/sh
# 
#  Stop  jar  Run 

#  Project deployment directory 
projectDir="/opt/springboot/"
#  Project operation  jar  Name 
jarName="springbootdemo.jar"
#  Script name 
scriptName="stop-springboot.sh"

#  Judgment item SpringBoot Does the program run 
count=$(ps -ef |grep ${jarName} |grep -v "grep" |wc -l)
if [ ${count} -gt 0 ]; then
    echo " Already exists  ${count}  A ${jarName}  The program is running "
    #  Get the running program process  id( Exclude  grep  In itself, awk  Command and script itself )
    jarPid=$(ps x | grep ${jarName} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
    #  Stop the running project process  
    kill -9 ${jarPid}
    output=`echo " Closing ${jarName} Program , Process id: ${jarPid}"`
    echo ${output}
else
    echo ' There is no corresponding program running '
fi

#  Delete   jar  Bag 
rm -rf ${projectDir}${jarName}
#  Enter   Project deployment directory 
cd ${projectDir}

3.3 Monitoring the Spring Boot Project

If the project stops running for various reasons in the production environment, the server can't provide services to the outside world at this time. In order to ensure that the service can provide services continuously in the unattended time period, it is particularly important to realize the automatic repair/restart of the project. Here, in a simple and rude way, the project hangs up and restarts directly, and the startup script can be executed by using timed tasks.

The timed task crontab is simple to use, taking centOS 7 as an example:

Start-up timed task service


systemctl enable cornd

Start a timed task


systemctl start cornd

Turn off the Timed Task Service


systemctl stop crond

Add and edit timed tasks


## spring config
spring:
  # environment: dev|test|pro
  profiles:
    active: dev
0

The contents are as follows:


## spring config
spring:
  # environment: dev|test|pro
  profiles:
    active: dev
1

The current timed task means to execute the synchronization script once every 10 minutes

cron expression description:


## spring config
spring:
  # environment: dev|test|pro
  profiles:
    active: dev
2

Online generation of cron: http://cron.qqe2.com/

Note: */5**** indicates that it is executed every 5 minutes, but it may not be executed in some systems

4 Github source code

Gtihub source address: https://github.com/Flying9001/springBootDemo

Detailed Explanation of springboot Project Startup Parameters

This article introduces many ways to add startup parameters when springboot project starts. We all know that Spring can inject attributes through @ Value annotation, so what range can @ Value read? This is the main content of this paper.

The following methods can be read by @ Value

1. java-jar-Dserver. port=8888-Xms 1024m demo. jar

Parameters added in this way are set to the system properties of the application and can be obtained using System. getProperty ("server. port") (can be configured in idea VM options of idea, separated by spaces)

-D (defintion) represents custom parameters

2. java-jar demo. jar-server. port=8888

Arguments added in this way are command-line arguments that are passed in as arguments from String [] args of main method when springboot is started (can be configured in program arguments of idea, separated by spaces)

3. Read from the environment variables of the operating system

The parameters in this way belong to the operating system, such as the environment variables set when installing jdk and defining JAVA_HOME, or can be obtained through System. getenv ("JAVA_HOME") (can be configured in VM Environment variables of idea to; Separate)

4. Load through the configuration file bootstrap/application in the project

This method is configured in the project, which is more common

These are the common ways to introduce


Related articles: