首页  ·  知识 ·  编程语言
springboot安全STOP及启动脚本
网友  CSDN  JAVA  编辑:不羁与醉   图片来源:网络
一:通过HTTP发送shutdown信号该方式主要依赖SpringBootActuator的endpoint特性.

一:通过HTTP发送shutdown信号


该方式主要依赖Spring Boot Actuator的endpoint特性,具体步骤如下:


1. 在pom.xml中引入actuator依赖


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

2. 开启shutdown endpoint

Spring Boot Actuator的shutdown endpoint默认是关闭的,因此在application.properties中开启shutdown endpoint:


#启用shutdown

endpoints.shutdown.enabled=true

#禁用密码验证

endpoints.shutdown.sensitive=false

3. 发送shutdown信号

shutdown的默认url为host:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:

curl -X POST host:port/shutdown

将得到形如{“message”:”Shutting down, bye…”}的响应

4. 安全设置


可以看出,使用该方法可以非常方便的进行远程操作,但是需要注意的是,正式使用时,必须对该请求进行必要的安全设置,比如借助spring-boot-starter-security进行身份认证:


pom.xml添加security依赖


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

开启安全验证

在application.properties中变更配置,并

#开启shutdown的安全验证

endpoints.shutdown.sensitive=true

#验证用户名

security.user.name=admin

#验证密码

security.user.password=secret

#角色

management.security.role=SUPERUSER

指定路径、IP、端口

#指定shutdown endpoint的路径

endpoints.shutdown.path=/custompath

#也可以统一指定所有endpoints的路径`management.context-path=/manage`

#指定管理端口和IP

management.port=8081

management.address=127.0.0.1



二、创建启动脚本boot.sh 一下是脚本内容

 ———————————————— 

#!/bin/bash

JAVA_OPTIONS_INITIAL=-Xms1024M

JAVA_OPTIONS_MAX=-Xmx2048M

_JAR_KEYWORDS=ylb_gateway.jar

APP_NAME=ylb_gateway 

#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

curl -X POST http://localhost:7070/shutdown

else

echo -e "\033[32m $APP_NAME already stoped  \033[0m"

fi

;;

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  > /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

 ———————————————— 

image.png


本文作者:网友 来源:CSDN
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的