這篇文章將為大家詳細(xì)講解有關(guān)spring boot中的shell腳本怎么利用jar方式啟動,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)長期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為崖州企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),崖州網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
用spring boot框架做的項(xiàng)目,將第三方包全部打在jar里面,通過shell腳本啟動和停止服務(wù),常用的shell腳本模板如下:
#!/bin/bash JAVA_OPTIONS_INITIAL=-Xms128M JAVA_OPTIONS_MAX=-Xmx512M _JAR_KEYWORDS=monitor-alarm-task-1.0-SNAPSHOT.jar APP_NAME=monitor-alarm-task APPLICATION_FILE=/opt/scpip_monitor/application.properties PID=$(ps aux | grep ${_JAR_KEYWORDS} | grep -v grep | awk '{print $2}' ) ALARM_CONFIG_FILE=`pwd`/alarmConfig.yaml function check_if_process_is_running { if [ "$PID" = "" ]; then return 1 fi ps -p $PID | grep "java" return $? } case "$1" in status) if check_if_process_is_running then echo -e "\033[32m $APP_NAME is running \033[0m" else echo -e "\033[32m $APP_NAME not running \033[0m" fi ;; stop) if ! check_if_process_is_running then echo -e "\033[32m $APP_NAME already stopped \033[0m" exit 0 fi kill -9 $PID echo -e "\033[32m Waiting for process to stop \033[0m" NOT_KILLED=1 for i in {1..20}; do if check_if_process_is_running then echo -ne "\033[32m . \033[0m" sleep 1 else NOT_KILLED=0 fi done echo if [ $NOT_KILLED = 1 ] then echo -e "\033[32m Cannot kill process \033[0m" exit 1 fi echo -e "\033[32m $APP_NAME already stopped \033[0m" ;; start) if [ "$PID" != "" ] && check_if_process_is_running then echo -e "\033[32m $APP_NAME already running \033[0m" exit 1 fi nohup java -jar -Dalarm.config.file=$ALARM_CONFIG_FILE $JAVA_OPTIONS_INITIAL $JAVA_OPTIONS_MAX $_JAR_KEYWORDS --spring.config.location=$APPLICATION_FILE > /dev/null 2>&1 & echo -ne "\033[32m Starting \033[0m" for i in {1..20}; do echo -ne "\033[32m.\033[0m" sleep 1 done if check_if_process_is_running then echo -e "\033[32m $APP_NAME fail \033[0m" else echo -e "\033[32m $APP_NAME started \033[0m" fi ;; restart) $0 stop if [ $? = 1 ] then exit 1 fi $0 start ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 esac exit 0
正真啟動的命令:
其中-Dalarm.config.file 指定了外部配置文件的路徑,在service初始化中通過這個(gè)路徑讀取外部配置文件,然后解析成對象,如下:
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.yaml.snakeyaml.Yaml; import scpip.monitor.task.obj.MetricObj; @Service public class AlarmConfigService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private MapmetricMap; public AlarmConfigService (){ metricMap = new HashMap (); init(); } private void init(){ BufferedReader buffer; try { InputStream cpResource = new FileInputStream(getAlarmConfigFile()); buffer = new BufferedReader(new InputStreamReader(cpResource,"utf-8")); Yaml yaml = new Yaml(); //Map >> object = (Map >>) yaml.load(getAlarmConfigFile()); Map >> object = (Map >>) yaml.load(buffer); logger.info("object==" + object); parseConfigMap(object); } catch (Exception e) { e.printStackTrace(); } } public Map getMetricMap() { return metricMap; } //{metricName=當(dāng)前響應(yīng)時(shí)間, alarmValue=10,20,40, columnName=response_time}, private void parseConfigMap(Map >> object){ MetricObj obj = null; for (String key : object.keySet()) { List
關(guān)于spring boot中的shell腳本怎么利用jar方式啟動就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。