avatar

SpringBoot热部署与监控

开发热部署

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

  • 模板引擎

    • 在SpringBoot开发情况下禁用模板引擎的cache
    • 页面模板改变ctrl+F9可以重新编译当前页面并生效
  • Spring Loaded
    Spring官方提供的热部署程序,实现修改类文件的热部署

  • 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为我们提供的准生产环境下的应用监控和管理功能。我们可以通过HTTPJMXSSH协议来进行操作,自动得到审计、健康及指标信息等

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端点:
    management.endpoint.shutdown.enabled=true
  • 另外可以通过management.endpoints.enabled-by-default来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:
    management.endpoints.enabled-by-default=false
    management.endpoint.info.enabled=true
  • 修改路径
    # 修改根目录路径
    management.endpoints.web.base-path=/management
    # 修改/health路径
    management.endpoints.web.path-mapping.health=healthcheck
    更多参阅spring-actuator官方文档

自定义健康状态指示器HealthIndicator

1、编写一个指示器,继承AbstractHealthIndicator并实现doHealthCheck函数或者实现HandlerInterceptor的healyh函数
2、指示器的名字必须是 XXXHealthIndicator,接口返回的json就叫XXX

@Component
public class MyHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
System.out.println("进入健康检查");
builder.up().withDetail("msg","aaaa");
}
}

这里还有一个问题,就是需要打开health的一个配置我们才能看到信息

management.endpoint.health.show-details=always

自定义EndPoint

1、编写自定义的Endpoint类
2、在类上面标注Endpoint
3、类里面写主方法,并在主方法上标注@ReadOperation
4、编写配置类,然后将自定义的EndPoint类注入到容器中

@Endpoint(id = "mytest")
public class Myendpoint {

@ReadOperation
public Map<String,String> mytest() {
Map<String,String> a = new HashMap<>();
a.put("status","OK");
return a;
}
}

编写配置类

@Configuration
public class endpointConfig {
@Bean
public Myendpoint getMyendpoint()
{
return new Myendpoint();
}
}

启动容器后,我们能通过http://localhost:8080/actuator/mytesturl返回我们的信息

更多参阅spring-actuator官方文档

文章作者: zenshin
文章链接: https://zlh.giserhub.com/2020/04/30/springboot/monitoring/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 zenshin's blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论