欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-28 22:40:16
用spring boot框架做的项目,将第三方包全部打在jar里面,通过shell脚本启动和停止服务,常用的shell脚本模板如下: #!/bin/bash j...

用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

正真启动的命令:

复制代码 代码如下:

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初始化中通过这个路径读取外部配置文件,然后解析成对象,如下:

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");
  }
}

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