开发热部署
在开发中我们修改一个java文件后想要看到效果不得不重启应用,这导致大量时间浪费,我们希望不重启应用的情况下,程序可以自动部署(热部署).有以下四种情况:
模板引擎
- 在SpringBoot开发情况下禁用模板引擎的cache
- 页面模板改变ctrl+F9可以重新编译当前页面并生效
Spring Loaded
Spring官方提供的热部署程序,实现修改类文件的热部署- 下载Spring Loaded(项目地址 https://github.com/spring-projects/spring-loaded)
- 添加运行时参数: -javaagent:C:/spribgloaded-1.2.5.RELEASE.jar -noverify
JRebel
- 收费的一个热部署软件
- 安装插件使用即可
Spring Boot Devtools(推荐)
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency> - IDEA使用ctrl+F9重新编译实现热部署
- 引入依赖
Spring Boot与监控管理
通过引入spring-boot-starter-actuator
,可以使用Spring Boot
为我们提供的准生产环境下的应用监控和管理功能。我们可以通过HTTP
,JMX
,SSH
协议来进行操作,自动得到审计、健康及指标信息等
Actuator监控管理
- 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> - 浏览器打开链接
http://localhost:8080/actuator/
, 可以看到所有支持的连接,响应如下,默认只支持这些端点{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
} - 如果要看到所有支持的状态查询,需要配置
management.endpoints.web.exposure.include=*
- bean加载情况
http://localhost:8080/actuator/beans
, 显示了容器中各类各项属性{
"contexts": {
"application": {
"beans": {
"endpointCachingOperationInvokerAdvisor": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
"dependencies": [
"environment"
]
},
"defaultServletHandlerMapping": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.web.servlet.HandlerMapping",
"resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]",
"dependencies": []
},
}
...端点配置
- 监控和管理端点
端点 | 描述 |
---|---|
autoconfig | 所有自动配置信息 |
auditevents | 审计事件 |
beans | 所有bean的信息 |
configprops | 所有配置属性 |
heapdump | 线程状态信息 |
env | 当前环境信息 |
health | 应用健康状况 |
info | 当前应用信息 |
metrics | 应用的各项指标 |
mappings | 应用@RequestMapping映射路径 |
shutdown | 关闭当前应用(默认关闭),开启后可以远程关闭这个应用 |
trace | 追踪信息(最新的http请求) |
- 默认情况下,除shutdown以外的所有端点均已启用。要配置单个端点的启用,请使用
management.endpoint.<id>.enabled
属性,以下示例启用shutdown端点:true =
- 另外可以通过
management.endpoints.enabled-by-default
来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:false =
true = - 修改路径更多参阅spring-actuator官方文档
# 修改根目录路径
/management =
# 修改/health路径
healthcheck =
自定义健康状态指示器HealthIndicator
1、编写一个指示器,继承AbstractHealthIndicator并实现doHealthCheck
函数或者实现HandlerInterceptor
的healyh函数
2、指示器的名字必须是 XXXHealthIndicator,接口返回的json就叫XXX
|
这里还有一个问题,就是需要打开health的一个配置我们才能看到信息
always = |
自定义EndPoint
1、编写自定义的Endpoint类
2、在类上面标注Endpoint
3、类里面写主方法,并在主方法上标注@ReadOperation
4、编写配置类,然后将自定义的EndPoint类注入到容器中
"mytest") (id = |
编写配置类
|
启动容器后,我们能通过http://localhost:8080/actuator/mytest
url返回我们的信息
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 zenshin's blog!
评论