服务器之家

服务器之家 > 正文

详解spring boot 以jar的方式启动常用shell脚本

时间:2021-01-05 11:26     来源/作者:矮子爬楼梯

spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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

正真启动的命令:

 

复制代码 代码如下:

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 &

 

其中-Dalarm.config.file 指定了外部配置文件的路径,在service初始化中通过这个路径读取外部配置文件,然后解析成对象,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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");
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/ahang/p/6739938.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部