SpringBoot高级篇Ⅸ --- 热部署与监控管理

一.热部署

在开发中我们修改一个Java文件后想看到效果不得不重启应用,这导致大量时间花费,我们不希望重启应用的情况下,程序可以自动部署(热部署)。

1.1 模板引擎

在SpringBoot中开发情况下禁用模板引擎的cache,页面模板改变ctrl+F9可以重新编译当前页面并生效。

1.2 Spring Loaded

Spring官方提供的热部署程序,实现修改类文件的热部署,需要下载Spring Loaded(项目地址),使用时需要添加运行时参数:-javaagent:C:/springloaded-1.2.5.RELEASE.jar-noverify

1.3  JRebel

收费的一个热部署软件,安装插件使用。

1.4  Spring Boot Devtools(推荐)

引入依赖,使用IDEA的编译或者ctrl+f9,如果用eclipse使用保存即ctrl+s即可实现热部署。

 

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
 </dependency>

 

.监控管理

通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准生产环境下的应用监控和管理功能。我们可以通过HTTP、JMX,SSH协议来进行操作,自动得到审计、健康及指标信息等。

由于SpringBoot2.x版本在这一块有所变动所以需要将SpringBoot版本切换为1.5.x版本,防止版本问题影响。

2.1 引入spring-boot-starter-actuator

 

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.2 通过http方式访问监控端点

监控和管理端点:

 

2.3 可进行shutdown(POST提交,此端点默认关闭)

可以通过发送"/shutdown"post请求来关闭应用。

2.3.1 开启shutdown关闭应用

endpoints.shutdown.enabled=true

2.3.2 使用postman发送post请求

2.4 定制端点信息

定制端点一般通过endpoints+端点名+属性名来设置。

修改端点id(endpoints.beans.id=mybeans),这样设置以后访问beans端点就需要访问"/mybeans"了

修改端点访问路径(endpoints.beans.path=beans)

开启远程应用关闭功能(endpoints.shutdown.enabled=true)

开启关闭端点(endpoints.beans.enabled=false)

关闭所有端点访问(endpoints.enabled=false)

修改根路径方法(management.context-path=/manage)

修改访问端点的端口(management.port=8181)

2.5 自定义HealthIndicator

访问/health端口,可以看到应用监控的监控状况。例如redis连接是否正常,如果连接地址不正确导致连接不上redis地址,健康状态就会显示down。那么如何自定义一个健康状态指示器呢?

首先,编写一个指示器 实现HealthIndicator接口,并且指示器的名字需要命名为xxxHealthIndicator并加入容器中。

@Component
public class MyHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        //自定义检查方法
      //  return Health.up().build();
        return Health.down().withDetail("msg","服务异常").build();
    }
}

全部编写好后,重启项目访问"/health"就可以查看该指示器了。

 

posted @ 2019-01-28 11:04  薛定谔病态猫  阅读(1031)  评论(0编辑  收藏  举报