您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關spring boot中的shell腳本怎么利用jar方式啟動,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
用spring boot框架做的項目,將第三方包全部打在jar里面,通過shell腳本啟動和停止服務,常用的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初始化中通過這個路徑讀取外部配置文件,然后解析成對象,如下:
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 Map<String,MetricObj> metricMap; public AlarmConfigService (){ metricMap = new HashMap<String,MetricObj>(); 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<String, List<Map<String,String>>> object = (Map<String, List<Map<String,String>>>) yaml.load(getAlarmConfigFile()); Map<String, List<Map<String,String>>> object = (Map<String, List<Map<String,String>>>) yaml.load(buffer); logger.info("object==" + object); parseConfigMap(object); } catch (Exception e) { e.printStackTrace(); } } public Map<String, MetricObj> getMetricMap() { return metricMap; } //{metricName=當前響應時間, alarmValue=10,20,40, columnName=response_time}, private void parseConfigMap(Map<String,List<Map<String,String>>> object){ MetricObj obj = null; for (String key : object.keySet()) { List<Map<String,String>> values = object.get(key); for(Map<String,String> map : values){ obj = new MetricObj(); String metricName = map.get("metricName"); obj.setAlarmValue(map.get("alarmValue")); obj.setColumnName(map.get("columnName")); obj.setTableName(map.get("tableName")); obj.setMetricName(metricName); metricMap.put(metricName,obj); } } } private static String getAlarmConfigFile() { return System.getProperty("alarm.config.file"); } }
關于spring boot中的shell腳本怎么利用jar方式啟動就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。