spring boot 以jar的方式启动常用shell脚本
用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");
}
}
spring boot 以jar的方式启动常用shell脚本的更多相关文章
- Spring Boot 以 jar 包方式运行在后台
spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...
- spring boot 不占用端口方式启动
随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供RPC服务,可以不用占用web启动的端口,此时 ...
- Spring boot 执行jar文件 方式
项目jar包名wxo.jar 清理,打包,跳过测试(不测试) mvn clean package -Dmaven.test.skip=true 后台执行(默认环境) nohup java -jar w ...
- spring boot 服务 正确关闭方式
引言 Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的 ...
- Spring Boot应用的启动和停止(Spring Boot应用通过start命令启动)
Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的基于S ...
- Spring Boot Executable jar/war 原理
spring boot executable jar/war spring boot里其实不仅可以直接以 java -jar demo.jar的方式启动,还可以把jar/war变为一个可以执行的脚本来 ...
- Spring Boot由jar包转成war包
Spring Boot由jar包转成war包 spring boot 默认是以jar包形式启动web程序,在新建spring boot项目时候可以选择war包的启动方式. 建议在开发的时候建立以jar ...
- (四)Springboot以jar包方式启动、关闭、重启脚本
Springboot以jar包方式启动.关闭.重启脚本 1.启动 2.关闭 3.重启 4.脚本授权 SpringBoot: nohup java -jar zlkj-data-server-1.0-S ...
- Spring boot 打成jar包问题总结
Spring boot 打成jar包问题总结 1.Unable to find a single main class from the following candidates 1.1.问题描述 m ...
随机推荐
- 【转载】IE下利用滤镜实现背景颜色渐变
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nginx日志配置指令详解
这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...
- Tomcat闲聊第二话
windows下安装Tomcat可以直接下载Installer,需要注意的是,先确保安装了Java虚拟机. 注:默认安装路径为 C:\Program Files\Apache Software Fou ...
- Nginx编译安装第三方模块http_substitutions_filter_module2222
Nginx编译安装第三方模块http_substitutions_filter_module Rming -- 阅读 安装 Http 编译 module filter nginx 模块 >> ...
- HDU高精度总结(java大数类)
HDU1002 A + B Problem II [题意]大数相加 [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1002 Sample Inpu ...
- linux服务器宕机分析/性能瓶颈分析
linux服务器宕机分析/性能瓶颈分析 服务器宕机原因很多,资源不足.应用.硬件.系统内核bug等,以下一个小例子 服务器宕机了,首先得知道服务器宕机的时间点,然后分析日志查找原因 1.last ...
- 创建动作action类:
Action类是Struts 2应用程序的关键,我们实现了大部分Action类中的业务逻辑.因此,让我们创建一个Java文件HelloWorldAction.java的Java资源> SRC下一 ...
- Codeforces Round #404 (Div. 2) DE
昨晚玩游戏竟然不小心错过了CF..我是有多浪啊. 今天总算趁着下课时间补了,感觉最后两题还是挺有意思的,写个题解. D: 题目大意: 给出一个括号序列,问有多少个子序列 是k个'(' + k个')' ...
- 详解 比特(位,bit),字节(Byte),字符的区别 *(转)
比特(位):英文bit,是计算机晶体管的一种状态(通电与断电).就是0与1,真与假,是计算机最基本的传输单位. 示例: 2bit : 10; 4bit : 1111; 8bit : 1111 1111 ...
- Ajax工作原理及实例
1.关于ajax的名字 ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式. 2. ...