springboot部署学习笔记
1 如何优雅的关闭服务?
对于停止springboot项目,一般先要做以下几点。
- 停止接收请求和内部线程
- 判断是否有线程正在执行
- 等待线程执行完毕
- 停止容器
第一种
springboot提供的用来对应用系统进行自省和监控的功能模块。使用这个进行关闭服务需要做一些配置,使得shutdown暴露在外,我们去调用。
1)导入maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2)properties配置
# 监控权限全开,可以查看应用监控信息
management.security.enabled=false
# 开启关闭引用
endpoints.shutdown.enabled: true
management.endpoints.web.exposure.include=shutdown
Actuator的一些学习记录:
Actuator监控的所有项目都定义在spring-boot-actuator-x.x.x.RELEASE.jar的org.springframework.boot.actuate.endpoint包中,包含以下Endpoint。
这些Endpoint都继承自AbstractEndpoint,AbstractEndpoint中定义了两个重要的属性:enabled和sensitive。
其中,enabled用来打开或关闭该监控项,语法为:
endpoints.[endpoint_name].enabled=false/true
,以关闭/autoconfig监控项为例,其配置如下。
endpoints.autoconfig.enabled=false
sensitive用来配置该监控项是否属于敏感信息,访问敏感信息需要用户具有ACTUATOR角色权限,或者使用以下配置关闭安全限制。
management.security.enabled=false
# 监控权限,查看应用监控信息,web就是通过http的形式,jmx就是通过jmx的形式 # 这里设置false就是关闭监控 management.endpoints.enabled-by-default=true # 开启/actuator路径下的端点 #management.endpoints.web.base-path=/actuator # 这里的* 开启全部,设置名字即是开启那个端点 management.endpoints.web.exposure.include=shutdown,info,health # 这里设置排除的端点 management.endpoints.web.exposure.exclude=
3)编写Bean配置类
@Configuration
public class ShutdownConfig {
@PreDestroy
public void preDestroy() {
System.out.println("关闭服务了哦!!!!");
}
}
第二种
调用SpringApplication.exit();方法退出服务。
编写一个Controller类,实现ApplicationContextAware类。
@Controller
public class BicycleMonitorController implements ApplicationContextAware {
private ApplicationContext context;
@GetMapping("/shutdown")
@ResponseBody
public void shutdown() {
int exitCode = SpringApplication.exit(context);
System.exit(exitCode);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
第三种
在springboot项目启动以后将进程号写入一个固定的文件中,比如当前项目目录下的 pid.txt 文件中。
停止服务时候:cat ./pid.txt | xargs kill 命令直接停止服务,对于这样的停止服务方式,我们前面写的@PreDestroy方法中的代码也会执行的。也是大家用的比较对的一种关闭服务的方法。写一个start.sh用于启动springboot程序,然后写一个停止程序将服务停止。
在springboot的启动类中做以下修改即可。
SpringApplication application = new SpringApplication(XXXXXXApplication.class);
application.addListeners(new ApplicationPidFileWriter("./pid.txt"));
application.run();
关闭服务脚本
#!/bin/bash
PID=$(ps -ef | grep Xxxx-xx.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
2 启动服务
1)通过Maven将项目打成jar包
2)简单运行:java -jar Xxx-xxx.jar
设置内存的方式启动jar:java -jar -Xmx2G xxxxxx-xxx.jar
查看进程: ps -ef | grep java
结束进程:
暴力结束:kill -9 xxxxx(进程号) ------------不推荐
优雅关闭:上述1
在服务启动之前可以做一些健康检查以便优雅的上线,可以监听ApplicationReadyEvent事件。
3)Liux运行jar包
① 命令启动
nohup java -jar Xxx-server-0.0.1.jar > catalina.out 2>&1 &
- catalina.out 将标准输出指向定制文件;
- 2>&1 输出所有的日志文件
- & 后台启动
nohup java -jar xxx.jar >/dev/null &
- 表示不要输出日志
② 脚本启动
#!/bin/bash
nohup java -jar yourapp.jar --server.port=8088 &
脚本整合:先执行关闭脚本,在执行启动脚本
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh
hup java -jar yourapp.jar --server.port=8088 &
脚本整合:先执行关闭脚本,在执行启动脚本
```bash
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh