SpringBoot学习之SpringBoot执行器

简介: 在以往的分布式开发当中,各个服务节点的监控必不可少。监控包含有很多方面,比如说:内存占用情况,节点是否健康等。在spring-boot会给我们提供相关资源监控叫做spring-boot-actuator, 通过执行器可以帮我管理和监控生产环境下的应用服务。

    在以往的分布式开发当中,各个服务节点的监控必不可少。监控包含有很多方面,比如说:内存占用情况,节点是否健康等。在spring-boot会给我们提供相关资源监控叫做spring-boot-actuator通过执行器可以帮我管理和监控生产环境下的应用服务。

 一。添加SpringBoot执行器的依赖(版本2.0.0.RELEASE)

   添加gradle配置依赖:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
}
View Code

 

 二。关于SpringBoot的端点

     端点是一个提供给我们监控应用程序的功能点,SpringBoot提供了一系列内置端点叫我们使用,举个例子:health端点为我们提供了一个对我们基础程序的一个健康状态的监控

每个端点都可以打开或关闭,绝大多数的应用都可以通过http请求进行访问,springboot包含很多内置内置端点。tips:参考官网的

ID Description Enabled by default

auditevents

Exposes audit events information for the current application.

Yes

beans

Displays a complete list of all the Spring beans in your application.

Yes

conditions

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

Yes

configprops

Displays a collated list of all @ConfigurationProperties.

Yes

env

Exposes properties from Spring’s ConfigurableEnvironment.

Yes

flyway

Shows any Flyway database migrations that have been applied.

Yes

health

Shows application health information.

Yes

httptrace

Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).

Yes

info

Displays arbitrary application info.

Yes

loggers

Shows and modifies the configuration of loggers in the application.

Yes

liquibase

Shows any Liquibase database migrations that have been applied.

Yes

metrics

Shows ‘metrics’ information for the current application.

Yes

mappings

Displays a collated list of all @RequestMapping paths.

Yes

scheduledtasks

Displays the scheduled tasks in your application.

Yes

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.

Yes

shutdown

Lets the application be gracefully shutdown.

No

threaddump

Performs a thread dump.

Yes

  

有几点要补充说明一下:

  1). 我们访问的端点监控的地址规范是:/actuator/{ID}的方式访问,比如说:health端点默认被映射的路径就是/actuator/health

      2) 并不是所有端点都可以通过http请求访问,以下表格列举了各个端点的状态值:

ID JMX Web

auditevents

Yes

No

beans

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

  
    我们可以发现默认情况下只有health与info端点才能通过http请求访问,当然我们可以在属性文件中配置  management.endpoints.web.exposure.include 来开放可以访问的端点:
    
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

 

3)当我们访问端点时,响应的数据会缓存一定的时间,我们可以通过这个属性进行配置:

  

management.endpoint.<id>.cache.time-to-live=10s

 

4) 我们也可以自己定义监视路径,默认情况是:/actuator/xxx,通过如下属性可以设置:
management.endpoints.web.base-path=/

5) 关于保护敏感端点,首先我们要添加对spring-security的依赖,并设置进行安全验证的用户名,密码以及角色,如果不使用spring-security就要慎重考虑暴露端口的端点了

 

   三。关于Health端点

  1.  health端点用于检测我们运行程序的健康状态,当程序宕机时,可以提供给开发人员相关的提示信息
  2. 默认情况下该端点只是显示简略的监控信息,不过我们可以通过management.endpoint.health.show-details属性来让其显示详细的监控信息
  3. 端点有如下几种状态: UP DOWN UNKNOW-SERVICE 从字面上我们很好理解
  4. 实现自定义健康监控:
package com.bdqn.lyrk.springboot.study.monitor;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class SelfMonitor implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().withDetail("health","next").build();
    }
}
View Code

    例子很简单,主要是实现HealthIndicator接口,当我们访问:http://localhost:8080/actuator/health时,我们可以看到相关监控信息,如下图:

    

 

目录
相关文章
|
18天前
|
缓存 监控 安全
Spring Boot 的执行器注解:@Endpoint、@ReadOperation 等
Spring Boot Actuator 提供多种生产就绪功能,帮助开发者监控和管理应用。通过注解如 `@Endpoint`、`@ReadOperation` 等,可轻松创建自定义端点,实现健康检查、指标收集、环境信息查看等功能,提升应用的可观测性与可管理性。
Spring Boot 的执行器注解:@Endpoint、@ReadOperation 等
|
6月前
|
监控 Java 应用服务中间件
微服务——SpringBoot使用归纳——为什么学习Spring Boot
本文主要探讨为什么学习Spring Boot。从Spring官方定位来看,Spring Boot旨在快速启动和运行项目,简化配置与编码。其优点包括:1) 良好的基因,继承了Spring框架的优点;2) 简化编码,通过starter依赖减少手动配置;3) 简化配置,采用Java Config方式替代繁琐的XML配置;4) 简化部署,内嵌Tomcat支持一键式启动;5) 简化监控,提供运行期性能参数获取功能。此外,从未来发展趋势看,微服务架构逐渐成为主流,而Spring Boot作为官方推荐技术,与Spring Cloud配合使用,将成为未来发展的重要方向。
203 0
微服务——SpringBoot使用归纳——为什么学习Spring Boot
|
5月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
147 20
|
8月前
|
负载均衡 IDE Java
SpringBoot整合XXL-JOB【04】- 以GLUE模式运行与执行器负载均衡策略
在本节中,我们将介绍XXL-JOB的GLUE模式和集群模式下的路由策略。GLUE模式允许直接在线上改造方法为定时任务,无需重新部署。通过一个测试方法,展示了如何在调度中心配置并使用GLUE模式执行定时任务。接着,我们探讨了多实例环境下的负载均衡策略,确保任务不会重复执行,并可通过修改路由策略(如轮训)实现任务在多个实例间的均衡分配。最后,总结了GLUE模式和负载均衡策略的应用,帮助读者更深入理解XXL-JOB的使用。
398 9
SpringBoot整合XXL-JOB【04】-  以GLUE模式运行与执行器负载均衡策略
|
8月前
|
Java 中间件 调度
SpringBoot整合XXL-JOB【03】- 执行器的使用
本文介绍了如何将调度中心与项目结合,通过配置“执行器”实现定时任务控制。首先新建SpringBoot项目并引入依赖,接着配置xxl-job相关参数,如调度中心地址、执行器名称等。然后通过Java代码将执行器注册为Spring Bean,并声明测试方法使用`@XxlJob`注解。最后,在调度中心配置并启动定时任务,验证任务是否按预期执行。通过这些步骤,读者可以掌握Xxl-Job的基本使用,专注于业务逻辑的编写而无需关心定时器本身的实现。
1403 10
SpringBoot整合XXL-JOB【03】-  执行器的使用
|
11月前
|
Java Maven Spring
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
这篇文章介绍了在IntelliJ IDEA社区版中创建Spring Boot项目的三种方法,特别强调了第三种方法的详细步骤。
9210 0
springboot学习一:idea社区版本创建springboot项目的三种方式(第三种为主)
|
10月前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
220 9
|
11月前
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
133 9
|
11月前
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
87 1
|
11月前
|
Java 测试技术 开发者
springboot学习四:Spring Boot profile多环境配置、devtools热部署
这篇文章主要介绍了如何在Spring Boot中进行多环境配置以及如何整合DevTools实现热部署,以提高开发效率。
478 2